[PYTHON] I tried simple image recognition with Jupyter

Overview

https://qiita.com/uz29/items/ec854106355bf783e316 Since the preparation was completed last time, I first made an image discrimination program using the existing learning model VGG16.

Library import and model import

The model will be downloaded for the first time.

import glob
import pprint
import numpy as np
import tensorflow as tf
from PIL import Image

model = tf.keras.applications.vgg16.VGG16(weights='imagenet')

Prediction script

I wanted to make predictions for all the images in the folder, so I used glob to get a list of paths and create an array of each image. Prediction seems to be able to process multiple images at the same time with a single function call.

#Predict all photos in a folder
file_list = glob.glob("./images/*")

pil = []
imgs = []
for path in file_list:
    #Image loading
    img_pil = tf.keras.preprocessing.image.load_img(path, target_size=(224, 224))
    pil.append(img_pil)
    #Convert images to arrays
    img = tf.keras.preprocessing.image.img_to_array(img_pil)
    #Convert to a 4-dimensional array
    imgs.append(img)
imgs = np.stack(imgs, 0)

#Preprocessing
img_p = tf.keras.applications.vgg16.preprocess_input(imgs)
#Forecast
predict = model.predict(img_p)
result = tf.keras.applications.vgg16.decode_predictions(predict, top=5)

View results

The prediction results can be seen below.

pprint.pprint(result[0])
plt.imshow(pil[0])

[('n02124075', 'Egyptian_cat', 0.42277277), ('n02123159', 'tiger_cat', 0.18187998), ('n02123045', 'tabby', 0.12070633), ('n02883205', 'bow_tie', 0.0892005), ('n02127052', 'lynx', 0.024664408)] image.png

pprint.pprint(result[1])
plt.imshow(pil[1])

[('n02119789', 'kit_fox', 0.6857688), ('n02119022', 'red_fox', 0.24295172), ('n02120505', 'grey_fox', 0.065218925), ('n02114855', 'coyote', 0.004371826), ('n02115913', 'dhole', 0.00046840237)] image.png

pprint.pprint(result[2])
plt.imshow(pil[2])

[('n02138441', 'meerkat', 0.9073721), ('n02137549', 'mongoose', 0.092063464), ('n02447366', 'badger', 0.00037895824), ('n02361337', 'marmot', 8.514335e-05), ('n02441942', 'weasel', 2.4436611e-05)] image.png

The subtle species of animals are not included in VGG16 and may differ, but they generally return the exact species. In the future, I would like to make my own learning model and make predictions.

Recommended Posts

I tried simple image recognition with Jupyter
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried simple image processing with Google Colaboratory.
Until you can do simple image recognition with Jupyter
I tried face recognition with OpenCV
I tried to make a simple image recognition API with Fast API and Tensorflow
I tried playing with the image with Pillow
Image recognition with keras
I tried using Jupyter
I tried handwriting recognition of runes with scikit-learn
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
I tried to start Jupyter with Amazon lightsail
I tried fp-growth with python
I tried scraping with Python
I tried Learning-to-Rank with Elasticsearch!
I tried face recognition using Face ++
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried clustering with PyCaret
I tried gRPC with Python
I tried scraping with python
I tried a simple RPA for login with selenium
I tried to touch jupyter
Image recognition with Keras + OpenCV
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried handwriting recognition of runes with CNN using Keras
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
seam carving (image cropping) I tried
I tried machine learning with liblinear
I tried web scraping with python.
I tried knocking 100 image processes (Q1 ~ Q10)
I tried implementing DeepPose with PyTorch
I tried face detection with MTCNN
Somehow I tried using jupyter notebook
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
I tried VS Code's Jupyter notebook
I tried to process the image in "sketch style" with OpenCV
[Python] I made an image viewer with a simple sorting function.
I tried to process the image in "pencil style" with OpenCV
Image recognition
I tried to make an image similarity function with Python + OpenCV
I tried multiple regression analysis with polynomial regression
I tried sending an SMS with Twilio
I tried using Amazon SQS with django-celery
I tried to implement Autoencoder with TensorFlow
I tried linebot with flask (anaconda) + heroku
I tried to visualize AutoEncoder with TensorFlow
I tried scraping Yahoo News with Python
I want to blog with Jupyter Notebook
I tried using Selenium with Headless chrome
I tried factor analysis with Titanic data!
I tried learning with Kaggle's Titanic (kaggle②)
I tried sending an email with python.
I tried a functional language with Python
I tried batch normalization with PyTorch (+ note)