Upload 2 files
Browse files- GunDetect.py +39 -0
GunDetect.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import cv2 as cv
|
| 3 |
+
|
| 4 |
+
def process():
|
| 5 |
+
writer = None
|
| 6 |
+
(W, H) = (None, None)
|
| 7 |
+
path = "../videos/gun.mp4"
|
| 8 |
+
model = YOLO("gunModel.pt")
|
| 9 |
+
vs = cv.VideoCapture(path)
|
| 10 |
+
while True:
|
| 11 |
+
# read the next frame from the file
|
| 12 |
+
(grabbed, frame) = vs.read()
|
| 13 |
+
if not grabbed:
|
| 14 |
+
break
|
| 15 |
+
results = model.predict(frame, stream=False)
|
| 16 |
+
for result in results:
|
| 17 |
+
for data in result.boxes.data.tolist():
|
| 18 |
+
draw_Box(data,frame," ")
|
| 19 |
+
|
| 20 |
+
if writer is None:
|
| 21 |
+
# initialize our video writer
|
| 22 |
+
fourcc = cv.VideoWriter_fourcc(*"MJPG")
|
| 23 |
+
writer = cv.VideoWriter("op3.mp4", fourcc, 24,
|
| 24 |
+
(frame.shape[1], frame.shape[0]), True)
|
| 25 |
+
print("writing")
|
| 26 |
+
writer.write(frame)
|
| 27 |
+
cv.imshow('image', frame)
|
| 28 |
+
cv.waitKey(24)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def draw_Box(data,image,name):
|
| 32 |
+
x1,y1,x2,y2,_,_ = data
|
| 33 |
+
p1 = (int(x1),int(y1))
|
| 34 |
+
p2 = (int(x2),int(y2))
|
| 35 |
+
cv.rectangle(image,p1,p2,(0,0,255),3)
|
| 36 |
+
cv.putText(image, "gun", p1, cv.FONT_HERSHEY_SIMPLEX, 2, (0, 200, 255),
|
| 37 |
+
9)
|
| 38 |
+
|
| 39 |
+
process()
|