解决OPENCV保存视频无法播放问题

 opencv 保存视频无法播放!!原因是writer里的(width,height)设置的和while循环里的大小不一致,这样使用超参数把width,height定为一样的

使用get()方法获得width,height 转化为int型

import cv2
import numpy as np


if __name__ == '__main__':
    cv2.namedWindow('video', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('video', 640, 480)
    video = cv2.VideoCapture('./9.mp4')
    width = int(video.get(propId=cv2.CAP_PROP_FRAME_WIDTH)/2)
    height = int(video.get(propId=cv2.CAP_PROP_FRAME_HEIGHT)/2)



    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    writer = cv2.VideoWriter('ouu.mp4',fourcc,24,(width,height))

    fps = video.get(propId=cv2.CAP_PROP_FPS)

    print(fps,width,height)
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')


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


        if ret == True:
            frame = cv2.resize(frame, (width,height))


            gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)


            faces = face_detector.detectMultiScale(gray)
            for x,y,w,h in faces:
                # cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),4)
                face = frame[y:y+h,x:x+w]
                face = face[::5,::5]
                face = np.repeat(np.repeat(face,5,axis=0),5,axis=1)

                frame[y:y + h, x:x + w]=face[:h,:w]
            writer.write(frame)






            cv2.imshow('video',frame)


        key = cv2.waitKey(1)
        if key ==27:
            break

    cv2.destroyAllWindows()
    video.release()
    writer.release()

猜你喜欢

转载自blog.csdn.net/doumaidexiaozhi/article/details/127814507