I added Maki Horikita and Kanna Hashimoto and divided by 2 with python

【environment】

windows8.1  python2.7  opencv3

[Folder structure]

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)

[Preparation]

I will take images of Maki Horikita and Kanna Hashimoto from the net. I used the following image.  horikita.jpg hasimoto.jpg

[Program execution method]

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)

【result】

horikita.jpg hasimoto.jpg

Image added and divided by 2 horikita_hasimoto.jpg

[Impression]

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

I added Maki Horikita and Kanna Hashimoto and divided by 2 with python
I played with PyQt5 and Python3
I tried to automatically collect images of Kanna Hashimoto with Python! !!
I installed and used Numba with Python3.5
Organize data divided by folder with Python
[Python] I introduced Word2Vec and played with it.
I tried Jacobian and partial differential with python
I tried function synthesis and curry with python
I want to handle optimization with python and cplex
3 things I noticed by analyzing twitter followers with python
I created a stacked bar graph with matplotlib in Python and added a data label
I made a LINE BOT with Python and Heroku
I implemented collaborative filtering (recommendation) with redis and python
I tried follow management with Twitter API and Python (easy)
I tried to make GUI tic-tac-toe with Python and Tkinter
This time I learned python III and IV with Prorate
I tried fp-growth with python
Programming with Python and Tkinter
I tried scraping with Python
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
I made blackjack with python!
I compared Java and Python!
python with pyenv and venv
I tried gRPC with Python
I tried scraping with python
I made blackjack with Python.
I made wordcloud with Python.
Works with Python and R
I made a simple circuit with Python (AND, OR, NOR, etc.)
I couldn't use tkinter with python installed by pyenv of anyenv
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
[System trade] I tried playing with Stochastic Oscillator by decomposing with python ♬
I made a Nyanko tweet form with Python, Flask and Heroku
I tried to make a periodical process with Selenium and Python
I tried to easily detect facial landmarks with python and dlib
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
I can't install python3 with pyenv-vertualenv
I tried web scraping with python.
Scraping with Python and Beautiful Soup
I made a fortune with Python.
I sent an SMS with Python
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
I liked the tweet with python. ..
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
I want to debug with Python
I tried running prolog with python 3.8.2.
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
I made a daemon with Python
I tried SMTP communication with Python