def detectvideo(vid_file, detector_xml_path, dest_img_dir):
if not os.path.exists(dest_img_dir):
os.makedirs(dest_img_dir)
detector = cv2.CascadeClassifier(detector_xml_path)
vid = imageio.get_reader(vid_file, 'ffmpeg')
# If size and source_size are not equal, then device was probably
# rotated (like a mobile) and we should compensate for the rotation.
# Images will have 'source_size' dimensions but we need 'size'.
metadata = vid.get_meta_data()
rotate = False
if metadata['source_size'] != metadata['size']:
print('Rotating')
rotate = True
for i, img in enumerate(vid):
if rotate:
#img = np.transpose(img, axes=(1, 0, 2)).copy()
img = np.rot90(img).copy()
print('Frame ',i, img.shape)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
min_size = (min(20, gray_img.shape[0] // 10), min(20, gray_img.shape[1] // 10))
hits = detector.detectMultiScale(gray_img, 1.1, 3, 0, min_size)
#cv2.groupRectangles(hits, 2)
print(len(hits), ' hits')
hits_img = np.copy(img)
if len(hits) > 0:
for (x,y,w,h) in hits:
cv2.rectangle(hits_img, (x,y), (x+w, y+h), (0,0,255), 2)
cv2.imwrite(os.path.join(dest_img_dir, 'frame-%d.png'%(i)), hits_img)
评论列表
文章目录