-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpytclip.py
108 lines (82 loc) · 2.87 KB
/
pytclip.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
import cv2
from cv2 import resize
from surf import detect_character
import argparse
FACIAL_CASADE_CONF = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"
X = 0
Y = 1
HEIGHT = 3
def detect_face(img):
height, width, depth = img.shape
cascade = cv2.CascadeClassifier(FACIAL_CASADE_CONF)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = cascade.detectMultiScale(img_gray, 1.1, 2, cv2.cv.CV_HAAR_SCALE_IMAGE, (30,30))
if len(rects) == 0:
return -1
height = rects[-1][HEIGHT]
sY = rects[-1][Y] - height / 2
if sY > height / 2:
return -1
else:
return 0 if sY < 0 else sY
def tclip(source_path, dest_path, dest_width, dest_height, debug_mode=False):
img = cv2.imread(source_path)
height, width, depth = img.shape
ratio = 300.0 / width
tmp_size = (int(width * ratio), int(height * ratio))
resize(img, tmp_size)
result = detect_face(img)
if result == -1:
result = detect_character(img)
result = -1 if result == -1 else int(float(result / ratio))
if debug_mode:
print dest_width, width
print dest_height, height
ratio_width = float(dest_width) / width
ratio_height = float(dest_height) / height
if ratio_width > ratio_height:
ratio = ratio_width
else:
ratio = ratio_height
if debug_mode:
print "ratio", ratio
result = -1 if result == -1 else int(result * ratio)
tmp_size = (int(width * ratio), int(height * ratio))
if debug_mode:
print "resize image width", tmp_size[X]
print "resize image height", tmp_size[Y]
dest_img = resize(img, tmp_size)
d_height, d_width, d_depth = dest_img.shape
clip_left = 0
clip_right = 0
if ratio_width > ratio_height:
if result == -1:
clip_top = -((d_height - dest_height) / 2)
clip_bootom = clip_top
else:
if d_height - result >= dest_height:
clip_top = -result;
clip_bottom = -(d_height - result - dest_height)
else:
clip_top = -(d_height - dest_height)
else:
clip_left = -((d_width - dest_width)/2)
clip_right = clip_left
if debug_mode:
print clip_top, clip_bottom
print clip_left, clip_right
print d_width, d_height
print width+clip_right, height+clip_bottom
crop_img = dest_img[clip_left:d_height+clip_bottom, clip_top:d_width+clip_right]
cv2.imwrite(dest_path, crop_img);
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-s")
parser.add_argument("-d")
parser.add_argument("--width", default=300)
parser.add_argument("--height", default=180)
parser.add_argument("-m", default=False)
args = parser.parse_args()
tclip(args.s, args.d, args.width, args.height, args.m)
if __name__ == "__main__":
main()