-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreading_object_detection.py
More file actions
51 lines (40 loc) · 1.22 KB
/
multithreading_object_detection.py
File metadata and controls
51 lines (40 loc) · 1.22 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
import torch
import cv2
import numpy as np
import threading
cap_right = cv2.VideoCapture(0)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
results_lock = threading.Lock()
results=None
stop_flag=False
def detect_object():
global results,stop_flag
while True:
ret_right, frame_right = cap_right.read()
if ret_right==False:
break
else:
_results = model(frame_right)
# for res in results.xyxy[0]:
# print(results.names[int(res[5])])
with results_lock:
results=_results
cv2.imshow("frame-right",np.squeeze(results.render()))
if cv2.waitKey(1) & 0xFF == 27:
break
cap_right.release()
cv2.destroyAllWindows()
stop_flag=True
def print_results():
global results,stop_flag
while not stop_flag:
with results_lock:
if results is not None:
for res in results.xyxy[0]:
print(results.names[int(res[5])])
thread1=threading.Thread(target=detect_object)
thread2=threading.Thread(target=print_results)
thread1.start()
thread2.start()
thread1.join()
thread2.join()