-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_1.py
executable file
·85 lines (62 loc) · 2.3 KB
/
run_1.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
# 1º Etapa
# Recorda as faces e codifica em 128 dimensoes
#
# data/video/data - Dados
# data/video/face - imagens
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
import dlib
import face_recognition
import json
from moviepy.editor import VideoFileClip
file_name = 'data/video_2.mp4'
path_data = 'data/video_2'
clip = VideoFileClip(file_name) # can be gif or movie
count_frame = 1
def save_face(file, face_image):
cv2.imwrite('{}/face/{}.jpg'.format(path_data, file), face_image)
def save_json(file, frame, point, face_128_dim):
#face = ",".join([str(i) for i in face_128_dim])
data = {'frame': frame, 'point_1': point[0], 'point_2': point[1],
'filename': file, 'face_encode': face_128_dim.tolist()}
with open('{}/data/{}.json'.format(path_data, file), 'w') as outfile:
json.dump(data, outfile)
# Main
# Frames in Video
for frame in clip.iter_frames(fps=1):
count_face = 1
# shape of image
image_h, image_w, _ = np.shape(frame)
# Converto to Color Image
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Find all the faces in the image
face_locations = face_recognition.face_locations(frame)
print("{}: I found {} face(s) in this photograph.".format(count_frame, len(face_locations)))
# Faces in image
for face_location in face_locations:
# Localize face in image
top, right, bottom, left = face_location
w, h = right - left, bottom - top
xw1 = max(int(left - 0.4 * w), 0)
yw1 = max(int(top - 0.4 * h), 0)
xw2 = min(int(right + 0.4 * w), image_w - 1)
yw2 = min(int(bottom + 0.4 * h), image_h - 1)
# You can access the actual face itself like this:
face_image = frame[yw1:yw2 + 1, xw1:xw2 + 1]
# Point face in the image
point = [[xw1, yw1], [xw2, yw2]]
# File name with frame and face
file = "Frame_{}_Face_{}".format(count_frame, count_face)
# FaceToVec
face_128_dim = face_recognition.face_encodings(face_image)
if len(face_128_dim) > 0:
# Save /face
save_face(file, face_image)
# Save /data
save_json(file, count_frame, point, face_128_dim[0])
count_face = count_face + 1
count_frame = count_frame + 1
# if count_frame > 5:
# break