SNBS
SNBS

Reputation: 849

detectMultiScale hangs when using LBP cascade classifier (OpenCV 4.8.0, C++)

I've trained an LBP cascade classifier for car detection using OpenCV 4.8.0. When I try to use it, the detectMultiScale function hangs.

Here's the classifier:

<?xml version="1.0"?>
<opencv_storage>
<cascade>
  <stageType>BOOST</stageType>
  <featureType>LBP</featureType>
  <height>75</height>
  <width>100</width>
  <stageParams>
    <boostType>GAB</boostType>
    <minHitRate>9.9500000476837158e-01</minHitRate>
    <maxFalseAlarm>5.0000000000000000e-01</maxFalseAlarm>
    <weightTrimRate>9.4999999999999996e-01</weightTrimRate>
    <maxDepth>1</maxDepth>
    <maxWeakCount>100</maxWeakCount></stageParams>
  <featureParams>
    <maxCatCount>256</maxCatCount>
    <featSize>1</featSize></featureParams>
  <stageNum>4</stageNum>
  <stages>
    <!-- stage 0 -->
    <_>
      <maxWeakCount>3</maxWeakCount>
      <stageThreshold>-1.0529006719589233e+00</stageThreshold>
      <weakClassifiers>
        <_>
          <internalNodes>
            0 -1 5 -232271405 -150405104 -2147270639 -788316039
            -636434134 -922025216 -1996324149 -1170220853</internalNodes>
          <leafValues>
            -8.4758365154266357e-01 6.4864861965179443e-01</leafValues></_>
        <_>
          <internalNodes>
            0 -1 8 -217326957 -779419584 -1040187376 1497449481
            -2146905949 -938803005 281788605 -863116279</internalNodes>
          <leafValues>
            -8.3338397741317749e-01 6.5263098478317261e-01</leafValues></_>
        <_>
          <internalNodes>
            0 -1 3 -799062757 -706575487 16961969 818069578 -872412557
            -100466555 571088992 -255137189</internalNodes>
          <leafValues>
            -8.5794800519943237e-01 6.8386518955230713e-01</leafValues></_></weakClassifiers></_>
    <!-- stage 1 -->
    <_>
      <maxWeakCount>3</maxWeakCount>
      <stageThreshold>-1.1046675443649292e+00</stageThreshold>
      <weakClassifiers>
        <_>
          <internalNodes>
            0 -1 2 -582363137 -268369599 285347019 -642973687 555
            -913309688 -2013011505 -76031825</internalNodes>
          <leafValues>
            -8.5018724203109741e-01 5.8974361419677734e-01</leafValues></_>
        <_>
          <internalNodes>
            0 -1 9 -984360501 -2075981309 -1860677159 -1039814568
            -1987469154 165355976 155371681 -8140661</internalNodes>
          <leafValues>
            -8.7958252429962158e-01 5.8119404315948486e-01</leafValues></_>
        <_>
          <internalNodes>
            0 -1 10 -1481380181 1233174855 1091111312 -1778179965
            1090585999 -1442633216 1074364613 -661659159</internalNodes>
          <leafValues>
            -8.3567428588867188e-01 6.2639969587326050e-01</leafValues></_></weakClassifiers></_>
    <!-- stage 2 -->
    <_>
      <maxWeakCount>3</maxWeakCount>
      <stageThreshold>-1.0118436813354492e+00</stageThreshold>
      <weakClassifiers>
        <_>
          <internalNodes>
            0 -1 6 -913141661 1367671040 134266899 269586464 -2147426165
            -1971322496 -1577058169 -1194221141</internalNodes>
          <leafValues>
            -8.6471664905548096e-01 1.</leafValues></_>
        <_>
          <internalNodes>
            0 -1 0 -779271921 -1874788352 268468224 -1322844127 8520
            -2013265408 134226115 -633071478</internalNodes>
          <leafValues>
            -9.2569231986999512e-01 6.8080991506576538e-01</leafValues></_>
        <_>
          <internalNodes>
            0 -1 1 -805203965 553715712 50401792 293797891 -1006632832
            1158 1073774632 -1980792702</internalNodes>
          <leafValues>
            -8.2793700695037842e-01 8.2165032625198364e-01</leafValues></_></weakClassifiers></_>
    <!-- stage 3 -->
    <_>
      <maxWeakCount>2</maxWeakCount>
      <stageThreshold>7.8125000000000000e-03</stageThreshold>
      <weakClassifiers>
        <_>
          <internalNodes>
            0 -1 7 -2147434368 0 1 805306368 160 -1610612720 3 42336256</internalNodes>
          <leafValues>
            -9.9218750000000000e-01 1.</leafValues></_>
        <_>
          <internalNodes>
            0 -1 4 65538 37120 16 -2147483648 -2147483648 1 1 1208635408</internalNodes>
          <leafValues>
            -9.8839455842971802e-01 1.</leafValues></_></weakClassifiers></_></stages>
  <features>
    <_>
      <rect>
        1 51 15 6</rect></_>
    <_>
      <rect>
        3 6 9 6</rect></_>
    <_>
      <rect>
        4 51 13 6</rect></_>
    <_>
      <rect>
        5 54 5 4</rect></_>
    <_>
      <rect>
        8 59 8 5</rect></_>
    <_>
      <rect>
        14 28 24 13</rect></_>
    <_>
      <rect>
        28 43 21 10</rect></_>
    <_>
      <rect>
        30 53 13 4</rect></_>
    <_>
      <rect>
        50 29 15 12</rect></_>
    <_>
      <rect>
        66 48 8 3</rect></_>
    <_>
      <rect>
        80 64 5 3</rect></_></features></cascade>
</opencv_storage>

And this is my code:

CascadeClassifier cascade;
if (!cascade.load("path\\to\\cascade.xml")) {
  cout << "Failed to load classifier" << endl;
  return -1;
}

Mat frame = imread("path\\to\\test\\frame.jpg");
cvtColor(frame, frame, COLOR_BGR2GRAY);
equalizeHist(frame, frame);

vector<Rect> cars;
classifier.detectMultiScale(frame, cars);  // execution freezes here

for (const auto& car : cars) {
  rectangle(frame, car, Scalar(255, 0, 0), 2);
}

namedWindow("Detections", WINDOW_NORMAL);
imshow("Detections", frame);

As I can see from the debugging data, the execution freezes at the detectMultiScale call.

The XML file is loaded properly — load returns true.

UPDATE: this message might be relevant here. It appeared after training stage 3 (fourth if you count from 1).

Required leaf false alarm rate achieved. Branch training terminated.

However, as stated here, that's not an error and my cascade will still work. Despite that, I tried training a new classifier with more data to fit (200 positive samples per stage), as advised on the webpage above, which produced the same result.

An acceptable solution would be either a C++ implementation that uses my current cascade properly or instructions on training a new, usable cascade.

Upvotes: 2

Views: 173

Answers (0)

Related Questions