-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoloThread.py
More file actions
55 lines (50 loc) · 2.26 KB
/
Copy pathyoloThread.py
File metadata and controls
55 lines (50 loc) · 2.26 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
import threading
import datetime
from PySignal import Signal
import time
from time import sleep
import cv2
import torch
from config import *
class YoloThread(threading.Thread):
sendSignal = Signal()
def __init__(self):
threading.Thread.__init__(self)
self.Class_ = class_()
self.camera1 = cv2.VideoCapture(0)
self.camera1.set(cv2.CAP_PROP_FRAME_WIDTH,640)
self.camera1.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
self.model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
self._fps = self.camera1.get(cv2.CAP_PROP_FPS)
self.frame_size = (640, 640)
self.count = 0
self.cam1_frame = None
pass # end of SaverClass constructor
def run(self):
while(1):
image1_ret, frame1 = self.camera1.read()
results = self.model(frame1)
if image1_ret:
for box in results.xyxy[0]:
if True: #box[5]==0:
start = time.time()
dateTime = datetime.datetime.now()
self.count += 1
print(f"[INFO] counts is now = {self.count} ....................., Class Name = {self.Class_.numToName[int(box[5])]}........................")
xB = int(box[2])
xA = int(box[0])
yB = int(box[3])
yA = int(box[1])
#cv2.rectangle(frame1, (xA, yA), (xB, yB), (0, 255, 0), 2)
#cv2.putText(frame1,"%.2f %.2f %s"%(self._fps,float(box[4]),self.Class_.numToName[int(box[5])]),(xA,yA-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)
dimensions = [yA,yB,xA,xB]
self.sendSignal.emit(dateTime,self.Class_.numToName[int(box[5])],frame1,dimensions,self.count)
end = time.time()
print(f"[INFO] total time in capture picture loop = {end-start}..................................................................")
cv2.waitKey(1)
self.camera1.release()
pass # end of run function
pass # end of SaverClass
if __name__ == "__main__":
yoloThread = YoloThread()
yoloThread.start()