? ? ?今天在GitHub上看到一个对车辆训练好的模型,即xml文件,于是拿来测试了一个效果。我用这个xml文件对视频中的每一帧画面进行简单的车辆识别定位,演示代码如下:
import cv2import numpy as npcamera = cv2.VideoCapture ("video.avi")camera.open("video.avi")car_cascade = cv2.CascadeClassifier('cars.xml')while True: (grabbed,frame) = camera.read() grayvideo = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cars = car_cascade.detectMultiScale(grayvideo, 1.1, 1) # print(cars) # print(type(cars)) # print(cars.shape) # 部分输出如下所示: # [[255 62 37 37] # [144 25 35 35] # [219 81 62 62] # [246 52 54 54]] # < class 'numpy.ndarray'> # (4, 4) # ... for (x,y,w,h) in cars: cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2) cv2.imshow("video",frame) if cv2.waitKey(1)== ord('q'): breakcamera.release()cv2.destroyAllWindows()
运行程序,截取其中某几帧画面,如下:
显然,该模型对汽车识别的精度不够。
?附:测试视频以及xml模型文件下载地址
? ?https://download.csdn.net/download/weixin_41695564/10419268
87965765