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.
Reference: https://www.tech-tech.xyz/haar-cascade.html
There seems to be about 3 patterns,
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.
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.
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.
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.
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
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
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
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.
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()
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/
● 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
The file list generated in the location specified by -info is output. Create multiple files and merge them into one file to use as input for vector creation.
At this time, the contents of nglist can be just the file name. No file path required.
⇒ [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
From here, I have the impression that there is no choice but to arrange the input data a little ... Continued. ..
Recommended Posts