[PYTHON] Make a Sato Yohei discriminator using OpenCV and TensorFlow

Introduction

Yohei Sato with me

The timing when I and Yohei Sato should have met for the first time was the 2008 Yahoo Japan Corporation new graduate training. The reason for the phrase "should be" is that the number of new graduates hired by Yahoo Japan Corporation at that time exceeded 250. It was Zara that I had no acquaintance unless I became the same team in the training or the department to which I was assigned was the same.

At the time of joining the company, I was a beginner in programming, and I was only asking "Can I eat pointers?" It is said that Sato volunteered to enter the selection class and overcame the training. And because they were assigned to different places, they passed each other.

After that, from @ doradora09 who entered the department I belonged to as a junior "Sato sponsors a study session called TokyoR, so if you'd like, why don't you join us?" Occasionally, I started to show up at Tokyo R, where I repeatedly passed each other.

In 2015, after changing jobs, I was in a neat state after restructuring. When I contacted @ doradora09 on a whim, "I joined Datum Studio, where Sato is the vice president, so why don't you come and listen to me?" I've attended Datum Studio seminars in the past and thought it was an interesting company.

It was supposed to be just a story, but it became a drinking party, and I was able to connect with the vice president, Sato. And, as an aside, he was hit by a calculator on the spot and decided to join the company until now.

What is Yohei Sato?

Yohei Sato is one of Japan's leading data scientists, not to mention At the same time, he is the vice president of Datum Studio Co., Ltd. We should aim for Yohei Sato.

Creation of Yohei Sato detector

And if you are a data scientist, you need to extract and understand the features of Yohei Sato. I think we need to make an effort to get closer to that feature.

So, this time, I extracted what makes Sato Yohei from the images collected on the net and inside Datum Studio. I decided to create a Sato Yohei detector.

Main subject

Technology used and processing flow

Use OpenCV for image processing Tensorflow, a deep learning library, is used to extract features. For the language, I chose python3, which can handle these comprehensively.

The processing flow is as follows.

  1. Image collection (human power)
  2. Image processing (grayscale using OpenCV, face recognition, cut out face part, convert to 64 * 64 image)
  3. Classify images into Yohei Sato and others
  4. Creating a learner using tensorflow
  5. Evaluation

environment

I will rent a good instance on AWS and use Amazon Linux as the OS.

When developing with python3, we introduced a virtual environment using anaconda3.

conda create -n yohei python=3.5 numpy scipy pandas jupyter scikit-learn matplotlib
source activate yohei
conda install tensorflow
conda install -c menpo opencv3=3.1.0

I encountered a problem that OpenCV does not work on Amazon Linux, so I referred to the following. http://amazarashi.me/archives/855

Image collection

We are looking for images of Yohei Sato from the front on the Internet and Datum Studio.

advent_2016_03.PNG

Image processing

I used OpenCV for image processing. All you have to do is run the code below.

import cv2, matplotlib
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display, Image
import os

def face_detector(img_path, i):
    #Image loading
    img = cv2.imread(img_path)
    
    #Grayscale
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    #Face detection
    img_face = faceCascade.detectMultiScale(img_gray, 1.1, 3)

    #If face detection is possible, proceed to the following processing
    if len(img_face) > 0:
        for rect in img_face:
            x = rect[0]
            y = rect[1]
            width = rect[2]
            height = rect[3]
            #Cutout
            dst = img_gray[y:y+height, x:x+width]
            # 64*Resized to 64
            dst_resized = cv2.resize(dst,(64, 64))
            new_image_path = 'Output directory' + str(i) + '.png'
            cv2.imwrite(new_image_path, dst_resized)
            i += 1
        
    return(i)

i = 0

for img_file in file_list:
    full_path_name = 'Image storage'+ img_file
    i = face_detector(full_path_name, i)

In this way, you can create a face image that has been grayscaled and cut out to 64 * 64.

advent_2016_01.PNG

Classify images into Yohei Sato and others

Images are manually classified into Yohei Sato and others. Also, prepare a csv file with the following correct / incorrect judgment label. (1 is Yohei Sato, 0 is other than that)

img_0.png 1
img_1.png 0
img_9.png 0
︙

Creating a learner using tensorflow

Create a learner by referring to the following.

Identify the anime Yuruyuri production company with TensorFlow http://kivantium.hateblo.jp/entry/2015/11/18/233834

I will add it later from here. Please wait a little longer.

Recommended Posts

Make a Sato Yohei discriminator using OpenCV and TensorFlow
Make a face recognizer using TensorFlow
Make one Mario using Numpy and OpenCV
I tried to make a ○ ✕ game using TensorFlow
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
How to make a surveillance camera (Security Camera) with Opencv and Python
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
I want to make a web application using React and Python flask
Make a video player with PySimpleGUI + OpenCV
I tried playing a ○ ✕ game using TensorFlow
Let's make a multilingual site using flask-babel
I tried to make a simple image recognition API with Fast API and Tensorflow
A addictive story when using tensorflow on Android
A story about simple machine learning using TensorFlow
Head orientation estimation using Python and OpenCV + dlib
I tried object detection using Python and OpenCV
Make a chatbot and practice to be popular.
Create a web map using Python and GDAL
[Chat De Tornado] Make a chat using WebSocket with Tornado
Try using tensorflow ① Build python environment and introduce tensorflow
Create a Mac app using py2app and Python3! !!
Let's make a module for Python using SWIG
Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV