-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalCombine2.py
150 lines (123 loc) · 6.13 KB
/
finalCombine2.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#from PIL import Image
from PIL import Image, ImageOps
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM
import os, sys
import cv2
import sys
import math
import numpy as np
import glob
from progressbar import ProgressBar
pbar = ProgressBar()
def resizeImage(infile, output_dir="resized"):
outfile = os.path.splitext(infile)[0]+""
extension = os.path.splitext(infile)[1]
if (infile != outfile and infile!= "croppedpicpp.PNG" and extension != ".svg" and "SVG" not in os.path.splitext(infile)[0]):
try :
#opening image in numpy
im = Image.open(infile)
imageW,imageH = im.size
#declaring CascadeClassifier:
cascPath = "haarcascade_frontalface_alt2.xml"
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
#opening image in cv2
imagePath = infile
imageCV = cv2.imread(imagePath)
#finding if there is a face in the image
#converting image to gray image to detect faces
gray = cv2.cvtColor(imageCV, cv2.COLOR_BGR2GRAY)
side = math.sqrt(imageCV.size)
minlen = int(side / 20)
maxlen = int(side / 2)
foundFace = ""
faces = faceCascade.detectMultiScale(gray,1.3, 4, 0, (minlen, minlen), (maxlen, maxlen))
#print ("Found {0} faces!".format(len(faces)))
#checking how many faces found:
#if 0, do regular crop, if more than one found, change parameters to get only one faceCascade
#if one face found, call def foundFace to crop w.r.t to face
#if (len(faces)) == 1 :
# foundFace = "yes"
#elif (len(faces)) > 1 :
# foundFace = "moreFaces"
# faces = faceCascade.detectMultiScale(gray,1.4, 5, 0, (minlen, minlen), (maxlen, maxlen))
#elif (len(faces)) == 0 :
# foundFace = "no"
#do regular cropping since no face was found
#........
def detectface(imagefile):
imagefile = imageCV
for (x, y, w, h) in faces:
#these parametersare for original pic to get ration of sides Left around the face.
#cv2.rectangle(imageCV, (x, y), (x+w, y+h), (0, 255, 0), 2)
#print((x,y),(x+w, y),(x+w, y+h), (x, y+h))
#cv2.imshow("face Image", imageCV)
Lw = x
Rw = imageW-(x+w)
Th = y
Bh = imageH-(y+h)
ThRatio = Th/(Th+Bh)
BhRatio = Bh/(Th+Bh)
LwRatio = Lw/(Lw+Rw)
RwRatio = Rw/(Lw+Rw)
nTh, nBh, nRw, nLw = 0, 0, 0, 0
if(imageH<imageW):
nTh = ThRatio * (imageH - h)
nBh = BhRatio * (imageH - h)
nRw = RwRatio * (imageH - w)
nLw = LwRatio * (imageH - w)
else:
nTh = ThRatio * (imageW - h)
nBh = BhRatio * (imageW - h)
nRw = RwRatio * (imageW - w)
nLw = LwRatio * (imageW - w)
nTh = int(round(nTh))
nBh = int(round(nBh))
nLw = int(round(nLw))
nRw = int(round(nRw))
roi = imageCV[ y - nTh : y + h + nBh , x - nLw : x + w + nRw ]
cv2.imwrite( "croppedpicpp.PNG", roi );
#cv2.imshow("Cropped Imagepp", roi)
#cv2.waitKey(0)
im2 = Image.open("croppedpicpp.PNG")
im2 = ImageOps.fit(im2, (300,300), method=0, bleed=0.0, centering=(0.5, 0.5))
#im2.save("finalcrop"+ imagePath +"finalpp.PNG")
#path = "C:/playground/images/main/resized"
path = output_dir
filename = os.path.join(path, outfile)
im2.save(filename +"" + ".PNG")
#............................End of def detectface().............................
if (len(faces)) == 0:
size=(300,300)
if imageH <= 100 and imageW <=100:
im = ImageOps.fit(im, size, method=0, bleed=0.0, centering=(0.5, 0.5))
else:
im.thumbnail(size, Image.ANTIALIAS)
background = Image.new('RGBA', size, (255, 255, 255, 0))
background2 = Image.new('RGBA', size, (255, 255, 255, 0))
background.paste(
im, (int((size[0] - im.size[0]) / 2), int((size[1] - im.size[1]) / 2))
)
background2.paste(background, (0, 0), background)
img_with_border = ImageOps.expand(background2,border=0,fill='white')
#path = "C:/playground/images/main/resized"
path = output_dir
filename = os.path.join(path, outfile)
img_with_border.save(filename + outfile + ".PNG")
elif (len(faces)) == 1:
detectface(imageCV)
elif (len(faces)) > 1:
# increase the parameter value, scalescaleFactor=1.4,minNeighbors=5,
faces = faceCascade.detectMultiScale(gray,1.4, 5, 0, (minlen, minlen), (maxlen, maxlen))
detectface(imageCV)
#........................
except IOError:
print ("cannot reduce image for ", infile)
if __name__=="__main__":
output_dir = "resized"
dir = os.getcwd()
if not os.path.exists(os.path.join(dir,output_dir)):
os.mkdir(output_dir)
for file in pbar(os.listdir(dir)):
resizeImage(file,output_dir)