Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,54 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
+
import cv2
|
3 |
+
import cvzone
|
4 |
+
import math
|
5 |
+
import imutils
|
6 |
+
from imutils.video import VideoStream
|
7 |
+
|
8 |
+
cap = cv2.VideoCapture(0)#'rtsp://admin:[email protected]:554/Streaming/Channels/1')
|
9 |
+
cap.set(3, 640)
|
10 |
+
cap.set(4, 128)
|
11 |
+
|
12 |
+
#cap = cv2.VideoCapture("C:/Users/Moh/PycharmProjects/pythonProject/video/GVD.mp4")
|
13 |
+
|
14 |
+
model = YOLO("best20.pt")
|
15 |
+
|
16 |
+
classNames = ['person', 'Gun' ]
|
17 |
+
|
18 |
+
while True:
|
19 |
+
success, frame = cap.read()
|
20 |
+
|
21 |
+
result = model(frame, stream=True)
|
22 |
+
|
23 |
+
for r in result:
|
24 |
+
boxes = r.boxes
|
25 |
+
for box in boxes:
|
26 |
+
# bounding box
|
27 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
28 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
29 |
+
w, h = x2 - x1, y2 - y1
|
30 |
+
|
31 |
+
#confidence
|
32 |
+
|
33 |
+
conf = math.ceil((box.conf[0] * 100)) / 100
|
34 |
+
|
35 |
+
#className
|
36 |
+
|
37 |
+
cls = int(box.cls[0])
|
38 |
+
currentClass = classNames[cls]
|
39 |
+
|
40 |
+
if currentClass == "Gun" and conf > 0.4:
|
41 |
+
print(conf)
|
42 |
+
|
43 |
+
cvzone.cornerRect(frame, (x1, y1, w, h), l=9, rt=5, colorR=(255, 0, 0))
|
44 |
+
cvzone.putTextRect(frame, f'{conf}{currentClass}', (max(0, x1), max(0, y1)))
|
45 |
+
print(conf)
|
46 |
+
|
47 |
+
frame = imutils.resize(frame, width=1200)
|
48 |
+
cv2.imshow('AsimCodeCam', frame)
|
49 |
+
key = cv2.waitKey(1) & 0xFF
|
50 |
+
if key == ord('q'):
|
51 |
+
break
|
52 |
+
|
53 |
+
cv2.destroyAllWindows()
|
54 |
+
video_stream.stop()
|