通过 OpenCV 将 MTCNN 与网络摄像头配合使用 [英] Using MTCNN with a webcam via OpenCV

查看:58
本文介绍了通过 OpenCV 将 MTCNN 与网络摄像头配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用网络摄像头并将 MTCNN 用作主要的面部检测器.就像可以使用 Haar Cascades 一样,我想使用 MTCNN 在我的网络摄像头上查找人脸

I wish to be able to use a webcam and utilize MTCNN as the primary facial detector. Just as one can use Haar Cascades, I want to use MTCNN to find faces on my webcam

这个视频是关于打破 MTCNN 的,但仍然提供了对我目标的洞察:https://www.youtube.com/watch?v=OY70OIS8bxs

This video is about breaking MTCNN, but nonetheless provides insight into my goal: https://www.youtube.com/watch?v=OY70OIS8bxs

到目前为止,这是我的代码.以前是这样,情节会显示,我必须X"出来,但现在它不起作用

Here is my code so far. It used to be so that the plot would show and I'd have to "X" it out but now it just doesn't work

from mtcnn.mtcnn import MTCNN 
import cv2 as cv
from matplotlib import pyplot
from matplotlib.patches import Rectangle

cap =  cv.VideoCapture(0)

detector = MTCNN()

#face = detector.detect_faces(img)


while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if (ret):
        # Our operations on the frame come here
        gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

        ax = pyplot.gca()
        face = detector.detect_faces(frame)
        face = pyplot.imread(frame)
        x, y, width, height = face[0]['box']
        rect = Rectangle((x, y), width, height, fill=False, color='red')
        ax.add_patch(rect)
        pyplot.imshow(frame)
        cv.imshow('frame',gray)
        pyplot.show()
     # Display the resulting frame
        #cv.imshow('frame',gray)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv.destroyAllWindows()

我希望有人能帮助我...

I was hoping someone could help me...

推荐答案

我目前遇到了类似的问题.我有一个程序可以裁剪视频的面部.我使用 OpenCV 读取帧,然后对它们进行裁剪.之后我想将裁剪后的人脸视频保存到一个新视频中.

Im experiencing a similiar issue at the moment. I have a program which crops the face of a video. Im using OpenCV to read in the frames and then perform the cropping on them. After that I want to save the cropped face video to a new video.

首先我也在使用 Haar Cascade.一切正常,但缺乏一些一般性能 -->它通常无法识别人脸.

First I was also using Haar Cascade. Everything is working fine but has some general performance lacks --> It often doesnt recognize faces.

现在我想使用 MTCNN.我更改了代码以使用 MTCNN.一切正常——>它读入帧,对其进行裁剪等等.但是,一旦我去保存视频,麻烦就会发生.代码运行良好,但在打开保存的视频后,我收到视频已损坏的错误消息.

Now I wanted to use MTCNN. I changed the code in order to work with MTCNN. Everything is working fine --> It reads in the frames, performs the crop on it etc. HOWEVER, as soon as I go to saving the video the trouble happens. The code runs fine, however after opening the saved video I get an error that the video is corrupted.

我坐了 2 小时,很困惑,因为代码是相同的.所有输出都相同(即格式、大小等)

I was sitting for 2h and was so confused, because the code is identical. All the outputs are same (i.e. format, size etc)

我现在必须得出结论,MTCNN 和 Opencv 之间存在一些错误.尽管这对我来说完全没有意义,为什么会发生这种情况.

I now have to conclude that there is some error between MTCNN and Opencv. Even though this totally doesnt make sense to me why this should happen.

更新:如果您运行以下代码:它运行良好,视频再次保存.但是,如果您取消注释顶部的 2 行 -->它会损坏文件,您将无法再获得工作视频.不幸的是,我还没有弄清楚原因.

Update: If you run the following code: It runs fine and the video is saved again. However if you uncomment the 2 lines at the top --> it will corrupt the file and you will no longer get a working video back. Unfortunately, I could not figure out the reason for that yet.

import cv2

# from mtcnn.mtcnn import MTCNN
# cropper = MTCNN()

read_video = cv2.VideoCapture('video.mp4')
fps = read_video.get(cv2.CAP_PROP_FPS)

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
write_video = cv2.VideoWriter('new3.mp4', fourcc, fps, (256,256))
images = []

success,image = read_video.read()
count = 0
while success:
    print(count)
    images.append(image)
    success, image = read_video.read()
    count += 1

for i in images:
    write_video.write(cv2.resize(i, (256, 256), interpolation=cv2.INTER_AREA))

这篇关于通过 OpenCV 将 MTCNN 与网络摄像头配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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