This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
97 lines (74 loc) · 2.78 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
93
94
95
96
97
import cv2
import logging
import logging.handlers
import math
import sys
import numpy as np
def save_frame(frame, file_name, flip=True):
# flip BGR to RGB
if flip:
cv2.imwrite(file_name, np.flip(frame, 2))
else:
cv2.imwrite(file_name, frame)
def init_logging(to_file=False):
main_logger = logging.getLogger()
formatter = logging.Formatter(
fmt='%(asctime)s.%(msecs)03d %(levelname)-8s [%(name)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler_stream = logging.StreamHandler(sys.stdout)
handler_stream.setFormatter(formatter)
main_logger.addHandler(handler_stream)
if to_file:
handler_file = logging.handlers.RotatingFileHandler("debug.log", maxBytes=1024 * 1024 * 400 # 400MB
, backupCount=10)
handler_file.setFormatter(formatter)
main_logger.addHandler(handler_file)
# main_logger.setLevel(logging.DEBUG)
return main_logger
# =============================================================================
def distance(x, y, type='euclidian', x_weight=1.0, y_weight=1.0):
if type == 'euclidian':
return math.sqrt(float((x[0] - y[0])**2) / x_weight + float((x[1] - y[1])**2) / y_weight)
def get_centroid(x, y, w, h):
x1 = int(w / 2)
y1 = int(h / 2)
cx = x + x1
cy = y + y1
return (cx, cy)
def skeleton(img):
ret, img = cv2.threshold(img, 127, 255, 0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
done = False
size = np.size(img)
skel = np.zeros(img.shape, np.uint8)
while(not done):
eroded = cv2.erode(img, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(img, temp)
skel = cv2.bitwise_or(skel, temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros == size:
done = True
return skel
def calc_pathes_speed(pathes, meter_per_pixel, use_physical_speed):
pathes_speed = []
# calculate the average speed for each path
for i, path in enumerate(pathes):
# need at least two points to calculate speed
if len(path) >= 2:
start_point = path[0]
end_point = path[-1]
time_duration = end_point[2] - start_point[2]
d = distance(start_point[1], end_point[1])
if use_physical_speed:
# calculate physical speed in km/hour
d = d * (meter_per_pixel/1000) # distance in km
time_duration = time_duration/3600 # duration in hour
else:
# otherwise, use virtual speed in pixel/second
pass
path_speed = d / time_duration
else:
path_speed = 0.0
pathes_speed.append(path_speed)
return pathes_speed