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)
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))
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.
- Access Google Drive
You're ready to write code.
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
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.
Read cascade.xml in the same way. When it is displayed on the left like the image read by 2)
, it is ready.
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)
.
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!
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!
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. ..
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! !!
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