-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (74 loc) · 2.81 KB
/
main.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 os
import sys
import cv2
import time
import numpy as np
from datetime import datetime
import objectdetector
import videostream
import visualiser
from objectdetector import InitModel
from tracker import tracking_handler
sys.path.append("../Backend/")
#*TEST BLOCK {...}
#~Camera feed globals
VIDEO_SOURCE = 0 #0
FRAME_INPUT_DIMS = (720,405)
#~Object detection inference globals
#INF_GRAPH='ssd_inception_v2_coco_2018_01_28/frozen_inference_graph.pb' #*det_thres=0.35, nms_thres=0.8, tracker:0.2,0.7,0.2,5,4, R scalar *100 (much more noise)
INF_GRAPH = 'inceptionv2frcnn/frozen_inference_graph.pb' #*det_thres=0.75, nms_thres=0.75, tracker:0.3,0.6,0.5,5,2
LABELMAP='labelmap.pbtxt'
def write_data(frame, data):
timestamp = str(datetime.now())
output_string = timestamp + ">> " + str(data) + "\n"
return output_string
def main():
#~ Initialise deep learning model to detect objects
modelinfo = InitModel(INF_GRAPH, LABELMAP)
labels = modelinfo.load_labels()
sess,det_graph = modelinfo.graph_import()
#~ Initialise video stream
frame_count = 0 #debug
stream = videostream.set_input_feed(VIDEO_SOURCE)
stream.start()
#~ Start main loop for constant detection and tracking
while(stream.running):
#~ Get current active frame
fps_a = time.time()
frame = stream.get()
frame = cv2.resize(frame, (FRAME_INPUT_DIMS[0], int(FRAME_INPUT_DIMS[1])))
#~ Get detections off the current frame
detections = objectdetector.model_inference(sess, frame, det_graph, labels)
print("Number of initial detections: ", len(detections))
#~ Track these detections
tracked_detections = tracking_handler(detections, FRAME_INPUT_DIMS)
print("Number of currently tracked detections: ", len(tracked_detections))
#~ Data transfer function
output_string = write_data(frame, tracked_detections)
print(output_string)
#~ Calculate FPS
fps_b = time.time()
fps = str(round(1/(fps_b - fps_a),2))
#~ Display debug function for the bounding boxes with labels and person ID
# To show only detections add optional argument SHOW="DETECTED_ONLY"
# or for trackers "TRACKED_ONLY", or for only counter, "COUNTER_ONLY",
# to show nothing add empty string "" or "NONE"
frame = visualiser.show(
frame,
[detections,tracked_detections],
labels,
fps,
frame_count,
threshold,
counter,
SHOW="ALL")
#~ Show frame
cv2.imshow("debug feed", frame)
frame_count += 1 #debug
if cv2.waitKey(25) and 0xFF == ord('q'): #put back
break
cv2.destroyAllWindows()
sess.close()
exit()
if __name__ == "__main__":
main()