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
This video is about breaking MTCNN, but nonetheless provides insight into my goal: https://www.youtube.com/watch?v=OY70OIS8bxs
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...
Here is the simple code:
import cv2
from mtcnn import MTCNN
cap = cv2.VideoCapture(0)
detector = MTCNN()
while True:
ret,frame = cap.read()
output = detector.detect_faces(frame)
for single_output in output:
x,y,w,h = single_output['box']
cv2.rectangle(frame,pt1=(x,y), pt2=(x+w,y+h),color=(255,0,0),thickness=2)
cv2.imshow('win',frame)
if cv2.waitKey(1) & 0xFF == ('x'):
break
cv2.destroyAllWindows()
Dependencies, install these using the command in terminal then run the code above.
pip install mtcnn
pip install opencv-python
This works for me.