--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.
--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.
Flask
--Import the previously created face_deep.py
.
import face_deep
――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)
--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>
--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