A program that automatically determines whether an animation or a photo is entered when a person's image is input [python]

Introduction

I was studying face recognition to do various things with machine learning, but I made it without taking a break. If you put an image of a person, it will judge whether it is an animation or a photo. Since it is based on the face, it may be judged as a photo if you include a realistic picture. I think it can be used by a pc to determine whether it is really close to reality through a live-action realistic picture.

Things to prepare

python3.○ OpenCV for python version Cascade classifier for photographic animation (described later)

development of

It's a simple thing, so let's code it as soon as you get what you need.

Implementation

(Folder) img-Image storage
(Folder) haarcascade-Cascade classifier storage
(File) auto_anime_classifier.py

Please place the above items in the same directory The files to put in the haarcascade folder this time

Source code

auto_anime_classifier.py


import cv2
import numpy as np

if __name__ == '__main__':

    CASCADE_PATH = 'haarcascade/'
    face_count = []

    print('Plz input filename')
    img_name = input('>> ')
    img = cv2.imread('img/'+str(img_name), cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    photo = cv2.CascadeClassifier(CASCADE_PATH + 'haarcascade_frontalface_alt.xml')
    anime = cv2.CascadeClassifier(CASCADE_PATH + 'lbpcascade_animeface.xml')

    face_count.append(len(photo.detectMultiScale(gray, 1.1, 3)))
    face_count.append(len(anime.detectMultiScale(gray, 1.1, 3)))

    if face_count[0] > face_count[1]:
        print('Maybe photo.')
    elif face_count[1] > face_count[0]:
        print('Maybe anime.')
        elif face_count[0] >= 1 and face_count[1] >= 1:
        print('Both attributes.')
    elif face_count[0] == 0 and face_count[1] == 0:
        print('Undeciable.')

Commentary

About OpenCV, I will omit most of it because there are a lot of commentary blogs with wonderful contents. Enter the image in the img folder and read it with imread (). After that, it is grayed.

print('Plz input filename')
img_name = input('>> ')
img = cv2.imread('img/'+str(img_name), cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Create a classifier that reads the cascade classification files for each photo and animation.

photo = cv2.CascadeClassifier(CASCADE_PATH + 'haarcascade_frontalface_alt.xml')
anime = cv2.CascadeClassifier(CASCADE_PATH + 'lbpcascade_animeface.xml')

Store the number read by each classifier in face_count. The presence or absence of this is the criterion.

face_count.append(len(photo.detectMultiScale(gray, 1.1, 3)))
face_count.append(len(anime.detectMultiScale(gray, 1.1, 3)))

Finally, the judgment part. Compare whether it is simply recognized or not.

if face_count[0] > face_count[1]:
   print('Maybe photo.')
elif face_count[1] > face_count[0]:
   print('Maybe anime.')
elif face_count[0] >= 1 and face_count[1] >= 1:
   print('Both attributes.')
elif face_count[0] == 0 and face_count[1] == 0:
   print('Undeciable.')

Closeness to reality Maybe photo (probably a live-action film) → Both attributes (either one works) → Maybe anime (maybe a picture) → Undeciable (I don't know.

play

Let's play the explanation moderately.

Use normally

beatles.jpeg The Beatles. It is cool~

Plz input filename
>> beatles.jpeg
Maybe photo.

It looks like a photo. That's right.

aoba.jpeg The main character of NEW GAME. cute~

Plz input filename
>> aoba.jpeg
Maybe anime.

It seems to be an anime. Well, you can see it if you look at it.

Play with subtle images

Even if I did it normally, nothing was interesting, so let's put in a video that I do not know which one the program shows.

seeu.png This is Mr. Layer SeeU from China. The image is a quote from his Twitter account.

Plz input filename
>> seeu.png
Maybe anime.

It seems to be an anime. From a human perspective, it doesn't look like a picture. Does image processing mean anything?

kuroisabaku.png It is a character created by MMORPG called Black Desert. A type of game that takes hours to characterize.

Plz input filename
>> kuroisabaku.png
Both attributes.

It seems that you can go either way. Except for the big black eyes and the small nose, I don't think it feels like a picture.

honey.jpeg It is a male character made in a game called Honey Select. You can make illusion characters without any discomfort even for characters other than young people.

Plz input filename
>> honey.png
Maybe photo.

It seems to be a photo. I wonder if the shape of the nose is an important turning point. By the way, the male character made with PSO2 was treated as a picture.

orient.jpg A doll from Orient Industry. It's very real.

Plz input filename
>> orient.jpg
Both attributes.

There was an anime element. I wonder why ...

At the end

The program this time was just about whether each judge could recognize the face. If it were to be improved, I felt that it was necessary to output detailed judgment elements from each judgment device and create parameters as numerical values. You have to study cascading classification. It's been a long time, but thank you for watching so far! !!

Recommended Posts

A program that automatically determines whether an animation or a photo is entered when a person's image is input [python]
A program that determines whether a number entered in Python is a prime number
A python program that resizes a video and turns it into an image
There is a pattern that the program did not stop when using Python threading
A program that automatically resizes the iOS app icon to the required image size in Python
[Python] Execution time when a function is entered in a dictionary value
Cut out an image with python
Automatically generate collage from image list
Automatically generate Python Docstring Comment in Emacs
A program that automatically determines whether an animation or a photo is entered when a person's image is input [python]
Try to generate an image with aliasing
How to crop an image with Python + OpenCV
Try to automatically generate Python documents with Sphinx
Generate an insert statement from CSV with Python.
Create an image with characters in python (Japanese)
[Blender x Python] How to make an animation
Post an article with an image to WordPress with Python
python image processing
Make each PowerPoint page an image file in Python
Create an image file using PIL (Python Imaging Library).
I tried to automatically generate a password with Python3
Save screenshot of [Python] [Windows] screen as an image
How to create an image uploader in Bottle (Python)
I made an action to automatically format python code
When writing a program in Python
A Python script that automatically collects typical images using bing image search
Basic tech that easily determines whether the value is "yes" or "no"
Raise an exception or log when a Django template variable is invalid
[Python3] Code that can be used when you want to cut out an image in a specific size