I tried to automate the face hiding work of the coordination image for wear

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.

Source code

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()

Execution result

data.jpeg The coordination of the original image looks like this. (Use a lot of photos ...)

data20200129202956.png After execution, the icon fits perfectly! !! !! The troublesome work can be done in no time! !! Don't worry about the icon image being suitable ...

Finally

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

I tried to automate the face hiding work of the coordination image for wear
I tried to correct the keystone of the image
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to automate the watering of the planter with Raspberry Pi
I tried to build the SD boot image of LicheePi Nano
I tried to improve the efficiency of daily work with Python
I tried to get the batting results of Hachinai using image processing
I tried to touch the API of ebay
I tried using the image filter of OpenCV
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
[Python] I tried to judge the member image of the idol group using Keras
I tried to automate the 100 yen deposit of Rakuten horse racing (python / selenium)
I tried to process and transform the image and expand the data for machine learning
I tried to summarize the basic form of GPLVM
I tried the MNIST tutorial for beginners of tensorflow.
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to classify the voices of voice actors
I tried to compress the image using machine learning
I tried to summarize the string operations of Python
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to sort out the objects from the image of the steak set meal-④ Clustering
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried porting the code written for TensorFlow to Theano
[Horse Racing] I tried to quantify the strength of racehorses
I tried "gamma correction" of the image with Python + OpenCV
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
I tried face recognition of the laughter problem using Keras.
[Python] I tried to visualize the follow relationship of Twitter
I tried to fight the Local Minimum of Goldstein-Price Function
I tried to dig deeper about safety while calculating the stochastic finality of Proof of Work
I tried to sort out the objects from the image of the steak set meal-① Object detection
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
I forgot to evaluate the performance because of the habit of skipping "I tried to aim for the fastest FizzBuzz"
I tried to move the ball
I tried to estimate the interval.
[Linux] I tried to summarize the command of resource confirmation system
I tried to get the index of the list using the enumerate function
I tried to process the image in "sketch style" with OpenCV
How to use machine learning for work? 01_ Understand the purpose of machine learning
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
I tried to cut out a still image from the video
I tried to summarize the frequently used implementation method of pytest-mock
I tried to visualize the common condition of VTuber channel viewers
I tried to sort out the objects from the image of the steak set meal-② Overlap number sorting
I tried the asynchronous server of Django 3.0
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried moving the image to the specified folder by right-clicking and left-clicking
I tried to summarize the umask command
I tried to visualize the age group and rate distribution of Atcoder
I tried tensorflow for the first time
I tried transcribing the news of the example business integration to Amazon Transcribe
I tried to make a face diagnosis AI for a female professional golfer ①
I tried to recognize the wake word
zoom I tried to quantify the degree of excitement of the story at the meeting
I tried to take the difference of Config before and after work with pyATS / Genie self-made script
I tried to make a face diagnosis AI for a female professional golfer ②
I tried to sort out the objects from the image of the steak set meal --③ Similar image Heat map detection