微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用OpenCV VideoWriter保存包含矩形和已知人物姓名的视频?

如何解决如何使用OpenCV VideoWriter保存包含矩形和已知人物姓名的视频?

我正在尝试通过以下方式保存视频:

video = cv2.VideoCapture('video.mp4') 

(here's missing code,not important)

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
result = cv2.VideoWriter('output.mp4',fourcc,20.0,(640,480))

while True:
    ret,frame = video.read()

    if ret == True:
        result.write(frame)

        rgb_frame = frame[:,:,::-1]

        face_locations = face_recognition.face_locations(rgb_frame)
        face_encodings = face_recognition.face_encodings(rgb_frame,face_locations)

        for (top,right,bottom,left),face_encoding in zip (face_locations,face_encodings):
            matches = face_recognition.compare_faces(kNown_face_encodings,face_encoding)

            if True in matches:
                first_match_index = matches.index(True)
                name = kNown_face_names[first_match_index]

            else:
                name = 'UnkNown'

            cv2.rectangle(frame,(left,top),(right,bottom),(255,255,255),1)

            font = cv2.FONT_HERShey_DUPLEX
            cv2.putText(frame,name,bottom + 12),font,0.5,1)
        
        cv2.imshow('Video',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

result.release()
video.release()
cv2.destroyAllWindows()

我确实有output.mp4文件,但是我无法打开它,它说:

但是,如果我更改视频路径= cv2.VideoCapture(0),(“ 0”运行笔记本电脑网络摄像头),则工作正常。保存并运行.mp4文件 任何人都知道什么是错的吗? 附言我尝试更改格式,无济于事...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。