File size: 1,386 Bytes
81d9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from ultralytics import YOLO
import cv2
import cvzone
import math
import imutils
from imutils.video import VideoStream

cap = cv2.VideoCapture(0)#'rtsp://admin:[email protected]:554/Streaming/Channels/1')
cap.set(3, 640)
cap.set(4, 128)

#cap = cv2.VideoCapture("C:/Users/Moh/PycharmProjects/pythonProject/video/GVD.mp4")

model = YOLO("best20.pt")

classNames = ['person', 'Gun' ]

while True:
    success, frame = cap.read()

    result = model(frame, stream=True)

    for r in result:
        boxes = r.boxes
        for box in boxes:
            # bounding box
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            w, h = x2 - x1, y2 - y1

            #confidence

            conf = math.ceil((box.conf[0] * 100)) / 100

            #className

            cls = int(box.cls[0])
            currentClass = classNames[cls]

            if currentClass == "Gun"  and conf > 0.4:
                print(conf)

                cvzone.cornerRect(frame, (x1, y1, w, h), l=9, rt=5, colorR=(255, 0, 0))
                cvzone.putTextRect(frame, f'{conf}{currentClass}', (max(0, x1), max(0, y1)))
                print(conf)

    frame = imutils.resize(frame, width=1200)
    cv2.imshow('AsimCodeCam', frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break

cv2.destroyAllWindows()
video_stream.stop()