如何使用cv2和MTCNN在视频上书写?我遇到了Fourcc的问题 [英] How to write on video with cv2 AND MTCNN? I'm facing problems with fourcc

查看:106
本文介绍了如何使用cv2和MTCNN在视频上书写?我遇到了Fourcc的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使视频中的面孔匿名化的脚本.

I'm trying to write a script that anonymized faces on videos.

这是我的代码(python):

here is my code (python):

import cv2
from mtcnn.mtcnn import MTCNN

ksize = (101, 101)


def decode_fourcc(cc):
    return "".join([chr((int(cc) >> 8 * i) & 0xFF) for i in range(4)])


def find_face_MTCNN(color, result_list):
    for result in result_list:
        x, y, w, h = result['box']
        roi = color[y:y+h, x:x+w]
        cv2.rectangle(color, (x, y), (x+w, y+h), (0, 155, 255), 5)
        detectedFace = cv2.GaussianBlur(roi, ksize, 0)
        color[y:y+h, x:x+w] = detectedFace
    return color


detector = MTCNN()
video_capture = cv2.VideoCapture("basic.mp4")
width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
length = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(video_capture.get(cv2.CAP_PROP_FPS))

video_out = cv2.VideoWriter(
    "mtcnn.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))

while length:
    _, color = video_capture.read()
    faces = detector.detect_faces(color)
    detectFaceMTCNN = find_face_MTCNN(color, faces)
    video_out.write(detectFaceMTCNN)
    cv2.imshow("Video", detectFaceMTCNN)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

fourccIN = video_capture.get(cv2.CAP_PROP_FOURCC)
fourccOUT = video_out.get(cv2.CAP_PROP_FOURCC)
print(f"input fourcc is: {fourccIN, decode_fourcc(fourccIN)}")
print(f"output fourcc is: {fourccOUT, decode_fourcc(fourccOUT)}")

video_capture.release()
cv2.destroyAllWindows()

通过匿名化,我将获得一个完美的工作窗口,因此 imshow()可以正常工作.但是,新保存的视频"mtcnn.mp4"会无法打开.我发现问题是新视频的fourcc格式,因为我的输出是:

I'll get a perfect working window with the anonymization, so imshow() works fine. But the new saved video "mtcnn.mp4" can't be opened. I found out the problem is the fourcc format of the new video since my output is:

input fourcc is: (828601953.0, 'avc1')
output fourcc is: (-1.0, 'ÿÿÿÿ')

'ÿÿÿÿ'代表不可读,因此这就是问题的核心...

'ÿÿÿÿ' stands for unreadable so thats the core of the matter...

有人可以帮我吗?

他们可能面临着同样的问题:通过OpenCV将MTCNN与网络摄像头配合使用

They are facing probably the same problem: Using MTCNN with a webcam via OpenCV

然后我用它来编码fourcc: cv2.VideoWriter_fourcc的反面是什么?

And I used this to encode the fourcc: What is the opposite of cv2.VideoWriter_fourcc?

推荐答案

我更改了此行:

video_out = cv2.VideoWriter(
    "mtcnn.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))

收件人:

video_out = cv2.VideoWriter(
    "mtcnn.avi", cv2.VideoWriter_fourcc(*'XVID'), fps, (width, height))

现在它对我有用

这篇关于如何使用cv2和MTCNN在视频上书写?我遇到了Fourcc的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆