forked from Ayushlion8/Guardian-Vision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_temp.py
More file actions
45 lines (37 loc) · 1.51 KB
/
video_temp.py
File metadata and controls
45 lines (37 loc) · 1.51 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
from ultralytics import YOLO
import cv2
import math
def video_detection_vid(path_x):
video_capture = 0
#Create a Webcam Object
cap=cv2.VideoCapture(video_capture)
frame_width=int(cap.get(3))
frame_height=int(cap.get(4))
model=YOLO('ModelWeights\gun_nano_best.pt')
classNames = ["GUN","Knife"]
while True:
success, img = cap.read()
results=model(img,stream=True,conf=0.5)
for r in results:
boxes=r.boxes
for box in boxes:
x1,y1,x2,y2=box.xyxy[0]
x1,y1,x2,y2=int(x1), int(y1), int(x2), int(y2)
print(x1,y1,x2,y2)
conf=math.ceil((box.conf[0]*100))/100
cls=int(box.cls[0])
class_name=classNames[cls]
label=f'{class_name} {conf*100}%'
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
print(t_size)
c2 = x1 + t_size[0], y1 - t_size[1] - 3
color = (105, 245,66)
if (conf >=0.75):
color = (66, 138, 245)
if (conf >= 0.9):
color = (66, 66, 245)
cv2.rectangle(img, (x1,y1), (x2,y2), color ,3)
cv2.rectangle(img, (x1,y1), c2, color, -1, cv2.LINE_AA) # filled
cv2.putText(img, label, (x1,y1-2),0, 1,[255,255,255], thickness=2,lineType=cv2.LINE_AA)
yield img
cv2.destroyAllWindows()