[PYTHON] Face image inference using Flask and TensorFlow

Introduction

--Modified the tutorial of MNIST CNN of TensorFlow to learn and infer facial images. --This time, I will make an inference from Flask and display the result. --The complete source is here.

Overview

--Inference using test images is performed for each class specified in the configuration file. ――The result of each image means that blue is correct and red is incorrect. ――We made it possible to understand which class each was classified into.

40q6c-l084k.gif

Flask

Import training model

--Import the previously created face_deep.py.

import face_deep

inference

――We make it possible to make inferences with learning images and test images. --All .jpeg in the folder are inferred.

@app.route('/predict/<folder>/<item>')
def predict(folder, item):
    """Image inference."""

    if folder not in ['train', 'test']:
        abort(404)

    filename_list = sorted(glob.glob(os.path.join(DATA_PATH, folder, item, '*.jpeg')))

--Each image is read with Pillow and resized, grayscaled and the value is changed from 0-255 to 0-1.

    image_list = []
    for filename in filename_list:

        face = Image.open(filename)
        face = face.resize((IMG_ROWS, IMG_COLS), Image.LANCZOS)
        face = face.convert('L')
        face = np.array(face, dtype=np.float32) / 255.0
        face = np.ravel(face)
        image_list.append(face)

--Collect each image and enter it in predict of face_deep.py. --As for the inference result, an array containing a probability such as [99 0 0 0 0 0 0 0 0 0] is returned for each image.

    percent_list = face_deep.predict(image_list, dtype='int')

--Make modifications for the template. --color is given by True if the inference result of the target image is correct and False if it is incorrect. --With filename, you can create an image link from the template. ――In percent, the probability of each image class is displayed in the template.

    rows = []
    for filename, percent in zip(filename_list, percent_list):
        color = CLASSES.index(item) in [index for index, value in enumerate(percent) if value == max(percent)]
        row = {'filename': os.path.basename(filename), 'percent': percent, 'color': color}
        rows.append(row)

    return render_template('predict.html', folder=folder, item=item, headers=CLASSES, rows=rows)

template

--By the above color, the blue table-primary is set if the answer is correct, and the red table-danger is set if the answer is incorrect.

          {% if row.color %}
          <tr class="table-primary">
          {% else %}
          <tr class="table-danger">
          {% endif %}

--The face image link is created based on the file name etc. --Since the saved images are large and small, they are dynamically changed with size.

            <td>
                <figure class="figure">
                  <img src="/data/{{ folder }}/{{ item }}/{{ row.filename }}?size=100" />
                  <figcaption class="figure-caption">{{ row.filename }}</figcaption>
                </figure>
            </td>

--The probability for each class of each image is displayed.

            {% for percent in row.percent %}
            <td scope="row">{{ percent }}%</td>

in conclusion

--Using Flask, the inference result of the face image was displayed. --Since a list of inference results and probabilities for each class can be displayed, it is easier to check inappropriate images. ――Next time, I would like to create a web application that can upload various images.

Recommended Posts

Face image inference using Flask and TensorFlow
Response the resized image using Flask and PILImage
Similar face image detection using face recognition and PCA and K-means clustering
Make a face recognizer using TensorFlow
Image recognition using CNN Horses and deer
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
Face image dataset sorting using machine learning model (# 3)
Try using tensorflow ① Build python environment and introduce tensorflow
POST the image with json and receive it with flask
Make a Sato Yohei discriminator using OpenCV and TensorFlow
Image segmentation using U-net
Image uploader in Flask
Image normalization in TensorFlow
Twitter authentication using Flask and React is very forcible using WebSocket
[Python] Accessing and cropping image pixels using OpenCV (for beginners)