-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
executable file
·92 lines (75 loc) · 2.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#===============================================================================
# Brno University of Technology
# Faculty of Information Technology
# Academic year: 2018/2019
# Bachelor thesis: Monitoring Pedestrian by Drone
# Author: Vladimír Dušek
#===============================================================================
import os
import math
import cv2
from enum import Enum
from PyQt4.QtGui import QImage, QPixmap
# Smaller the parameter P is, thicker and bigger captions and borders are, according to a function y = sqrt(x/4P)
P = 0.5
class InputType(Enum):
"""
Enum for type of input
"""
VIDEO = 1
IMAGE = 2
class OutputType(Enum):
"""
Enum for type of output
"""
PANORAMA = 1
BORDERS = 2
class MessageBoxType(Enum):
"""
Enum for type of PyQt Message Boxes
"""
ERROR = 1
WARNING = 2
INFORMATION = 3
def npimg_to_pixmap(np_img):
"""
Convert numpy array image to Qt pixmap image for visualization.
:param np_img: numpy image
:return: pixmap image
"""
height, width, channel = np_img.shape
bytes_per_line = 3 * width
qimg = QImage(np_img.data, width, height, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimg)
return pixmap
def create_folder(directory):
"""
Create directory, ignore exceptions.
:param directory: directory path and name
"""
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
pass
def draw_caption(image, box, caption, size, thickness):
"""
Draw caption of the detected object.
:param image: where to draw
:param box: coordinates
:param caption: caption text
:param size: font size
:param thickness: font thickness
"""
cv2.putText(image, caption, (box[0], box[1] - 10), cv2.FONT_HERSHEY_PLAIN, size, (0, 0, 0), int(thickness * 2.5))
cv2.putText(image, caption, (box[0], box[1] - 10), cv2.FONT_HERSHEY_PLAIN, size, (255, 255, 255), thickness)
def get_thickness(x, y):
"""
Get thickness of font or border considering picture resolution, according to function y = sqrt(x/4P).
:param x: width of image
:param y: height of image
:return: thickness
"""
z = (x * y + 100000) // 100000
thickness = int(math.sqrt(z / (4 * P) + 1))
return thickness