――You can execute it without knowing the characteristics of OpenCV so much, but the key is whether OpenCV can be read normally.
face_detect.py
# -*- coding:utf-8 -*-
import cv2
import numpy as np
import os.path
from pathlib import Path
#Directory with the image data collected earlier
input_data_path = '{Directory path}'
#Directory to save the cropped image
save_path = '{Directory path}'
#Create a destination directory
Path(save_path).mkdir(parents=True, exist_ok=True)
#OpenCV default classifier path.(https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.Use xml file)
cascade_path = '/usr/local/Cellar/opencv3/3.2.0/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascade_path)
#Number of images collected(Optional change)
image_count = 1100
#Number of successful face detections(Specify 0 by default)
face_detect_count = 0
#When a face is detected from the collected image data, cut it out and save it.
for i in range(image_count):
file_name = input_data_path + str(i) + ".jpg "
if os.path.isfile(file_name):
img = cv2.imread(file_name, cv2.IMREAD_COLOR)
assert img is not None, Path(file_name).absolute()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = faceCascade.detectMultiScale(gray, 1.1, 3)
if len(face) > 0:
for rect in face:
#Surround the face recognition part with a red line and save(I don't need this part now)
# cv2.rectangle(img, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0,255), thickness=1)
# cv2.imwrite('detected.jpg', img)
x = rect[0]
y = rect[1]
w = rect[2]
h = rect[3]
cv2.imwrite(save_path + '{file name}' + str(face_detect_count) + '.jpg', img[y:y+h, x:x+w])
face_detect_count = face_detect_count + 1
else:
print('image' + str(i) + ':No Face')
else:
print('image' + str(i) + ':No File')
python face_detect.py
--find and find haarcascade_frontalface_default.xml. --There are some files whose images cannot be read well. There are quite a few. --There are some files that you can read the image but cannot extract the face well, so please delete it by hand.
-I made a Dir en gray face classifier using TensorFlow --(1) Introduction -I made a face classifier for Dir en gray using TensorFlow-② Environment construction -I made a face classifier for Dir en gray using TensorFlow-③ Image collection -I made a face classifier for Dir en gray using TensorFlow-④ Face extraction -I made a face classifier for Dir en gray using TensorFlow-⑤ Learning data preparation -I made a Dir en gray face classifier using TensorFlow-⑥ Learning program -I made a face classifier for Dir en gray using TensorFlow-⑦ Learning model -I made a face classifier for Dir en gray using TensorFlow-⑧ Learning execution -I made a Dir en gray face classifier using TensorFlow --⑨ Data visualization -I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test -I made a Dir en gray face classifier using TensorFlow-⑪ Web release preparation -I made a Dir en gray face classifier using TensorFlow --⑫ Web release -I tried to make a Dir en gray face classifier using TensorFlow --⑬ Playing (final)
Recommended Posts