Using Mtcnn With A Webcam Via Opencv
Solution 1:
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.
First I was also using Haar Cascade. Everything is working fine but has some general performance lacks --> It often doesnt recognize faces.
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.
I was sitting for 2h and was so confused, because the code is identical. All the outputs are same (i.e. format, size etc)
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.
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 = 0while success:
print(count)
images.append(image)
success, image = read_video.read()
count += 1for i in images:
write_video.write(cv2.resize(i, (256, 256), interpolation=cv2.INTER_AREA))
Solution 2:
This is my code to use MTCNN on a webcam and it works
import cv2
from mtcnn import MTCNN
ksize = (101, 101)
font = cv2.FONT_HERSHEY_SIMPLEX
deffind_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()
whileTrue:
_, 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()
About the problem below:
change
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
to
fourcc = cv2.VideoWriter_fourcc(*'XVID')
It worked for me
Post a Comment for "Using Mtcnn With A Webcam Via Opencv"