4

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

这个视频是关于打破 MTCNN,但仍然深入了解我的目标:https ://www.youtube.com/watch?v=OY70OIS8bxs

到目前为止,这是我的代码。以前是这样的

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()

我希望有人可以帮助我...

4

2 回答 2

2

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

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

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

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

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

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

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))
于 2020-09-16T22:04:41.217 回答
0

这是我在网络摄像头上使用 MTCNN 的代码,它可以工作

import cv2
from mtcnn import MTCNN

ksize = (101, 101)
font = cv2.FONT_HERSHEY_SIMPLEX


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


video_capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
detector = MTCNN()

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

video_capture.release()
cv2.destroyAllWindows()

关于以下问题:

改变

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

fourcc = cv2.VideoWriter_fourcc(*'XVID')

它对我有用

于 2020-11-16T10:00:10.313 回答