mirror of
https://github.com/caperren/project_archives.git
synced 2025-11-08 13:31:14 +00:00
Added headshot cropper project
This commit is contained in:
2
headshot_cropper/README.md
Normal file
2
headshot_cropper/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
This tool will take large images of a single person, and crop out just their face.
|
||||
I used this tool to crop out the headshots for the robotics club leadership, which couldn't be done with fixed coordinates as the profile pictures were not taken with a tripod.
|
||||
41
headshot_cropper/cropper.py
Normal file
41
headshot_cropper/cropper.py
Normal 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"
|
||||
BIN
headshot_cropper/inputs/Corwin.jpg
Normal file
BIN
headshot_cropper/inputs/Corwin.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.3 MiB |
BIN
headshot_cropper/outputs/Corwin.jpg
Normal file
BIN
headshot_cropper/outputs/Corwin.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 466 KiB |
33314
headshot_cropper/xml/haarcascade_frontalface_default.xml
Normal file
33314
headshot_cropper/xml/haarcascade_frontalface_default.xml
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user