-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcar_detection.py
30 lines (24 loc) · 789 Bytes
/
car_detection.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
import cv2
import numpy as np
import os
directory = './dataset/'
path = './to_rectified'
def car_detection():
#use trained cars XML classifiers
car_cascade = cv2.CascadeClassifier('cars.xml')
images = []
ind = 0
for filename in sorted(os.listdir(directory)):
rect = []
print(ind)
i = cv2.imread(os.path.join(directory, filename))
if i is not None:
if ind%2 == 0:
gray = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(gray, 1.1, 2, minSize=(210, 210))
if len(cars) == 0:
for (x, y, w, h) in cars:
i[y:y+w, x:x+h] = 0
cv2.imwrite(os.path.join(path, str(ind)+'.jpg'), i)
ind = ind + 1
car_detection()