1

I'm working on a OpenCV java project that detects wire container rigid from an image. I'm pretty new into customizing Haar Cascade Classifier by using Cascade Trainer GUI tool. I added 50 photos of wire container rigid only in the p folder and 200 other photos in the n folder. After generating the Haar Cascade Classifier XML file, I ran the java code using the xml file and it's not detecting the wire container on an image. I tested using the Haar Cascade Classifier xml file for face detection and it works but not for wire containers. Am I doing something wrong? Please insist.

here is a sample photo of a wire container that i'm trying to detect. Wire Container

And here is my code that i used.

public class DetectContainerDemo {

public static void main(String[] args) {
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            System.out.println("\nRunning Detect containers Demo");

            CascadeClassifier contDetector = new CascadeClassifier("data_files//cascade.xml");
            Mat image = Imgcodecs.imread("target//testcont2.jpg");
                            
            // Detect Container in the image.
            // MatOfRect is a special container class for Rect.
            MatOfRect contDetections = new MatOfRect();
            contDetector.detectMultiScale(image, contDetections);

            System.out.println(String.format("Detected %s containers", contDetections.toArray().length));

            // Draw a bounding box around the container.
            for (Rect rect : contDetections.toArray()) {
                Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));               
            }              
            BufferedImage meh = (BufferedImage) HighGui.toBufferedImage(image);
            
            //HighGui.toBufferedImage(m)
            File output = new File("target\\test1.png");
            
            try {
                ImageIO.write(meh, "png", output);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}

}

Please let me know if this is feasible or am I doing it wrong. the code above pretty much loads the xml file and detect an object using the detectMultiScale method from Opencv library.

timJIN520
  • 299
  • 1
  • 3
  • 15
  • you could try to increase the number of samples, 50/200 sounds very small to me. You could first try to detect only frontal wire containers (like there are different cascade classifiers for frontal faces and side faces). Or you could immediately switch to deep learning, which is state of the art and better than cascade classifiers in every aspect. You could try a tiny yolo v4 network. Initialize it with pretrained weights of MS COCO and train it with your 50+200 images. – Micka Mar 30 '21 at 20:44
  • Your samples number is pretty less and also labeling is important in here. your target object seems not an easy object to detect. You may get help in [here](https://stackoverflow.com/a/60166686/11048887) about training steps. Also dont forget that `detectMultiScale` has parameters according to scenario, I ve seen you used the default ones, have a look at the [parameters](https://docs.opencv.org/3.4/d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498) – Yunus Temurlenk Mar 31 '21 at 05:35
  • Thank you for your response! I'm not sure what 'number of stages' actually meant. I increase it and it generate about 7 hours but couldnt detect the object. I'll try to increase the number of Positive images. How much positive photos would it recommend? – timJIN520 Mar 31 '21 at 14:35
  • Hello Micka and Yunus, in this case, i'm planning to stop by a facility to take pictures. In your opinion, what would be a recommend amount of pictures should i take? 500? Another question, is Cascade Trainer GUI would be ok to use? We have to use an open source tool. – timJIN520 Apr 01 '21 at 20:04

0 Answers0