-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_and_human_tracking.py
100 lines (52 loc) · 1.77 KB
/
car_and_human_tracking.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
89
90
91
92
93
94
95
96
97
98
99
100
import cv2
# Our image
img_file = 'car.jpg'
# video = cv2.VideoCapture('cars1.mp4')
video = cv2.VideoCapture('ped_car_Trim1.mp4')
# Create open cv image
# img = cv2.imread(img_file)
# our pre-trained
classifier_file = 'cars_casscade.xml'
car_tracker = cv2.CascadeClassifier(classifier_file)
# pedestrian tracker
pedestrian_file = 'haarcascade_fullbody.xml'
pedestrian_tracker = cv2.CascadeClassifier(pedestrian_file)
# Run forever until car stops
while True:
(read_successful, frame) = video.read()
if read_successful:
grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
cars = car_tracker.detectMultiScale(grayscaled_frame)
pedestrian = pedestrian_tracker.detectMultiScale(grayscaled_frame)
for(x, y, w,h) in cars:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 2)
for(x, y, w,h) in pedestrian:
cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 255), 2)
# print(cars)
cv2.imshow('Car Detector', frame)
key = cv2.waitKey(1)
#Stop If q is pressed
if key==81 or key==113:
break
# Release the videoCapture object
video.release()
# # convert to grey scale
# black_n_white = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# # Create Class classifier
# car_tracker = cv2.CascadeClassifier(classifier_file)
# # detect cars
# cars = car_tracker.detectMultiScale(black_n_white)
# # Draw rectangle around the car
# # x , y , width, height (Blue, Green, Red), thickness
# for(x, y, w,h) in cars:
# cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
# # car1 = cars[0]
# # (x, y, w, h) = car1
# # cv2.rectangle(img, (x, y), (x+w, y+h), (0,255, 0), 2)
# # Display image with face doted
# cv2.imshow('Car Detector', img)
# # Dont autoclose (wait here in the code and listen a key press)
# cv2.waitKey()
print("Code Completed!!")