-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_threaded.py
96 lines (82 loc) · 2.67 KB
/
camera_threaded.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
from threading import Thread, Lock
import numpy as np
import cv2
import sys
sys.path.append("./object_detection")
import matplotlib.pyplot as plt
from object_detection.notebooks import visualization
from object_detection.feat import FeatureVectors
from audio import mix
from subprocess import Popen
class WebcamVideoStream :
def __init__(self, src=1, width=300, height=200) :
self.stream = cv2.VideoCapture(src)
self.stream.set(3, width)
self.stream.set(4, height)
(self.grabbed, self.frame) = self.stream.read()
self.started = False
self.read_lock = Lock()
def start(self) :
if self.started :
print ("already started!!")
return None
self.started = True
self.thread = Thread(target=self.update, args=())
self.thread.start()
return self
def update(self) :
while self.started :
(grabbed, frame) = self.stream.read()
self.read_lock.acquire()
self.grabbed, self.frame = grabbed, frame
self.read_lock.release()
def read(self) :
self.read_lock.acquire()
frame = self.frame.copy()
self.read_lock.release()
return frame
def stop(self) :
self.started = False
if self.thread.is_alive():
self.thread.join()
def __exit__(self, exc_type, exc_value, traceback) :
self.stream.release()
if __name__ == "__main__" :
vs = WebcamVideoStream().start()
fv = FeatureVectors().start()
oldprocesses = []
processes = []
names = []
try:
while True :
frame = vs.read()
fv = fv.update(frame)
features, rbboxes, rclasses, rscores = fv.read()
# print(features)
# print(rclasses)
for f, rcl in zip(features,rclasses):
names.append(mix(f,rcl))
if processes:
for p in processes: p.kill()
# oldprocesses = processes
processes = []
for n in names:
print(n)
processes.append(Popen(['ffplay', "-nodisp", "-autoexit", "-hide_banner", n]))
print("rboxes: " + str(rbboxes))
img = visualization.plt_bboxes(frame, rclasses, rscores, rbboxes)
cv2.imshow('webcam', img)
if cv2.waitKey(1) == 27:
break
except Exception as e:
fv.stop()
vs.stop()
cv2.destroyAllWindows()
for p in processes: p.kill()
for p in oldprocesses: p.kill()
raise e
fv.stop()
vs.stop()
cv2.destroyAllWindows()
for p in processes: p.kill()
for p in oldprocesses: p.kill()