mtcnn
mtcnn copied to clipboard
cut face from video
hi! I am newbie in python.
I needed a little magic (help) :)
I detect a face and save a picture from a video with a face. But I would like to save not the whole picture, but only the part where there is a face.
What do I need to read to find a solution to my problem? thank
import cv2
from mtcnn import MTCNN
import os
import time
detector = MTCNN()
cap = cv2.VideoCapture("https://linktovideo")
while True:
__, frame = cap.read()
result = detector.detect_faces(frame)
if result != []:
for person in result:
bounding_box = person['box']
keypoints = person['keypoints']
cv2.rectangle(frame,
(bounding_box[0], bounding_box[1]),
(bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]),
(0,155,255),
2)
timestr = time.strftime("%Y%m%d-%H%M%S")
cv2.imwrite("dataset/User." + str(timestr) + '.' + str(count) + ".jpg", frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) &0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
ok. im create noob-code without normal logic :)
import tensorflow as tf
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle
from mtcnn import MTCNN
import cv2
import os
import time
import sys
def draw_faces(filename, result_list):
data = pyplot.imread(filename)
count = 0
for i in range(len(result_list)):
x1, y1, width, height = result_list[i]['box']
x2, y2 = x1 + width, y1 + height
pyplot.subplot(1, len(result_list), i+1)
timestr = time.strftime("%Y%m%d-%H%M%S")
count += 1
newfile = ("dataset/User." + str(timestr) + '.' + str(count) + "_new.jpg")
pyplot.imsave(newfile, data[y1:y2, x1:x2])
detector = MTCNN(post_process=False, device='cuda:0')
count = 0
cap = cv2.VideoCapture("https://videolink")
cap.set(cv2.CAP_PROP_FPS, int(10))
while True:
#Capture frame-by-frame
__, frame = cap.read()
#Use MTCNN to detect faces
result = detector.detect_faces(frame)
if result != []:
for person in result:
count += 1
timestr = time.strftime("%Y%m%d-%H%M%S")
filename = ("dataset/User." + str(timestr) + '.' + str(count) + ".jpg")
cv2.imwrite("dataset/User." + str(timestr) + '.' + str(count) + ".jpg", frame)
print(filename)
draw_faces(filename, result)
cv2.imshow('frame', frame)
k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
if k == 27:
break
elif count >= 30: # Take 30 face sample and stop video
break