-
Notifications
You must be signed in to change notification settings - Fork 41
/
DetectAndTrackUtils.py
104 lines (95 loc) · 4.19 KB
/
DetectAndTrackUtils.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
98
99
100
101
102
103
104
import cv2
from yolo.detect import *
class TrackerList:
"""
Class allowing to list the whole tracker's history.
"""
def __init__(self):
self.ListOftrackers = {}
def add(self,frameNum, bbox, confidence):
self.ListOftrackers.update({frameNum:[bbox,confidence]})
def get_TrackerBox(self,frameNum):
try:
return True, self.ListOftrackers[frameNum][0]
except:
print("No tracker for frame"+str(frameNum))
return False
def get_TrackerConfidence(self,frameNum):
try:
return self.ListOftrackers[frameNum][1]
except:
print("No tracker for frame"+str(frameNum))
return False
class DetecterList:
"""
Class allowing to list the whole detection's history.
"""
def __init__(self):
self.ListOfDetections = {}
def add(self, frameNum, bbox, confidence):
self.ListOfDetections.update({frameNum:[bbox,confidence]})
def get_TrackerBox(self,frameNum):
try:
return True, self.ListOfDetections[frameNum][0]
except:
print("No tracker for frame"+str(frameNum))
return False
def get_TrackerConfidence(self,frameNum):
try:
return self.ListOfDetections[frameNum][1]
except:
print("No tracker for frame"+str(frameNum))
return False
#####
def plotTracking(frame, bbox, confidence, fps):
"""
Draws Bounding box arround tracker "bbox" and writes on the frame the tracking information.
:return: ImageDraw
"""
### Draw bounding box
ImageDraw = frame.copy()
ImageDraw = cv2.rectangle(ImageDraw, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)),
(50, 255, 50), 2)
# Display State and FPS on frame
cv2.putText(ImageDraw, "GOTURN TRACKING", (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
cv2.putText(ImageDraw, "FPS : " + str(int(fps)), (100, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
try:
cv2.putText(ImageDraw, "Tracking accuracy confidence : " + str(round(confidence[0],2)), (100, 100),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
except:
cv2.putText(ImageDraw, "Tracking accuracy confidence : ", (100, 100),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
return ImageDraw
def plotDetection(frame, DetectFlag, bbox=None,confidence=None):
"""
Draws Bounding box arround detection "bbox" if there is a detection
and writes on the frame the detection information
:return: ImageDraw
"""
ImageDraw = frame.copy()
### Display State and FPS on frame
cv2.putText(ImageDraw, "YOLO DETECTION", (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
cv2.putText(ImageDraw, "Sliding Window Detection: True", (100, 75),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
if DetectFlag:
### Add bbox to the image with label
label = '%s %.2f' % ('Drone', confidence)
plot_one_box([bbox.x1, bbox.y1, bbox.x2, bbox.y2], ImageDraw, label=label, color = (223,82,134))
return ImageDraw
def showSlidingWindow( frame, cropWidth, cropHeight, SlidingWinSize,fps):
"""
Plots the search reagion on the frame as well as all relevant detection information.
"""
ImageDraw = frame.copy()
### Display State and FPS on frame
cv2.putText(ImageDraw, "YOLO DETECTION", (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
cv2.putText(ImageDraw, "Sliding Window Detection: True", (100, 75),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
cv2.putText(ImageDraw, "FPS: "+str(round(fps)), (100, 100),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 50, 50), 2)
### Display sliding window
ImageDraw[cropHeight:cropHeight+5, cropWidth:cropWidth+SlidingWinSize] =(255,255,200)
ImageDraw[cropHeight+SlidingWinSize:cropHeight+SlidingWinSize+5, cropWidth:cropWidth+SlidingWinSize]=(255,255,200)
ImageDraw[cropHeight: cropHeight+SlidingWinSize,cropWidth:cropWidth+5]=(255,255,200)
ImageDraw[cropHeight:cropHeight+SlidingWinSize,cropWidth+SlidingWinSize:cropWidth+SlidingWinSize+5]=(255,255,200)
return ImageDraw