2

I'm trying to create my haar cascade for screw recognition. I create with the help of utilities from the folder opencv320. I use 700 positive and 1400 negative images. Creation ends before the 16th stage: "the required limit of the acceptance ratio has been reached." I'm trying to use a cascade, but the screws are not recognized. Command to create samples.vec File:

C:\opencv320\opencv\build\x64\vc14\bin>opencv_createsamples.exe -info D:\TrainingSample\Good.dat -vec D:\TrainingSample\samples.vec -w 40 -h 20 -num 702

Command to create a classifier:

C:\opencv320\opencv\build\x64\vc14\bin>opencv_traincascade.exe -data haarcascade -vec samples.vec -bg Bad.dat -numStages 16 -minhitrate 0.999 -maxFalseAlarmRate 0.4 -numPos 561 -numNeg 1403 -w 40 -h 20 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048

Code to use the cascade:

MyApp.screw_cascade.detectMultiScale(gray, screw, 1.1, 2, 0, new Size(40, 20), new Size());
                        for (Rect rect : screw.toArray()) {
                            //Core.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
                            Point center = new Point(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5);
                            Core.ellipse(frame, center, new Size(rect.width * 0.5, rect.height * 0.5), 0, 0, 360, new Scalar(0, 255, 0), 4, 8, 0);
                            Core.putText(frame, "Screw", new Point(rect.x, rect.y), Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 255));
                        }

What am I doing wrong? Can you advise something that can help me?

All the commands I used for training I pointed out at the beginning. Additionally, I uploaded all the files used for training, including all photos, to Google drive. For training, I took images of objects without a background (*.png), superimposed them on the background, cut out the site with the object, the position of the object on the image was recorded in a file Good.dat.

The result of attempts for several days: it was possible to create a cascade for detecting nuts with virtually no gaps. To create, we used 40 images of the nut at different angles without background, which were superimposed on the fragments of each of the backgrounds (25 background pictures). A total of 2100 positive and 4000+ negative. To create a file, use the command:

opencv_createsamples.exe -info D:\TrainingSample\Good.dat -vec D:\TrainingSample\samples.vec -maxxangle 1.1 -maxyangle 1.1 -maxzangle 0.5 -w 20 -h 20 -num 2099

To create a cascade, the command was used:

opencv_traincascade.exe -data haarcascade -vec samples.vec -bg Bad.dat -numStages 12 -minhitrate 0.999 -maxFalseAlarmRate 0.3 -numPos 1680 -numNeg 4049 -w 20 -h 20 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048

The training took 10 hours.

  • positive samples should have the same aspect ratio. It would be better to have all screws oriented in the sane direction. Can you post your training steps and commands? – Micka May 20 '17 at 09:04
  • @Micka Added in the last paragraph an additional description and source materials for training. – Eugene Hudkov May 20 '17 at 11:25
  • sorry, didn't see it during fast reading. Commands look ok. As said, the positive samples should be of same aspect ratio (2:1) because they'll be resized to 40:20, so they'll be distorted if not in aspect ratio 2:1 before resizing. – Micka May 20 '17 at 11:47
  • @Micka Well, thank you, I'll try to make a cascade for a nut with a size of 20x20 (1:1). And write about the results. – Eugene Hudkov May 20 '17 at 11:50
  • your positive samples should all be of same orientation: for example all screws diagonal with head in bottom left of the image. If you want to train a classifier that detects all kind of orientations at once you probably need much more positive sanples. – Micka May 20 '17 at 11:50
  • one more thing: your positive samples all have artificial background. Instead they should have "real" background. Otherwise you might detect only screws witj artificial background again. If you have images with a constant artificial background you can merge a background to them with opencv_createsamples tool. – Micka May 20 '17 at 11:54
  • in addition please add some sample images from which you would like to detect the objects (where the detection failed). – Micka May 20 '17 at 11:54
  • @Micka Creating a cascade is an additional part of my thesis work at my request, so the real background does not exist, maximum is the palm of the hand or the surface of the table. And the image is not recognized even on the image from the positive sample. – Eugene Hudkov May 20 '17 at 12:31
  • not "the real" background but as many (random) background as possible, so that the training doesn't rely on the sharp features between the screw and the (mostly white) artificial backgrounds of your positive samples. Something I forgot: If you want to train a single classifier for all orientations you can use create_samples to rotate the positive images. But if your trained classifier doesnt even detect any of you positive samples, there might be something wrong with your detection code. Does it work with a pre-trained classifier (face detect) and a sample face image? – Micka May 20 '17 at 12:39
  • @Micka Yes, with the standard classifier "lbpcascade_frontalface" everything works. Can you look in the folder "screw-nut" on Google drive. I will try to create a screw-nuts classifier. I will cut out the object from the background and add it to as many backgrounds as possible. What parameters do I need to specify for the create_samples.exe to create a cascade for different angles? – Eugene Hudkov May 20 '17 at 12:52
  • sorry, I never had to add new background to my positive samples. I once added backhround for someone else here on stackoverflow, but not sure whether I'll find the answer now :D – Micka May 20 '17 at 13:08
  • To merge your positive samples with background samples see http://stackoverflow.com/a/35130506/2393191 – Micka May 20 '17 at 19:06
  • 1
    @Micka thanks for the link) I used Photoshop to create a transparent background of objects "objects(1)" and create a small program to put an object on the background "Good(1)", cut it and convert it to a gray color. Training has already lasted 4+ hours, another stage is being implemented (15). If not, I'll use your advice on the link. The result of my manipulations with the images you can see by clicking on the link to Google Drive – Eugene Hudkov May 20 '17 at 20:10
  • 1
    @Micka thank you very much for your help and your advices) I Could create a classifier)) Tell me hosting please, so that I can upload my source files there, can someone come in handy) – Eugene Hudkov May 21 '17 at 19:15
  • nice to hear, many people use github to share code and resources but Ibdon't know whether this would be great for training data, too... – Micka May 21 '17 at 19:17

0 Answers0