Cat detection with OpenCV (model distribution)

(Added on 2014/09)

I also write an article on how to identify cat breeds using deep learning techniques. Please refer if you are interested.

Cat detection with OpenCV

detect_chocolat2.jpg (Chocolat-Kichijoji Calico)

Cat image processing part 1 – Collecting materials organized how to collect cat images, but here we use the collected cat images to organize cats. I created a learning model (classifier) for detection.

If you are interested, the technical details are written on the blog. Model distribution will be discussed later in this entry.

Learning is with the familiar Boosting (one of the ensemble learning methods). For the features, we adopted LBP features, which can quickly repeat model creation and verification. In addition, about 7,000 annotation data were collected using crowdsourcing.

How to use

C++ From C ++, use it as follows.


// objdetect.cpp
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>

int main(int argc, char** argv) {
  using namespace std;
  if(argc != 3) {
    cerr << "./objdetect [image file] [cascade file]" << endl;
    exit(-1);
  }
  cv::Mat src_img = cv::imread(argv[1], 1);
  if(src_img.empty()) {
    cerr << "cannot load image" << endl;
    exit(-1);
  }
  cv::Mat dst_img = src_img.clone();
  string cascade_file = string(argv[2]);
  cv::CascadeClassifier cascade;
  cascade.load(cascade_file);
  if(cascade.empty()) {
    cerr << "cannot load cascade file" << endl;
    exit(-1);
  }
  vector<cv::Rect> objects;
  cascade.detectMultiScale(src_img, objects, 1.1, 3);
  vector<cv::Rect>::const_iterator iter = objects.begin();
  cout << "count: " << objects.size() << endl;
  while(iter!=objects.end()) {
    cout << "(x, y, width, height) = (" << iter->x << ", " << iter->y << ", "
         << iter->width << ", " << iter->height << ")" << endl;
    cv::rectangle(dst_img,
                  cv::Rect(iter->x, iter->y, iter->width, iter->height),
                  cv::Scalar(0, 0, 255), 2);
    ++iter;
  }
  cv::imwrite("result.jpg ", dst_img);
  return 0;
}
##Compile and run
$ g++ -o objdetect objdetect.cpp -I/path/to/opencv/include -L/path/to/opencv/lib -lopencv_core -lopencv_highgui  -lopencv_objdetect
$ ./objdetect cat.jpg cascade.xml
count: 1
(x, y, width, height) = (278, 185, 65, 65)

It seems that the detection accuracy will be improved if the parameters at the time of detection are specified loosely (minNeighbors parameter is set to a small value). Of course, false positives will increase, but if you add processing such as excluding the case where the detection area is smaller than a certain size (specifying the minSize parameter), you can make it look like a certain degree of accuracy.

CascadeClassifier::detectMultiScale - docs.opencv.org

Python Easy to use from Python.

#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import sys
import cv2 as cv

def detect(imagefilename, cascadefilename):
    srcimg = cv.imread(imagefilename)
    if srcimg is None:
        print('cannot load image')
        sys.exit(-1)
    dstimg = srcimg.copy()
    cascade = cv.CascadeClassifier(cascadefilename)
    if cascade.empty():
        print('cannnot load cascade file')
        sys.exit(-1)
    objects = cascade.detectMultiScale(srcimg, 1.1, 3)
    for (x, y, w, h) in objects:
        print(x, y, w, h)
        cv.rectangle(dstimg, (x, y), (x + w, y + h), (0, 0, 255), 2)
    return dstimg

if __name__ == '__main__':
    result = detect('cat.jpg', 'cascade.xml')
    cv.imwrite('result.jpg', result)

Model distribution

I will publish the model I made this time. It is a cascade file (cascade.xml) that can be read by OpenCV.

Since we are running a batch process on our personal server that detects the new addition of teacher data to the database and runs learning, it should be continuously and automatically updated to a more accurate model.

That said, if no measures are taken, the accuracy will reach a plateau, so I think that the algorithm will be improved by adding various pre-processing and post-processing.

detect_edo1.jpg detect_mimi1.jpg detect_cat-nerima1.jpg detect_5610691368_f59529e648_z.jpg detect_8854205418_cc97dbeaaa_z.jpg

Recommended Posts

Cat detection with OpenCV (model distribution)
Try edge detection with OpenCV
Real-time edge detection with OpenCV
Face detection with Python + OpenCV
Anime face detection with OpenCV
OpenCV feature detection with Google Colaboratory
Face detection with Python + OpenCV (rotation invariant)
Hello World and face detection with OpenCV 4.3 + Python
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Detect stoop with OpenCV
Binarization with OpenCV / Python
Model fitting with lmfit
Rotate sprites with OpenCV
Regression with linear model
Data Augmentation with openCV
[Summary] Object detection model "End-to-End Object Detection with Transformers" using Transformer
Easy TopView with OpenCV
Stumble with homebrew opencv3
Improve detection accuracy quickly by specifying parameters with openCV face detection
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Camera capture with Python + OpenCV
3. Normal distribution with neural network!
Feature detection using opencv (corner detection)
Face detection with Python + dlib
Binarize photo data with OpenCV
Loop video loading with opencv
Calibrate the model with PyCaret
Get image features with OpenCV
Face recognition / cutting with OpenCV
Blockchain tampering detection with Python
Face detection with Haar Cascades
Try OpenCV with Google Colaboratory
Cascade classifier creation with opencv
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Draw a watercolor illusion with edge detection in Python3 and openCV3