[PYTHON] I want to detect objects with OpenCV

I want to detect and cut out a specific type of object from a photo. (Cars, fish, people ..) Find out if you can do it with OpenCV.

Confirmation of object detection method with OpenCV

Reference: https://www.tech-tech.xyz/haar-cascade.html

There seems to be about 3 patterns,

  1. Template matching (pattern matching) It compares the images as they are and extracts the parts with high similarity. It seems to be vulnerable to image rotation and scale changes. It also returns the degree of similarity, so I have no choice but to judge from that.

  2. Feature point extraction Unlike template patterns, different sizes and rotated ones can be extracted. As with 1, it is difficult to extract "these things". This seems to be possible for extracting unique things that do not have coins, specific paintings, or subtle differences.

  3. Use cascade classifier Using a cascade file that has been learned by reading hundreds of image files for learning in advance, those images are extracted from the images. Faces, people, upper body, eyes, etc. There are also distributed files, so if you have a face, you can use it without learning. Cascade file: https://github.com/opencv/opencv/tree/master/data/haarcascades Reference: http://opencv.jp/opencv-2.2/c/objdetect_cascade_classification.html

It seems that a cascade classifier is needed to detect something that is somewhat ambiguous that I want to do.

Template pattern if the image is specified. If the object to be extracted is specified, it is a feature point extraction, otherwise it is a cascade classifier.

Creating a cascade classifier

Reference: https://www.pro-s.co.jp/blog/system/opencv/6202

It seems that the contents written in the reference destination can be used .. It seems that the number of learning files that need to be increased can be increased with the utility tool. * I'm not sure about the accuracy, but ...

opencv_createsamples -img ./pos/1/dist/spoon1.png  -vec ./vec/1.vec  -num 10  -bgthresh 0 
opencv_createsamples -img ./pos/1/src/spoon1.png  -vec ./vec/1.vec  -num 500  -bgthresh 0   -maxidev 40 -maxxangle 0.5 -maxyangle 1.5 -maxzangle 1.5

The following output is output at each stage during learning during opencv_traincascade execution, but I'm not sure what each means.

opencv_traincascade -data ./cascade -vec ./vec/spoon1.vec -bg ./neg/jpg/nglist2.t
xt -numPos 50  -numNeg 50  -stagenumStages 12 

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count : acceptanceRatio    1000 : 1
Precalculation time: 1
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|    0.016|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 3 seconds.

Error 1 encountered

When I prepare the input file and execute ʻopencv_traincascade ...`, the following error occurs.

Train dataset for temp stage can not be filled. Branch training terminated.

In my case, it didn't work because I didn't have the path just for the file name of the negative file. Corrected by inserting the relative path from the execution location in the file. ls -v | grep jpg > nglist.txt

#At the beginning of the line./neg/jpg/Add
sed "s#^#./neg/jpg/#" nglist.txt >nglist2.txt

Error 2 encountered

Required leaf false alarm rate achieved. Branch training terminated.

Well, there is no answer to this, but the problem is that the stage progresses to some extent and the default 20 times can not be achieved, so I decided to lower the stage designation to a place where it can be cleared. The number of stages that can be cleared changes as the number of samples increases or decreases, so that seems to be affected as well.

-numStages Default 20. 10 or so.

It appears many times. This is only necessary to understand the meaning .. https://taktak.jp/2016/08/26/1618

Error 3 encountered

It's not an error, but training ʻopencv_traincascade` doesn't progress at all ... If I omitted the argument -numNeg, it would be too late. I don't know exactly what you're doing, but the default is 1000

Error 4 encountered

HOG cascade is not supported in 3.0 in function 'read'

I created a cascade classifier by specifying HOG in the featureType of traing .. When I tried to read it with openCV and use it, the above error occurred. It seems that HOG is no longer supported.

Script for execution

import cv2
 
def main():
    #Input image reading (test image file)
    img = cv2.imread("File name to be detected")
    
    #Cascade type classifier (self-made classifier)
    cascade = cv2.CascadeClassifier("./cascade/cascade.xml") # 

    #Change from face to ball (although you can leave it as it is)
    objects = cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3, minSize=(0, 0))

    #Surround the face area with a red rectangle
    for (x, y, w, h) in objects:
        cv2.rectangle(img, (x, y), (x + w, y+h), (0,0,200), 3)

    #Save result image
    cv2.imwrite("result.png ",img)
    

main()

But not detected ..

I wonder if the input data is not good ...

Try increasing the number of sample files. .. http://mirai-links.com/2018/06/11/opencv-%E7%89%A9%E4%BD%93%E6%A4%9C%E5%87%BA%E3%80%81%E5%88%86%E9%A1%9E%E5%99%A8%E4%BD%9C%E6%88%90%E3%81%A8%E6%A4%9C%E5%87%BA%E3%83%86%E3%82%B9%E3%83%88%E3%80%80%E3%81%9D%E3%81%AE%EF%BC%92/

Increase images

● Generate a sample image by combining the original image file and the background image

opencv_createsamples -info ./pos/info/1.txt -img ./pos/spoon1.png   -bg ./neg/jpg/nglist.txt -num 10 -w 24 -h 24  -show -bgthresh 200

⇒ [Caution] I have to describe the position of the target object in the output file list, but it does not adjust automatically and the size of the entire image is always specified. If you go this way, you have to make the background and input size exactly the same ..

● Create a vector file from the file list

opencv_createsamples -info ./pos/2/dist/poslist.txt  -vec ./vec/3.vec  -num 1700

● Training

opencv_traincascade -data ./cascade_harrlike/3/  -vec ./vec/3.vec  -bg ./neg/jpg/nglist2.txt -numPos 1500  -numNeg 1500  -numStages 10 -precalcValBufSize 2024 -precalcIdxBufSize 2024

Not yet detected ..

From here, I have the impression that there is no choice but to arrange the input data a little ... Continued. ..

Recommended Posts

I want to detect objects with OpenCV
I tried to detect motion quickly with OpenCV
I want to detect unauthorized login to facebook with Jubatus (1)
I want to do ○○ with Pandas
I want to debug with Python
I want to blog with Jupyter Notebook
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to check the position of my face with OpenCV!
Detect stoop with OpenCV
I tried to detect Mario with pytorch + yolov3
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to be an OREMO with setParam!
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
I tried to detect an object with M2Det!
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to write to a file with Python
Try to detect fish with python + OpenCV2.4 (unfinished)
I want to convert an image to WebP with lollipop
I want to detect images of cats from Instagram
I tried to extract features with SIFT of OpenCV
I want to transition with a button in flask
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I want to solve Sudoku (Sudoku)
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
[TensorFlow] I want to process windows with Ragged Tensor
[ML Ops] I want to do multi-project with Python
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to remove Python's Unresolved Import Warning with vsCode
I want to use R functions easily with ipython notebook
I want to specify another version of Python with pyvenv
I want to be able to analyze data with Python (Part 1)
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[NetworkX] I want to search for nodes with specific attributes
I want to make a click macro with pyautogui (desire)
I want to change the Japanese flag to the Palau flag with Numpy
I want to be able to analyze data with Python (Part 4)
I want to color black-and-white photos of memories with GAN
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I want to make a click macro with pyautogui (outlook)
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!