207 lines
3.9 KiB
Python
207 lines
3.9 KiB
Python
|
|
|
|
|
|
|
|
import cv2, sys, glob, os
|
|
|
|
cascPath = "haarcascade_frontalface_default.xml"
|
|
|
|
# Create the haar cascade
|
|
#faceCascade = cv2.CascadeClassifier(cascPath)
|
|
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
|
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
|
|
|
|
|
|
folder = "cache/picsId/Pending/test/"
|
|
outfolder = "cache/picsId/Pending/test3/"
|
|
files=glob.glob(folder + "*.jpg")
|
|
i = 0
|
|
|
|
for file in files:
|
|
|
|
# Read the image
|
|
fn = file.split("/")[-1]
|
|
print(file)
|
|
print(fn)
|
|
|
|
image = cv2.imread(file)
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
img_size = image.shape
|
|
ww = img_size[0]
|
|
hh = img_size[1]
|
|
|
|
print("Image size: " + str(img_size))
|
|
|
|
|
|
|
|
# Detect faces in the image
|
|
faces = faceCascade.detectMultiScale(gray, minNeighbors=5, minSize=(70, 70))
|
|
"""
|
|
gray,
|
|
scaleFactor=1.1,
|
|
minNeighbors=5,
|
|
minSize=(30, 30),
|
|
flags = cv2.CASCADE_SCALE_IMAGE )
|
|
"""
|
|
|
|
print("Found %d faces!" % len(faces))
|
|
if len(faces)==0: exit()
|
|
|
|
# Crop Padding
|
|
left = 10
|
|
right = 10
|
|
top = 10
|
|
bottom = 10
|
|
|
|
# Draw a rectangle around the faces
|
|
j = 0
|
|
for (x, y, w, h) in faces:
|
|
k = j
|
|
if k == 0: k = ''
|
|
else: k = str(k) + '_'
|
|
|
|
# Dubugging boxes
|
|
# cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
|
|
|
new_x1 = x-left
|
|
new_y1 = y-top
|
|
|
|
new_x2 = x+w+right
|
|
new_y2 = y+h+bottom
|
|
|
|
x1 = max(new_x1, 0)
|
|
y1 = max(new_y1, 0)
|
|
|
|
x2 = min(new_x2, ww)
|
|
y2 = min(new_y2, hh)
|
|
|
|
xx1 = min(x1,x2)
|
|
xx2 = max(x1,x2)
|
|
|
|
yy1 = min(y1,y2)
|
|
yy2 = max(y1,y2)
|
|
|
|
print(x, y, w, h)
|
|
print(ww, hh)
|
|
|
|
print(xx1,xx2,yy1,yy2)
|
|
|
|
#image = image[y-top:y+h+bottom, x-left:x+w+right]
|
|
write_image = image[yy1:yy2, xx1:xx2]
|
|
|
|
print("Writing: " + outfolder + k + fn)
|
|
try:
|
|
cv2.imwrite(outfolder + k + fn, write_image)
|
|
except:
|
|
print(" (failed. was image too small?)")
|
|
j += 1
|
|
|
|
|
|
|
|
|
|
|
|
# print ("cropped_{1}{0}".format(str(file),str(x)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# autocrop
|
|
#
|
|
|
|
"""
|
|
from PIL import Image
|
|
from autocrop import Cropper
|
|
|
|
cropper = Cropper()
|
|
|
|
# Get a Numpy array of the cropped image
|
|
cropped_array = cropper.crop('portrait.png')
|
|
|
|
# Save the cropped image with PIL
|
|
cropped_image = Image.fromarray(cropped_array)
|
|
cropped_image.save('cropped.png')
|
|
|
|
|
|
--------------
|
|
|
|
usage: [-h] [-o OUTPUT] [-i INPUT] [-w WIDTH] [-H HEIGHT] [-e EXTENSION] [-v]
|
|
|
|
Automatically crops faces from batches of pictures
|
|
|
|
optional arguments:
|
|
-h, --help
|
|
Show this help message and exit
|
|
-o, --output, -p, --path
|
|
Folder where cropped images will be placed.
|
|
Default: current working directory
|
|
-r, --reject
|
|
Folder where images without detected faces will be placed.
|
|
Default: same as output directory
|
|
-i, --input
|
|
Folder where images to crop are located.
|
|
Default: current working directory
|
|
-w, --width
|
|
Width of cropped files in px. Default=500
|
|
-H, --height
|
|
Height of cropped files in px. Default=500
|
|
--facePercent
|
|
Zoom factor. Percentage of face height to image height.
|
|
-e, --extension
|
|
Enter the image extension which to save at output.
|
|
Default: Your current image extension
|
|
-v, --version
|
|
Show program's version number and exit
|
|
|
|
|
|
|
|
|
|
autocrop -i . -o test4 -w 250 -H 333
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# smartcrop
|
|
#
|
|
#
|
|
# smartcrop -W 1140 -H 400 -i input.jpg -o output.jpg
|
|
#
|
|
#
|
|
|
|
|
|
|
|
|
|
# imagemagick jpeg compress
|
|
#
|
|
# convert -strip -interlace Plane -gaussian-blur 0.05 -quality 60% -adaptive-resize 60% img_original.jpg img_resize.jpg
|
|
#
|
|
#
|
|
# convert image.jpg -define jpeg:extent=150kb result.jpg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|