Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cut face from video #96

Open
prozaklob opened this issue Jul 7, 2020 · 1 comment
Open

cut face from video #96

prozaklob opened this issue Jul 7, 2020 · 1 comment

Comments

@prozaklob
Copy link

prozaklob commented Jul 7, 2020

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()
@prozaklob
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant