Added headshot cropper project

This commit is contained in:
2017-11-29 09:08:14 -08:00
parent a7431bdb25
commit 9e3c4cb41d
5 changed files with 33357 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
import numpy as np
import os
import cv2
extra_dimensions_percent = 0.3
show_window = False
draw_face_box = False
face_cascade = cv2.CascadeClassifier('xml/haarcascade_frontalface_default.xml')
def cropper(files):
for current_file in files:
img = cv2.imread('inputs/' + current_file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
if draw_face_box:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
(x, y, w, h) = faces[0]
max_dim = max(w, h)
extra_dimensions = int(max_dim * extra_dimensions_percent)
headshot = img[y-extra_dimensions:y+max_dim+extra_dimensions, x-extra_dimensions:x+max_dim+extra_dimensions]
height, width = headshot.shape[:2]
cv2.imwrite("outputs/" + current_file, headshot)
if show_window:
cv2.imshow('img', cv2.resize(headshot, (width // 4, height //4)))
cv2.waitKey(0)
cv2.destroyAllWindows()
print "Cropped %s" % current_file
if __name__ == '__main__':
print "Started Cropper"
cropper(os.listdir("inputs"))
print "Cropping Complete"