【问题标题】:Using MTCNN with a webcam via OpenCV通过 OpenCV 将 MTCNN 与网络摄像头一起使用
【发布时间】:2020-03-25 00:53:07
【问题描述】:

我希望能够使用网络摄像头并将 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()

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

【问题讨论】:

    标签: python opencv neural-network face facial-identification


    【解决方案1】:

    这是我在网络摄像头上使用 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')

    它适用于me

    【讨论】:

      【解决方案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))
      

      【讨论】:

      • 如果你已经弄明白了,别忘了更新你的答案。我们也很好奇。
      • 看看更新 --> 没有解决方案,只是证明 MTCNN 是它的起源
      • 好吧...我们现在要依靠 clarifai。
      猜你喜欢
      • 1970-01-01
      • 2011-06-29
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多