Qengineering / TensorFlow_Lite_SSD_RPi_64-bits

TensorFlow Lite SSD on bare Raspberry Pi 4 with 64-bit OS at 24 FPS
https://qengineering.eu/install-ubuntu-18.04-on-raspberry-pi-4.html
BSD 3-Clause "New" or "Revised" License
40 stars 6 forks source link

Ignore unnecessary objects #6

Closed mmucahitkaya closed 1 year ago

mmucahitkaya commented 1 year ago

i want to ignore some objects like refrigator. How can i edit COCO_labels.txt file? I try to change label name with none, null, unlabeled, but when i run code, just refrigator name changed with none, null or unlabeled

Qengineering commented 1 year ago

Changing the labels is just doing that: you alter the printed text. You have to add some additional code. See MobileNetV1.cpp line 75 Here you find the variable det_index holding the index to your object name.

    const float confidence_threshold = 0.5;
    for(int i = 0; i < num_detections; i++){
        if(detection_scores[i] > confidence_threshold){
            int  det_index = (int)detection_classes[i]+1;
            float y1=detection_locations[4*i  ]*cam_height;
            float x1=detection_locations[4*i+1]*cam_width;
            float y2=detection_locations[4*i+2]*cam_height;
            float x2=detection_locations[4*i+3]*cam_width;

            Rect rec((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
            rectangle(src,rec, Scalar(0, 0, 255), 1, 8, 0);
            putText(src, format("%s", Labels[det_index].c_str()), Point(x1, y1-5) ,FONT_HERSHEY_SIMPLEX,0.5, Scalar(0, 0, 255), 1, 8, 0);
        }
    }

If you want to exclude refrigators, look for the index in the COCO list, number 83. Next skip all the code printing the info with a continue statement like this.

    const float confidence_threshold = 0.5;
    for(int i = 0; i < num_detections; i++){
        if(detection_scores[i] > confidence_threshold){
            int  det_index = (int)detection_classes[i]+1;

            if(det_index == 83) continue;

            float y1=detection_locations[4*i  ]*cam_height;
            float x1=detection_locations[4*i+1]*cam_width;
            float y2=detection_locations[4*i+2]*cam_height;
            float x2=detection_locations[4*i+3]*cam_width;

            Rect rec((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
            rectangle(src,rec, Scalar(0, 0, 255), 1, 8, 0);
            putText(src, format("%s", Labels[det_index].c_str()), Point(x1, y1-5) ,FONT_HERSHEY_SIMPLEX,0.5, Scalar(0, 0, 255), 1, 8, 0);
        }
    }

More continues are possible

mmucahitkaya commented 1 year ago

thanks for your information, I have a last question. How can i count total detected object? I try to num_detections but that equal to 10.

Qengineering commented 1 year ago

Lets use a new variable CntObj (shorthand for Count Objects) and put it the loop like this

    const float confidence_threshold = 0.5;

    CntObj = 0;

    for(int i = 0; i < num_detections; i++){
        if(detection_scores[i] > confidence_threshold){
            int  det_index = (int)detection_classes[i]+1;

            if(det_index == 83) continue;

            CntObj++;

            float y1=detection_locations[4*i  ]*cam_height;
            float x1=detection_locations[4*i+1]*cam_width;
            float y2=detection_locations[4*i+2]*cam_height;
            float x2=detection_locations[4*i+3]*cam_width;

            Rect rec((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
            rectangle(src,rec, Scalar(0, 0, 255), 1, 8, 0);
            putText(src, format("%s", Labels[det_index].c_str()), Point(x1, y1-5) ,FONT_HERSHEY_SIMPLEX,0.5, Scalar(0, 0, 255), 1, 8, 0);
        }
    }

You want to reset the counter every time the loops start, so just before the for(int i = 0; i < num_detections; i++){ the variable is set to zero CntObj = 0;.

The last action is to tell the C++ compiler the existance of the variable CntObj. There are many ways. For now the quick and dirty solution by declaring it global at the beginning of MobileNetV1.cpp line 16

int CntObj;
const size_t width = 300;
const size_t height = 300;

std::vector<std::string> Labels;
std::unique_ptr<tflite::Interpreter> interpreter;
mmucahitkaya commented 1 year ago

Thanks, i did it