-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_streaming.py
65 lines (44 loc) · 1.62 KB
/
camera_streaming.py
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
56
57
58
59
import numpy as np
import cv2
import imutils
import datetime
gun_cascade = cv2.CascadeClassifier('cascade.xml')
#camera = cv2.VideoCapture('data/soma.mp4')
camera = cv2.VideoCapture(0)
# initialize the first frame in the video stream
firstFrame = None
# loop over the frames of the video
gun_exist = False
while True:
(grabbed, frame) = camera.read()
# if the frame could not be grabbed, then we have reached the end of the video
if not grabbed:
break
# resize the frame, convert it to grayscale, and blur it
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
gun = gun_cascade.detectMultiScale(gray, 1.3, 5, minSize = (100, 100))
if len(gun) > 0:
gun_exist = True
for (x,y,w,h) in gun:
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
# if the first frame is None, initialize it
if firstFrame is None:
firstFrame = gray
continue
# draw the text and timestamp on the frame
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
# show the frame and record if the user presses a key
cv2.imshow("Security Feed", frame)
key = cv2.waitKey(1) & 0xFF
if gun_exist:
print("guns detected")
else:
print("guns NOT detected")
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()