-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
89 lines (71 loc) · 2.89 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import numpy as np
import argparse
from Filtering import Filtering as f
from Rules import Rules as r
def contours(frame):
contour, _ = cv2.findContours(frame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return contour, _
# Receiving the arguments
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--input',type=str, required=True, help='path to input video')
args = vars(ap.parse_args())
print('[INFO] input :', args['input'])
# Opening a video e checking if the video is valid
print('[INFO] Loading video...')
cap = cv2.VideoCapture(args['input'])
if (cap.isOpened() == False):
print("[INFO] Unable to read camera feed")
# Preparing the subtractors to identify the background
subtractor1 = cv2.createBackgroundSubtractorMOG2(800, 16, True) #it is not so good if compared with the o KNN method
subtractor2 = cv2.createBackgroundSubtractorKNN(dist2Threshold=1000, detectShadows=False)
frame_number = 0
filter = f()
print('[INFO] Video loaded. Showing it...')
while (True):
ret, frame = cap.read()
object_number = 0
frame2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Applying the subtractor and separetin background and foreground
mask = subtractor2.apply(frame2)
#cv2.imshow("mask", mask)
#mask11 = subtractor1.apply(frame2) -- it is not being used because the other presented method the best result, if you compare with the other
rett, mask = cv2.threshold(mask,127,255,cv2.THRESH_BINARY)
#cv2.imshow("mask", mask)
hei, wid = frame2.shape
# Taking the countours of the objects and drawning them on the frame
mask1 = mask.copy()
mask1 = filter.opening(mask1, 1, 1)
mask1 = filter.dilating(mask1, 2, 2)
# Fill the contours and the convex hull | I did twice because it presents better results this way.
c, hierarchy = contours(mask1)
hull = []
filter.fillCountours(mask1, c, wid, hei)
filter.fillConvexHull(mask1, hull, c, wid, hei)
c, hierarchy = contours(mask1)
hull = []
filter.fillCountours(mask1, c, wid, hei)
filter.fillConvexHull(mask1, hull, c, wid, hei)
cv2.imshow("filled convex hull", mask1)
# Drawning a rectangule around the objects
mask2 = mask1.copy()
c, hierarchy = contours(mask2)
for contour in c:
(x, y, w, h) = cv2.boundingRect(contour)
if cv2.contourArea(contour) < 700:
continue
crop = frame[y:y+h,x:x+w]
#cv2.imwrite('CARS/'+'car'+str(frame_number)+str(object_number)+'.png', crop)
object_number += 1
cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2)
cv2.putText(frame,'object',(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),1)
frame_number += 1
#Defining the Region of Interest
roi = r()
roi.defineROI(frame)
# Display the resulting frame
cv2.imshow("classified", frame)
cv2.waitKey(50)
print('[INFO] Done!')
cap.release()
cv2.destroyAllWindows()