I want to implement face recognition in TensorFlow. I want to crop a face from an image to collect and normalize face data
Serious memorandum
jupyter notebook OpenCV3.0 Python3.5
Put Lena and haarcascade_frontalface_alt.xml in the same directory as the .py file, or rewrite the path.
trimming.py
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
#Image loading
src_image = cv2.imread('lenna.jpeg')
#Convert to grayscale
dst_image = cv2.cvtColor(src_image,cv2.COLOR_RGB2GRAY)
#Haar-like classifier read
cascade=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
#Store the coordinate information of the face detected by cascading in facerect
#[Top left X coordinate of the rectangle,Upper left Y coordinate of the rectangle,The length of the face image in the X-axis direction,The length of the face image in the Y-axis direction]I'm worried if it's here
facerect = cascade.detectMultiScale(dst_image, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))
#When one or more faces are detected
if len(facerect) > 0:
for rect in facerect:
dst_image = dst_image[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2]]
#When no face is detected
else:
print("no face")
#When this is done, it becomes grayscale. Mystery
dst_image = cv2.cvtColor(dst_image,cv2.COLOR_GRAY2RGB)
plt.imshow(dst_image)
When displaying a color image read by imread () in grayscale with imshow (), It seems that you have to change from RGB to gray with cvtColor and then from gray to RGB with cvtColor again. Perhaps
I will add this area as soon as I understand it.
You can't save this, right? That's why I made a mechanism to trim and save. Say goodbye to jupyter I'm working on PyCharm. pycharm convenient
trimming.py
import cv2
cascade_path = "./haarcascades/haarcascade_frontalface_alt.xml"
origin_image_path = "Image folder path"
dir_path = "Image save destination path"
i = 0
for line in open('Text file path','r'):
line = line.rstrip()
print(line)
image = cv2.imread(origin_image_path+line,0)
if image is None:
print('Not open : ',line)
quit()
cascade = cv2.CascadeClassifier(cascade_path)
facerect = cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=1, minSize=(10, 10))
if len(facerect) > 0:
for rect in facerect:
#Cut out only the face and save
x = rect[0]
y = rect[1]
width = rect[2]
height = rect[3]
dst = image[y:y + height, x:x + width]
save_path = dir_path + '/' + 'image(' + str(i) + ')' + '.jpg'
#Saving recognition results
cv2.imwrite(save_path, dst)
print("save!")
i += 1
print("Finish")
You can write line by line to the text by doing $ ls> hoge.txt
in the folder containing the image.
While reading the name of the written image, open it, trim it, and save it.
rstrip () The image cannot be read for some reason unless it is bitten. .. ..
But isn't this something like resizing the image? (Continue)
Cut out a part of the statue and save http://clngn.hatenablog.com/entry/20120113/1326442326 Play with Python -part1- (Face recognition with OpenCV) http://www.takunoko.com/blog/python%E3%81%A7%E9%81%8A%E3%82%93%E3%81%A7%E3%81%BF%E3%82%8B-part1-opencv%E3%81%A7%E9%A1%94%E8%AA%8D%E8%AD%98/
Recommended Posts