windows8.1 python2.7 opencv3
horikita_hasimoto | --- joyu (image folder taken from the net) | |---hasimoto.jpg | |---horikita.jpg | --- joyu_face (face image folder extracted by program) | --- haarcascade_frontalface_alt.xml (cascade file) | --- tashite_nidewaru.py (a program that divides by 2) | --- kao_toridasi.py (program to extract face)
I will take images of Maki Horikita and Kanna Hashimoto from the net. I used the following image.
kao_toridasi.py
# -*- coding: utf-8 -*-
import cv2
import os
import glob
#Substitution of the folder path where the program is located
current_dir = os.getcwd()
#List all images from joyu folder
joyu_list = glob.glob(current_dir + "\\joyu\\*")
#Cascade file
cascade_path = "haarcascade_frontalface_alt.xml"
for joyu in joyu_list:
#File reading
image = cv2.imread(joyu)
if(image is None):
pass
continue
#Grayscale conversion
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Acquire the features of the cascade classifier
cascade = cv2.CascadeClassifier(cascade_path)
#Perform face recognition
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.2, minNeighbors=2, minSize=(10, 10))
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]
resize_image = cv2.resize(dst,(256,256))
new_image_path = current_dir + '\\joyu_face\\' + str(i) + '.jpg'
cv2.imwrite(new_image_path, resize_image)
i += 1
When you run the above program, the face image will be saved in joyu_face, so Please change the image name to horikita.jpg and hasimoto.jpg.
tashite_nidewaru.py
# -*- coding: utf-8 -*-
import cv2
import os
current_dir = os.getcwd()
image_path = current_dir + "\\joyu_face\\"
#File reading
horikita = cv2.imread(image_path + "\\horikita.jpg ")
hasimoto = cv2.imread(image_path + "\\hasimoto.jpg ")
#Grayscale conversion
horikita_gray = cv2.cvtColor(horikita, cv2.COLOR_BGR2GRAY)
hasimoto_gray = cv2.cvtColor(hasimoto, cv2.COLOR_BGR2GRAY)
#In the default state, the image looks pretty creepy, so
#I tried 4 kinds of colors
for i in xrange(len(horikita_gray)):
for j in xrange(len(horikita_gray[0])):
if horikita_gray[i][j] >= 192:
horikita_gray[i][j] = 192
elif horikita_gray[i][j] >= 128:
horikita_gray[i][j] = 128
elif horikita_gray[i][j] >= 64:
horikita_gray[i][j] = 64
else:
horikita_gray[i][j] = 0
for i in xrange(len(hasimoto_gray)):
for j in xrange(len(hasimoto_gray[0])):
if hasimoto_gray[i][j] >= 192:
hasimoto_gray[i][j] = 192
elif hasimoto_gray[i][j] >= 128:
hasimoto_gray[i][j] = 128
elif hasimoto_gray[i][j] >= 64:
hasimoto_gray[i][j] = 64
else:
hasimoto_gray[i][j] = 0
horikita_hasimoto = horikita_gray / 2 + hasimoto_gray / 2
for i in xrange(len(horikita_hasimoto)):
for j in xrange(len(horikita_hasimoto[0])):
if horikita_hasimoto[i][j] >= 192:
horikita_hasimoto[i][j] = 192
elif horikita_hasimoto[i][j] >= 128:
horikita_hasimoto[i][j] = 128
elif horikita_hasimoto[i][j] >= 64:
horikita_hasimoto[i][j] = 64
else:
horikita_hasimoto[i][j] = 0
#Save image
new_image_path = current_dir + '\\horikita_hasimoto.jpg'
cv2.imwrite(new_image_path, horikita_hasimoto)
new_image_path = current_dir + '\\horikita.jpg'
cv2.imwrite(new_image_path, horikita_gray)
new_image_path = current_dir + '\\hasimoto.jpg'
cv2.imwrite(new_image_path, hasimoto_gray)
Image added and divided by 2
I tried it in color first, but it turned out to be creepy. However, I don't think it's a beautiful woman even in black and white ... (; 1_1) There are likely to be many improvements.
Recommended Posts