-
Notifications
You must be signed in to change notification settings - Fork 37
/
crop_image2.py
132 lines (100 loc) · 3.94 KB
/
crop_image2.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 24 11:36:01 2021
@author: Xinya
"""
import os
import glob
import time
import numpy as np
import csv
import cv2
import dlib
from skimage import transform as tf
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('data_preprocess/shape_predictor_68_face_landmarks.dat')
import imageio
def save(path, frames, format):
if format == '.mp4':
imageio.mimsave(path, frames)
elif format == '.png':
if not os.path.exists(path):
os.makedirs(path)
for j, frame in enumerate(frames):
cv2.imwrite(path+'/'+str(j)+'.png',frame)
# imageio.imsave(os.path.join(path, str(j) + '.png'), frames[j])
else:
print ("Unknown format %s" % format)
exit()
def crop_image(image_path, out_path):
template = np.load('data_preprocess/M003_template.npy')
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1) #detect human face
if len(rects) != 1:
return 0
for (j, rect) in enumerate(rects):
shape = predictor(gray, rect) #detect 68 points
shape = shape_to_np(shape)
pts2 = np.float32(template[:47,:])
# pts2 = np.float32(template[17:35,:])
# pts1 = np.vstack((landmark[27:36,:], landmark[39,:],landmark[42,:],landmark[45,:]))
pts1 = np.float32(shape[:47,:]) #eye and nose
# pts1 = np.float32(landmark[17:35,:])
tform = tf.SimilarityTransform()
tform.estimate( pts2, pts1) #Set the transformation matrix with the explicit parameters.
dst = tf.warp(image, tform, output_shape=(256, 256))
dst = np.array(dst * 255, dtype=np.uint8)
cv2.imwrite(out_path,dst)
def shape_to_np(shape, dtype="int"):
# initialize the list of (x, y)-coordinates
coords = np.zeros((shape.num_parts, 2), dtype=dtype)
# loop over all facial landmarks and convert them
# to a 2-tuple of (x, y)-coordinates
for i in range(0, shape.num_parts):
coords[i] = (shape.part(i).x, shape.part(i).y)
# return the list of (x, y)-coordinates
return coords
def crop_image_tem(video_path, out_path):
image_all = []
videoCapture = cv2.VideoCapture(video_path)
success, frame = videoCapture.read()
n = 0
while success :
image_all.append(frame)
n = n + 1
success, frame = videoCapture.read()
if len(image_all)!=0 :
template = np.load('./M003_template.npy')
image=image_all[0]
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1) #detect human face
if len(rects) != 1:
return 0
for (j, rect) in enumerate(rects):
shape = predictor(gray, rect) #detect 68 points
shape = shape_to_np(shape)
pts2 = np.float32(template[:47,:])
# pts2 = np.float32(template[17:35,:])
# pts1 = np.vstack((landmark[27:36,:], landmark[39,:],landmark[42,:],landmark[45,:]))
pts1 = np.float32(shape[:47,:]) #eye and nose
# pts1 = np.float32(landmark[17:35,:])
tform = tf.SimilarityTransform()
tform.estimate( pts2, pts1) #Set the transformation matrix with the explicit parameters.
out = []
for i in range(len(image_all)):
image = image_all[i]
dst = tf.warp(image, tform, output_shape=(256, 256))
dst = np.array(dst * 255, dtype=np.uint8)
out.append(dst)
if not os.path.exists(out_path):
os.makedirs(out_path)
save(out_path,out,'.png')
def proc_audio(src_mouth_path, dst_audio_path):
audio_command = 'ffmpeg -i \"{}\" -loglevel error -y -f wav -acodec pcm_s16le ' \
'-ar 16000 \"{}\"'.format(src_mouth_path, dst_audio_path)
os.system(audio_command)
if __name__ == "__main__":
image_path ='test_data/test_chinese/person3.png'
save_path = 'test_data/test_chinese/person3_crop.png'
crop_image(image_path, save_path)