Repository navigation
Expand file tree
/
Copy pathVehicles_detection_final.py
More file actions
executable file
·62 lines (47 loc) · 1.89 KB
/
Copy pathVehicles_detection_final.py
File metadata and controls
executable file
·62 lines (47 loc) · 1.89 KB
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
# OpenCV Python program to detect cars in video frame
# import libraries of python OpenCV
import cv2
import pandas as pd
# capture frames from a video
cap = cv2.VideoCapture('video.avi')
# Trained XML classifiers describes some features of some object we want to detect
car_cascade = cv2.CascadeClassifier('cars.xml')
no_obj_det=0
frames_got_processed = 0
frame_processed = []
number_of_object_detected= []
# loop runs if capturing has been initialized.
while True:
# reads frames from a video
try:
ret, frames = cap.read()
frames_got_processed += 1
# convert to gray scale of each frames
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
# Detects cars of different sizes in the input image
cars = car_cascade.detectMultiScale(gray, 1.1, 1)
# To draw a rectangle in each cars
for (x,y,w,h) in cars:
cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)
# # Display frames in a window
cv2.imshow('video2', frames)
if cv2.waitKey(33) == 27:
break
# loop to count the number of objects detected at every 5th frame
if frames_got_processed % 5 == 0:
print "appended in frame number",frames_got_processed,len(cars)
frame_processed.append(frames_got_processed)
number_of_object_detected.append(len(cars))
except:
print "somthing went wrong"
break
# Display every 5th frame and number of object detected
print frame_processed
print number_of_object_detected
# writing results into CSV file
raw_data = {'frame_processed': frame_processed,
'number_of_object_detected':number_of_object_detected}
df = pd.DataFrame(raw_data, columns = ['frame_processed', 'number_of_object_detected'])
df.to_csv('example.csv')
# De-allocate any associated memory usage
cv2.destroyAllWindows()