-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pass_face_video_path.py
92 lines (79 loc) · 3.09 KB
/
Pass_face_video_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!pip install opencv-python==3.3.0.9
#!pip install cv2
#!python -V
#!python -m pip install --upgrade pip
#!pip install cv2
#!pip install opencv-python
#!pip install mtcnn
#!pip install tensorflow
#!pip install tensorflow==2.0.0-beta0
#!pip install -U numpy
import cv2
import mtcnn
from mtcnn.mtcnn import MTCNN
import numpy as np
import pandas as pd
import math
import os
import sys
import h5py
detector = MTCNN()
#Function for passing
def find_processed_faces(v_path):
n_frames = 11
video = cv2.VideoCapture(v_path)
print(video.isOpened())
framerate = video.get(5)
count=1
frameindex=1
try:
while (video.isOpened()):
frameId = video.get(1)
#print()
success, image = video.read()
if( image is not None ):
result = detector.detect_faces(image)
if(len(result)>1):
max_area = 0
max_idx = -1
idx=0
for i in range(len(result)):
bounding_box = result[i]['box']
area=abs((bounding_box[1]-(bounding_box[1] + bounding_box[3]))*(bounding_box[0]-(bounding_box[0]+bounding_box[2])))
#print(area)
if area>max_area:
max_area=area
max_idx=i
#idx=idx+1
#print(max_area)
#print(max_idx)
bounding_box = result[max_idx]['box']
crop_img = image[(bounding_box[1]-28):(bounding_box[1]-28 + bounding_box[3]+56), (bounding_box[0]-28):(bounding_box[0]-28+bounding_box[2]+56)]
else:
bounding_box = result[0]['box']
keypoints = result[0]['keypoints']
##Creating rectangle
cv2.rectangle(image,
(bounding_box[0]-25, bounding_box[1]-25),
(bounding_box[0]-25+bounding_box[2]+50, bounding_box[1]-25 + bounding_box[3]+50),
(0,155,255),2)
##Cropping the images
crop_img = image[(bounding_box[1]-28):(bounding_box[1]-28 + bounding_box[3]+56), (bounding_box[0]-28):(bounding_box[0]-28+bounding_box[2]+56)]
# print(crop_img.size())
crop_img = cv2.resize(crop_img, (152,179), interpolation = cv2.INTER_AREA)
if (success != True):
print("Break1")
break
if (frameId % math.floor(framerate) == 0):
destn_path="C:\\Users\\Administrator\\Desktop\\hackathon\\faces\\"+v_path.split("\\")[-1].replace(".mp4","")
os.mkdir(destn_path)
filename=destn_path+"\\image_" + str(int(frameId / math.floor(framerate))+1) + ".jpg"
cv2.imwrite(filename,crop_img)
frameindex=frameindex+1
count=count+1
if frameindex==n_frames:
print("Break2")
break
return (True,filename)
except:
return (False,"")