Image recognition with Keras + OpenCV

Abstract

I made a prototype program to classify images using Keras and OpenCV. You can determine the outcome of the backgammon precision dice. The accuracy is also reasonable.

Related work

I made the same thing last year. At that time, it was Caffe + OpenCV.

It worked reasonably well, but I soon got tired of it because Caffe was difficult and it became troublesome to write in C ++. However, the same learning data was used this time. In a sense, the task of creating learning data is the most difficult, so this time it was the job that I got on my shoulder last year.

Train the model

I used the model of CIFAR10 sample. To load training and validation data from an image file, here It is easy to use ImageDataGenerator.flow_from_directory () as shown in.

Predict with OpenCV images

Let's classify the images using the completed model. If you want to read and classify JPEG files, you can use the code from the blog above.

img = load_img('data/train/cats/cat.0.jpg')  # this is a PIL image
x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
x /= 255

You can call model.predict () with this x as an argument.

However, it took some time to use OpenCV format data. The image data of OpenCV was already an array of numpy, but since the color order is BGR, it needs to be converted to RGB. In addition, I will go to the array to match the shape.

x = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)
x = x.transpose((2, 0, 1))
x = x.reshape((1,) + x.shape)
x /= 255

Conclusions

I was frustrated by Caffe, but Keras is quite so. The YouTube video program at the beginning is about 100 lines. I'm really glad I remember Python.

Future work

I'm thinking of trying to recognize backgammon checkers.

Acknowlegements

I referred to the following page.

Recommended Posts

Image recognition with Keras + OpenCV
Image recognition with keras
Face recognition with Python's OpenCV
Get image features with OpenCV
Face recognition / cutting with OpenCV
Image recognition
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Try face recognition with python + OpenCV
Find image similarity with Python + OpenCV
Try blurring the image with opencv2
Object recognition with openCV by traincascade
I tried face recognition with OpenCV
Face recognition of anime characters with Keras
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
[python, openCV] base64 Face recognition with images
I tried simple image recognition with Jupyter
Light image processing with Python x OpenCV
Image processing with Lambda + OpenCV (gray image creation)
I tried "smoothing" the image with Python + OpenCV
XavierNX accelerates OpenCV image processing with GPU (CUDA)
I tried "differentiating" the image with Python + OpenCV
I tried image recognition of CIFAR-10 with Keras-Image recognition-
How to crop an image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
Do image recognition with Caffe model Chainer Yo!
[Small story] Test image generation with Python / OpenCV
Face recognition with Edison
Detect stoop with OpenCV
Binarization with OpenCV / Python
CIFAR-10 tutorial with Keras
Pepper Tutorial (7): Image Recognition
Rotate sprites with OpenCV
Image processing with Python
Multivariate LSTM with Keras
CNN 1 Image Recognition Basics
Data Augmentation with openCV
Easy TopView with OpenCV
Stumble with homebrew opencv3
Image Processing with PIL
[OpenCV / Python] I tried image analysis of cells with OpenCV
Image capture / OpenCV speed comparison with and without GPU
Challenge image classification with TensorFlow2 + Keras 3 ~ Visualize MNIST data ~
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Until you can do simple image recognition with Jupyter
Image download with Flickr API
Image processing with Python (Part 2)
I tried "gamma correction" of the image with Python + OpenCV
Read image coordinates with Python-matplotlib
Image processing with PIL (Pillow)
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
Install Keras (used with Anaconda)
Try edge detection with OpenCV
Image classification with self-made neural network by Keras and PyTorch
Multiple regression analysis with Keras
Deep learning image recognition 1 theory
Auto Encodder notes with Keras
Face recognition with Amazon Rekognition
Implemented word2vec with Theano + Keras