-
Notifications
You must be signed in to change notification settings - Fork 1
/
Viddetection.py
61 lines (47 loc) · 1.65 KB
/
Viddetection.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
60
61
"""
Loads and displays a video.
"""
# Importing OpenCV
import cv2
import numpy as np
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('Vid1.mp4')
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video stream or file")
# Read the video
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
fgbg = cv2.createBackgroundSubtractorMOG2(
history=10,
varThreshold=2,
detectShadows=False)
# Converting the image to grayscale.
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Extract the foreground
edges_foreground = cv2.bilateralFilter(gray, 9, 75, 75)
foreground = fgbg.apply(edges_foreground)
# Smooth out to get the moving area
kernel = np.ones((50,50),np.uint8)
foreground = cv2.morphologyEx(foreground, cv2.MORPH_CLOSE, kernel)
# Applying static edge extraction
edges_foreground = cv2.bilateralFilter(gray, 9, 75, 75)
edges_filtered = cv2.Canny(edges_foreground, 60, 120)
# Crop off the edges out of the moving area
cropped = (foreground // 255) * edges_filtered
# Stacking the images to print them together for comparison
images = np.hstack((gray, edges_filtered, cropped))
cv2.imshow('Frame', cropped)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()