-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
181 lines (148 loc) · 5.8 KB
/
render.py
File metadata and controls
181 lines (148 loc) · 5.8 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import cv2
import numpy as np
from imutils.object_detection import non_max_suppression
### RENDERER Configuration
imagecount = 0
activeyolo = False
activetracker = False
activeMosse = False
#### OBJECT DETECTION
if activeyolo:
#Import YOLO
import sys
sys.path.append('/Users/Julian/GitHub/darkflow-master')
print("YOLO - Building CNN")
import yolo as persondetection
import csv
#Parameters YOLO
IDobject = 0
#### OBJECT TRACKER
if activetracker:
#Import TRACKER
import tracker as tracking
#Parameters TRACKER
objecttracks = []
#### Mosse TRACKER
if activeMosse:
#Import TRACKER
import Mossetracker as tracking
#Parameters TRACKER
objecttracks = []
def renderimage(image):
#Globale Variablenzugriffe
global imagecount
global IDobject
print("RENDERER - Starting frame " + str(imagecount))
#Erzeugt Resultatbild
result = image.copy()
if activetracker:
#Bestehende Objekttracker aktualisieren
print("TRACKER - Update current object trackers")
for object in objecttracks:
stop = object.nextimage(image)
if stop:
objecttracks.remove(object)
print("TRACKER - Remove outdated object tracker")
if activeMosse:
#Bestehende Mossetracker aktualisieren
print("TRACKER - Update current object trackers")
for object in objecttracks:
frame_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
stop = object.update(frame_gray)
if stop:
objecttracks.remove(object)
print("TRACKER - Remove outdated object tracker")
if activeyolo:
##YOLO
print("YOLO - Detecting new objects")
YOLO_objects = persondetection.YOLOperson(image)
#Annotiert aktuelle Framenummer
for box in YOLO_objects:
box['image'] = imagecount
#Compare to currently tracked objects
if activetracker:
remainingdetects = []
for detect in YOLO_objects:
cancel = False
x = detect['topleft']['x']
y = detect['topleft']['y']
w = detect['bottomright']['x'] - detect['topleft']['x']
h = detect['bottomright']['y'] - detect['topleft']['y']
for track in objecttracks:
currentwindow = track.getcurrenttrackwindow()
xt,yt,wt,ht = currentwindow
detectandtrack = np.array([[x,y, x+w,y+h],[xt, yt, xt + wt, yt + ht]])
suppression = non_max_suppression(detectandtrack, probs=None, overlapThresh=0.4)
if len(suppression)==1:
cancel = True
print("YOLO - Removing new detection due to existing person tracker")
track.update(detect, image)
if not cancel:
remainingdetects.append(detect)
YOLO_objects = remainingdetects
#Initialisiert neue Objekttracker
for object in YOLO_objects:
IDobject = IDobject + 1
object['ID'] = IDobject
newtracker = tracking.trackobject(10, object, image)
objecttracks.append(newtracker)
if activeMosse:
remainingdetects = []
for detect in YOLO_objects:
cancel = False
x = detect['topleft']['x']
y = detect['topleft']['y']
w = detect['bottomright']['x'] - detect['topleft']['x']
h = detect['bottomright']['y'] - detect['topleft']['y']
for track in objecttracks:
(xt, yt), (wt, ht) = track.pos, track.size
x1, y1, x2, y2 = int(xt-0.5*wt), int(yt-0.5*ht), int(xt+0.5*wt), int(yt+0.5*ht)
detectandtrack = np.array([[x,y, x+w,y+h],[x1, y1, x2, y2]])
suppression = non_max_suppression(detectandtrack, probs=None, overlapThresh=0.8)
if len(suppression)==1:
cancel = True
print("TRACKER - Removing current tracker due to existing new detection")
objecttracks.remove(track)
if not cancel:
remainingdetects.append(detect)
#Initialisiert neue Objekttracker
for object in YOLO_objects:
#IDobject = IDobject + 1
#object['ID'] = IDobject
x1 = object['topleft']['x']
y1 = object['topleft']['y']
x2 = object['bottomright']['x']
y2 = object['bottomright']['y']
frame_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rect = (x1,y1,x2,y2)
newtracker = tracking.MOSSE(frame_gray, rect, 15)
objecttracks.append(newtracker)
##Ausgabe kreieren
print("RENDERER - Create Output")
if activetracker:
print("TRACKER - Drawing Bounding Boxes")
#Bestehende Objekttracker einzeichnen
lastsignal = []
for object in objecttracks:
object.drawobjectBB(result)
if activeMosse:
print("Mosse - Drawing Bounding Boxes")
for tracker in objecttracks:
tracker.draw_state(result)
imagecount = imagecount + 1
return result
def checktext(text,previoustext):
check = False
if not text == 'unbekannt':
if len(previoustext) <= len(text):
check = True
return check
###Auswertungsdateien speichern
def saveresults():
print("RENDERER - Ergebnisse speichern")
keys = objecttracks[0].keys()
with open('detections.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=keys)
writer.writeheader()
for p in objecttracks:
writer.writerow(p)