Do you all know Wear? WEAR is a fashion coordination site and one of our services. In short, it is an SNS that shares each person's fashion coordination.
In such a wear, I don't know the reason, but it seems that it is popular to hide the face with an icon etc. and post it. Is it because people without a face can see the coordination objectively?
However, the work of hiding the face is unexpectedly troublesome, and I wish I could automate this ... so I implemented a program for automatic icon placement.
import os.path
import datetime
import cv2
import time
#constant
#data folder
DATA_PATH = "data"
#result folder
RESULT_PATH = "result"
#Cascade path
CASCADE_PATH = "haarcascade_frontalface_default.xml"
def main():
#Get the current directory
current_path = os.getcwd()
icon_image_path = os.path.join(current_path, "icon.png ")
icon_image = cv2.imread(cv2.samples.findFile(icon_image_path))
#get the data directory
data_path = os.path.join(current_path, DATA_PATH)
#get the data directory
result_path = os.path.join(current_path, RESULT_PATH)
#Get a list of files directly under the directory
data_list = os.listdir(data_path)
for file in data_list:
#Processing time measurement timer start
start = time.time()
#Get file extension
file_name, ext = os.path.splitext(file)
#When the extension is png or jpeg
if ext == u'.png' or ext == u'.jpg' or ext == u'.jpeg':
#Load image
input_path = os.path.join(data_path, file)
#Input image storage
image = cv2.imread(cv2.samples.findFile(input_path))
#Grayscale conversion
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Acquire the features of the cascade classifier
cascade = cv2.CascadeClassifier(CASCADE_PATH)
face_rect = cascade.detectMultiScale(image_gray, 1.1, 3, 5)
if 0 < len(face_rect):
print("Face discovery")
for x, y, w, h in face_rect:
#Hide face
image = put_icon(image, (x, y, x + w, y + h), icon_image)
else:
print("Face not found")
output_path = os.path.join(result_path, create_time_path(file_name, ".png "))
#Save image
cv2.imwrite(output_path, image)
#Processing time measurement timer stop
t = time.time() - start
print(output_path, ":", t)
#Output a unique file path including time
def create_time_path(file_name, ext):
#Get the current time
time = datetime.datetime.now()
#Create a path
path = file_name + time.strftime("%Y%m%d%H%M%S") + ext
return path
def put_icon(img, rect, icon_image):
#Get the area to cover the icon
x1, y1, x2, y2 = rect
w = x2 - x1
h = y2 - y1
#Overlay the icon on the image
img2 = img.copy()
icon_image = cv2.resize(icon_image, (w, h), cv2.INTER_AREA)
img2[y1:y2, x1:x2] = icon_image
return img2
if __name__ == '__main__':
main()
The coordination of the original image looks like this. (Use a lot of photos ...)
After execution, the icon fits perfectly! !! !! The troublesome work can be done in no time! !! Don't worry about the icon image being suitable ...
Since the accuracy of face recognition is not so good, many icons will be set depending on the photo, so it may be better to use it lightly.
Recommended Posts