在人脸检测中,使用预先被训练过的模型文件去侦测脸和眼睛。同样使用预先被训练过来的模型文件去检测汽车。将用这个模型检测出来的汽车用矩形框出来。
代码如下:
# OpenCV Python program to detect cars in video frame
# import libraries of python OpenCV
import cv2
# 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')
# loop runs if capturing has been initialized.
while True:
# reads frames from a video
ret, frames = cap.read()
# 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)
# Wait for Esc key to stop
if cv2.waitKey(33) == 27:
break
# De-allocate any associated memory usage
cv2.destroyAllWindows()
下面用个图片来试下,代码改成:
# OpenCV Python program to detect cars in video frame
# import libraries of python OpenCV
import cv2
if __name__ == '__main__':
# capture frames from a video
cap = cv2.imread('c1.jpg')
# Trained XML classifiers describes some features of some object we want to detect
car_cascade = cv2.CascadeClassifier('haarcascade_car.xml')
# convert to gray scale of each frames
gray = cv2.cvtColor(cap, 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(cap, (x, y), (x + w, y + h), (0, 0, 255), 2)
# Display frames in a window
cv2.imshow('ret', cap)
cv2.waitKey(0)
# De-allocate any associated memory usage
cv2.destroyAllWindows()
pass
结果如下: