Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~

Introduction

In this article, I made a "cat detector" by applying the "face recognition" technology that I used when I participated in an image-based hackathon hosted by a company and learning from images of cats. It is summarized in Qiita for keeping as a record. The whole story was posted on 12/14. "Making a cat detector with Google Colabratory (Part 1) [Python] ~ About scraping ~" This article mainly summarizes "scraping", which is the technology used in preprocessing. If you are reading this article for the first time, it is recommended that you deepen your knowledge by looking at the first part first! !! (20201216-3)

What is OpenCV

OpenCV is an abbreviation and its official name is "Open Source Computer Vision Library". In other words, it is a library that can be used for hobbies, commercial purposes, and academics, with various functions required when processing images and videos on a computer. (More details: 1st What is OpenCV? Latest 3.0 new function overview and module configuration (1/2))

Let's use OpenCV right away!

Prepare the Google Colab used in the whole story. If you scraped in the first part, I think you could download the "cat" image to your PC in zip format. If you haven't done so, please see Part 1 and scrape it, or get the image of the cat you want to use by yourself. Please download and use it!

Please open a new Google Colaboratory. How to open is as shown last time.

  1. Access Google Drive
  1. Right-click and click "Other"
  2. Click Google Colabora try to launch.

You're ready to write code.

Download the required files.

This time we will use a cascade file. I could make this cascade file myself, and I tried to make it myself, but I will post about how to make it later in the main method of making. Please wait.

This time, you need cascade.xml in the file below, so please download and use it. Also, the image of the cat used this time is also included in this file, so if you do not have the image of the cat or want to reproduce my result, please download the image as appropriate and use it. Download here

program

1) Read the image for use in GC

Click the icon on the left here to load the image. (The image below.) !

When you load an image, it is successful if the names of the loaded images are displayed side by side on the left. Proceed to the next step.

2) Read cascade.xml

Read cascade.xml in the same way. When it is displayed on the left like the image read by 2), it is ready.

3) Install the required modules

As with the last time, I will introduce the modules required this time.

#Load the required modules
import sys
import cv2 as cv #load opencv

If you get the same error as last time, please install with ! pip install module name (the word after import).

4) Create a function for the cat detector

It is finally the generation of the learner. Finally we do something like machine learning! !! The program is as follows.

def detect(imagefilename, cascadefilename):
    srcimg = cv2.imread(imagefilename) #Load image file
    if srcimg is None:
        print('Image cannot be loaded')
        sys.exit(-1)
    dstimg = srcimg.copy()
    cascade = cv2.CascadeClassifier(cascadefilename) #Read a cascade file
    if cascade.empty():
        print('Unable to load cascading file')
        sys.exit(-1)
    objects = cascade.detectMultiScale(srcimg, 1.1, 3)
    for (x, y, w, h) in objects:
        print(x, y, w, h)
        cv2.rectangle(dstimg, (x, y), (x + w, y + h), (0, 0, 255), 2)
    return dstimg

if __name__ == '__main__':  
    result = detect('image_cat.jpg', 'cascade.xml')
    cv2.imwrite('result.jpg', result)

    result2 = detect('image_cat2.jpg', 'cascade.xml')
    cv2.imwrite('result2.jpg', result2)

    result3 = detect('image_cat3.jpg', 'cascade.xml')
    cv2.imwrite('result3.jpg', result3)
    
    #Enter the name of the image file read as above.

The whole program. Find the cat in the given image and embed in the cat. Let's see the result!

result

Successful example

Cats that are in focus on the front are accurate! The multiple cats that are facing us are also doing well with precision!

This image is a little different from the image above, but the area around the face is a shadow, but it can be detected firmly!

Examples that didn't work

Looking at the above two images, it seems that they will not respond unless they are facing the front. ..

The sleeping cat was inaccurate. (Maybe it's detected by eye?)

Even though there are only two, I have judged three. ..

If the image is dark, the detection accuracy was poor. ..

Extra edition

Only cats are not included in the learning data, but I tried to see if cats react with something like a key chain or in the extra edition. It didn't respond.

As you can see, the accuracy is still poor and I think that it is still immature as a cat detector, but I was able to detect cats for the time being. It's easy, so give it a try! !!

Consideration / Prospect

Comparing the images that could be detected correctly with the images that could not be detected, I thought that the key difference was whether or not the "eyes" could be clearly seen. As I was keenly aware of in the hackathon I participated in, I was keenly aware that "eyes" are a very important factor in "face recognition". Therefore, I think that "face recognition" responds very accurately to images at close range.

I think that "whole body authentication" is necessary to make people recognize at a distance. Even in the participating hackathons, we made three types of "face recognition", "whole body recognition", and "skeleton recognition" and compared their accuracy to the same conclusion, and even animals such as cats came to the same conclusion.

As a future prospect, it will be a little smart detector by increasing the number of images included in cascade.xml, changing it to the whole body, including some images, and including various variations of images. I think. I will try it if I have time.

Thank you for reading this far. Thank you for following and LGTM! !! We also welcome any suggestions, advice, or questions. I'm still studying, so it's very helpful!

======================================== Please read the articles posted so far if you like [Image system] Cat Detector (Part 1) [Natural language processing system] Classification of data using natural language processing

========================================

Recommended Posts

Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~
Make a fortune with Python
Let's make a GUI with python.
Make a recommender system with python
Let's make a graph with python! !!
Use OpenCV with Python 3 in Window
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
Let's make a shiritori game with Python
Make a video player with PySimpleGUI + OpenCV
Let's make a voice slowly with Python
Make a cascade classifier with google colaboratory
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Until you can use opencv with python
Let's make a web framework with Python! (2)
Make a Twitter trend bot with heroku + Python
Performance comparison of face detector with Python + OpenCV
Try to make a "cryptanalysis" cipher with Python
Make OpenCV3 available from python3 installed with pyenv
[Python] Make a simple maze game with Pyxel
The easiest way to use OpenCV with python
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
If you want to make a discord bot with python, let's use a framework
Make one repeating string with a Python regular expression.
[Python] Use JSON with Python
Try to make a command standby tool with python
Binarization with OpenCV / Python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
[Let's play with Python] Make a household account book
How to make a shooting game with toio (Part 1)
Use mecab with Python3
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
Let's make a simple game with Python 3 and iPhone
Use DynamoDB with Python
Make a breakpoint on the c layer with python
Make a function to describe Japanese fonts with OpenCV
Fill the background with a single color with OpenCV2 + Python
Use Python 3.8 with Anaconda
Use python with docker
Make a CSV formatting tool with Python Pandas PyInstaller
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
What is God? Make a simple chatbot with python
[Super easy] Let's make a LINE BOT with Python.
[Practice] Make a Watson app with Python! # 3 [Natural language classification]
Make a tky2jgd plugin with no practicality in QGIS Part 2
Associate Python Enum with a function and make it Callable
Experiment to make a self-catering PDF for Kindle with Python
A memo when face is detected with Python + OpenCV quickly
[python] A note when trying to use numpy with Cython
Use a macro that runs when saving python with vscode
Make a tky2jgd plugin with no practicality in QGIS Part 1
Post a message to Google Hangouts Chat with a thread (Python)
Create a simple video analysis tool with python wxpython + openCV
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Image processing with Python (Part 2)
Studying Python with freeCodeCamp part1
Make a Blueqat backend ~ Part 1