MacOS Catalina 10.15.4
Python 3.7.6
A library that can perform some image processing such as face detection using a trained model. It is said that dlib is used. https://github.com/ageitgey/
This is a code that uses face_recognition. https://github.com/komiyakomiyakomiya/face_trim
terminal
$ git https://github.com/komiyakomiyakomiya/face_trim.git
opencv-python==4.2.0.34
face-recognition==1.3.0
terminal
$ pip install opencv-python==4.2.0.34 face-recognition==1.3.0
face_trim.py
import os
from pathlib import Path
import subprocess
import sys
import cv2
import face_recognition
from IPython.display import display
from IPython.display import Image
cwd = Path().resolve()
def exec_cmd(cmd):
"""Command execution"""
#Delete any spaces before or after the cmd string->Divide by space and make a list
cmd_split = cmd.strip().split()
#Get standard output with stdout settings
cp = subprocess.run(cmd_split, stdout=subprocess.PIPE)
# cp = subprocess.check_output(cmd_split)
if cp.returncode != 0:
print(f'{cmd_split[0]} faild.', file=sys.stderr)
sys.exit(1)
#Returns if there is standard output
if cp.stdout is not None:
# bytes ->Decode to str
return cp.stdout.decode('utf-8')
def get_face_location(img_path):
"""Get face coordinates"""
img = face_recognition.load_image_file(img_path)
# location = face_recognition.face_locations(img, model='cnn')
location = face_recognition.face_locations(img, model='hog')
print(location)
# [(82, 175, 180, 76)]
top = location[0][0]
right = location[0][1]
bottom = location[0][2]
left = location[0][3]
return top, right, bottom, left
def get_face_location_cli(img_path):
"""CLI tool face_Execute detection to get face coordinates
load_image_file()Use this if the method cannot be used due to a bug"""
#Detects face and Top, Right, Bottom,Command to output the coordinates of Left as standard
cmd_face_detection = f'face_detection {img_path}'
# cmd_face_detection = f'face_detection --model cnn {img_path}'
#Receive standard output
stdout = exec_cmd(cmd_face_detection)
print(stdout)
# /Users/USER_NAME/path/to/dir/input/Zinedine_Zidane_0001.jpg,89,181,192,77
#List separated by commas
stdout_list = stdout.strip().split(',')
top = int(stdout_list[1])
right = int(stdout_list[2])
bottom = int(stdout_list[3])
left = int(stdout_list[4])
return top, right, bottom, left
def display_image(img_path):
"""Display image"""
#Loading images
img = cv2.imread(img_path)
#Get extension
format = os.path.splitext(img_path)[1]
#Extension passed(format)Encoded in the format of
decoded_bytes = cv2.imencode(format, img)[1].tobytes()
print(cv2.imencode(format, img)[1])
# [[255]
# [216]
# [255]
# ...
# [103]
# [255]
# [217]]
# print(decoded_bytes)
# b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x0......
display(Image(data=decoded_bytes))
if __name__ == '__main__':
#Please replace it with your favorite image
file_name = f'Zinedine_Zidane_0001.jpg'
#Where to put the original image
input_path = f'{cwd}/../input'
#Output destination of cropped image
output_path = f'{cwd}/../output'
#Create if there is no output directory
os.makedirs(output_path, exist_ok=True)
#Get face coordinates
top, right, bottom, left = get_face_location(f'{input_path}/{file_name}')
#Reading the original file
img = cv2.imread(f'{input_path}/{file_name}')
#display
display_image(f'{input_path}/{file_name}')
#trimming
img_face = img[top:bottom, left:right]
#Save to output directory
cv2.imwrite(f'{output_path}/{file_name}', img_face)
#display
display_image(f'{output_path}/{file_name}')
if __name__ == '__main__':
#Where to put the original image
input_path = f'{cwd}/../input'
#Output destination of cropped image
output_path = f'{cwd}/../output'
#Create if there is no output directory
os.makedirs(output_path, exist_ok=True)
#Generate path object
path_obj = Path(input_path)
#Pattern matching with glob
files_path = path_obj.glob('*')
#posix conversion
files_path_posix = [file_path.as_posix() for file_path in files_path]
print(files_path_posix)
for file_path in files_path_posix:
#Get file name
file_name = file_path.split('/')[-1]
#Get face coordinates
top, right, bottom, left = get_face_location(file_path)
#Reading the original file
img = cv2.imread(file_path)
#display
display_image(file_path)
#trimming
img_face = img[top:bottom, left:right]
#Save to output directory
cv2.imwrite(f'{output_path}/{file_name}', img_face)
#display
display_image(f'{output_path}/{file_name}')
I sometimes got an error like this.
AttributeError: module 'face_recognition' has no attribute 'load_image_file'
The issue was also standing, but I couldn't solve it even if I tried what was written here. https://github.com/ageitgey/face_recognition/issues/318
When I gave up and tried the brute force method of getting the face coordinates from the standard output of the CLI tool, it was fixed before I knew it.
I wrote an article here. If you do not mind
Thank you for reading until the end! Zidane has no meaning. I happened to be by my side.
Recommended Posts