How to connect with drawing Bbox function and txt file?

Viewed 18

I'm using yolov3 with darknet, using my custom data.

Enviroment - window10, visual studio2019 darknet, yolov3

my custom data obj.names file written (0-good),(1-shape_miss),(2-color_miss),(3-both_miss)

The implementation of the object with the webcam was completed, but the problem occurred in the process of connecting yolo and Arduino.

step is

  1. webcam showing Boundbox about the object
  2. output in my txt file of my laptop directory(D:\..) EX> IF Bbox showing "good" -> txt file showing value =100 showing "shape-miss" -> txt file value = 200
  3. after reading txt file using Arduino communication
  4. modeling servo motors in Arduino IDE

Now, I found the some code in google, but that coding for yolov2 in 2018. so has a lot of different things in yolov3 in my code. I think it can't match the version I'm using because it's different from the one I'm using now. so this code only does build, not showing different results.

so I wanna solve this problem, I wanna see the new text file, please.... GOALS => I wanna do some processes that

  1. Starting in my laptop webcam using a darknet command
  2. Showing my custom objects to the webcam
  3. Recognizing objects and showing Bounding box
  4. Can check the value in the txt file
  5. Continue to operate on other objects

I'm using C, file input & output process

And, If put my code in a new file and run it, comes out well on the file as I want

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int main(void)
{
    FILE* fp;
    int bbox = 0;
    char context[32];

    if (bbox == 0) {
        if (fp = fopen("D:\\Programming\\Capstone\\test.txt", "r")) {
            int index = 0;
            printf("file reset\n");
            while ((context[index++] = fgetc(fp)) != EOF);
            fclose(fp);

            if ((fp = fopen("D:\\Programming\\Capstone\\test.txt", "w"))) {
                fprintf(fp, "%s", context);
                fprintf(fp, "\n");
                fclose(fp);
            }
            else
                printf("file open fail\n");

        }
        else
            printf("file open fail\n");

    }


    
    fp = fopen("C:\\Apache24\\htdocs\\index.html", "wt");
   
    if (fp == NULL) {
        printf("fail-done\n");
        return 1;
    }
    fprintf(fp, "500");
    fclose(fp);
    printf("good\n");
    return 0;
}

showing file reset good in command fromport showing 500 in txt file

But, when I'm put this code on Darknet(including Webcam), this coding is not working. just done build no error.

  1. [It's the code I referred to]
void draw_detections(image im, detection* dets, int num, float thresh, char** names, image** alphabet, int classes)
{
  FILE* hf;
  char context[32];
  int bBox = 0;
  int i,j;


  if(bBox == 0) {
    if((hf = fopen("/home/j/www/index.html", "r"))) {
        int index = 0;
        printf("file reset\n");
        while((context[index++] = fgetc(hf)) != EOF);
        fclose(hf);
        
        if((hf = fopen("/home/j/www/index.html", "w"))) {
            fprintf(hf, "%s", context);
            fprintf(hf, "\n");
            fclose(hf);
        }
        else
        printf("file open fail1\n");
    }
        else
        printf("file open fail1\n");

1-2.[other lines code I referred]

if(!strcmp(names[class], "fight")) {
    bBox = 1;
    printf("start \n");
    if((hf = fopen("/home/j/www/index.html", "w"))) {
        printf("file open success\n");
        fprintf(hf, "%d\n", (left+right)/2);
        fclose(hf);
    }
    else
    printf("file open fail\n");
}

  1. [THIS IS MY CODE ]
void draw_detections_v3(image im, detection* dets, int num, float thresh, char** names, image** alphabet, int classes, int ext_output)
{
    static int frame_id = 0;
    frame_id++;
    
    int selected_detections_num;
    detection_with_class* selected_detections = get_actual_detections(dets, num, thresh, &selected_detections_num, names);


    // text output
    qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_lefts);
    
int i;

    FILE* fp; 
    int Bbox = 0; 
    char context[32]; 

    if (Bbox == 0) { 
        if (fp = fopen("D:\\Programming\\Capstone\\test.txt", "r")) { 
            int index = 0; 
            printf("file reset\n"); 
            while ((context[index++] = fgetc(fp)) != EOF); 
            fclose(fp); 
            if ((fp = fopen("D:\\Programming\\Capstone\\test.txt", "w"))) {
               
                fprintf(fp, "%s", context);
                fprintf(fp, "\n");
                fclose(fp); 
            }
            else
                printf("file open fail\n");

        }
        else
            printf("file open fail\n");

    }

2-1. [other my code]

if (!strcmp(names[j], "good")) { 
                Bbox = 1;
                printf("start \n");
                if ((fp = fopen("D:\\Programming\\Capstone\\test.txt", "wt"))) {
                    printf("file open success\n");
                    fprintf(fp, "%d\n", (left + right) / 2);
                    fclose(fp);
                }
                else
                    printf("file open fail\n");
            }

  1. [And all of the code in mine]
void draw_detections_v3(image im, detection* dets, int num, float thresh, char** names, image** alphabet, int classes, int ext_output)
{
    static int frame_id = 0;
    frame_id++;
    
    int selected_detections_num;
    detection_with_class* selected_detections = get_actual_detections(dets, num, thresh, &selected_detections_num, names);


    // text output
    qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_lefts);
    int i;

    FILE* fp; 
    int Bbox = 0; 
    char context[32]; 

    if (Bbox == 0) { 
        if (fp = fopen("D:\\Programming\\Capstone\\test.txt", "r")) { 
            int index = 0; 
            printf("file reset\n");  
            while ((context[index++] = fgetc(fp)) != EOF); 
            fclose(fp); 

            if ((fp = fopen("D:\\Programming\\Capstone\\test.txt", "w"))) {
               
                fprintf(fp, "%s", context);
                fprintf(fp, "\n");
                fclose(fp); 
            }
            else
                printf("file open fail\n");

        }
        else
            printf("file open fail\n");

    }

    for (i = 0; i < selected_detections_num; ++i) {
        const int best_class = selected_detections[i].best_class;
        printf("%s: %.0f%%", names[best_class], selected_detections[i].det.prob[best_class] * 100);
        if (ext_output)
            printf("\t(left_x: %4.0f   top_y: %4.0f   width: %4.0f   height: %4.0f)\n",
                round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2) * im.w),
                round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2) * im.h),
                round(selected_detections[i].det.bbox.w * im.w), round(selected_detections[i].det.bbox.h * im.h));
        else
            printf("\n");
        int j;
        for (j = 0; j < classes; ++j) {
            if (selected_detections[i].det.prob[j] > thresh && j != best_class) {
                printf("%s: %.0f%%", names[j], selected_detections[i].det.prob[j] * 100);

                if (ext_output)
                    printf("\t(left_x: %4.0f   top_y: %4.0f   width: %4.0f   height: %4.0f)\n",
                        round((selected_detections[i].det.bbox.x - selected_detections[i].det.bbox.w / 2) * im.w),
                        round((selected_detections[i].det.bbox.y - selected_detections[i].det.bbox.h / 2) * im.h),
                        round(selected_detections[i].det.bbox.w * im.w), round(selected_detections[i].det.bbox.h * im.h));
                else
                    printf("\n");
            }
        }
    }


    // image output
    qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_probs);
    for (i = 0; i < selected_detections_num; ++i) {
        int width = im.h * .002;
        if (width < 1)
            width = 1;

        /*
        if(0){
        width = pow(prob, 1./2.)*10+1;
        alphabet = 0;
        }
        */

        //printf("%d %s: %.0f%%\n", i, names[selected_detections[i].best_class], prob*100);
        int offset = selected_detections[i].best_class * 123457 % classes;
        float red = get_color(2, offset, classes);
        float green = get_color(1, offset, classes);
        float blue = get_color(0, offset, classes);
        float rgb[3];

        //width = prob*20+2;

        rgb[0] = red;
        rgb[1] = green;
        rgb[2] = blue;
        box b = selected_detections[i].det.bbox;
        //printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);

        int left = (b.x - b.w / 2.) * im.w;
        int right = (b.x + b.w / 2.) * im.w;
        int top = (b.y - b.h / 2.) * im.h;
        int bot = (b.y + b.h / 2.) * im.h;

        if (left < 0) left = 0;
        if (right > im.w - 1) right = im.w - 1;
        if (top < 0) top = 0;
        if (bot > im.h - 1) bot = im.h - 1;

        //int b_x_center = (left + right) / 2;
        //int b_y_center = (top + bot) / 2;
        //int b_width = right - left;
        //int b_height = bot - top;
        //sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height);

        // you should create directory: result_img
        //static int copied_frame_id = -1;
        //static image copy_img;
        //if (copied_frame_id != frame_id) {
        //    copied_frame_id = frame_id;
        //    if (copy_img.data) free_image(copy_img);
        //    copy_img = copy_image(im);
        //}
        //image cropped_im = crop_image(copy_img, left, top, right - left, bot - top);
        //static int img_id = 0;
        //img_id++;
        //char image_name[1024];
        //int best_class_id = selected_detections[i].best_class;
        //sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, best_class_id, names[best_class_id]);
        //save_image(cropped_im, image_name);
        //free_image(cropped_im);

        if (im.c == 1) {
            draw_box_width_bw(im, left, top, right, bot, width, 0.8);    // 1 channel Black-White
        }
        else {
            draw_box_width(im, left, top, right, bot, width, red, green, blue); // 3 channels RGB
        }


        if (alphabet) {
            char labelstr[4096] = { 0 };
            strcat(labelstr, names[selected_detections[i].best_class]);
            char prob_str[10];
            sprintf(prob_str, ": %.2f", selected_detections[i].det.prob[selected_detections[i].best_class]);
            strcat(labelstr, prob_str);
            int j;
            for (j = 0; j < classes; ++j) {
                if (selected_detections[i].det.prob[j] > thresh && j != selected_detections[i].best_class) {
                    strcat(labelstr, ", ");
                    strcat(labelstr, names[j]);
                }
            }
            image label = get_label_v3(alphabet, labelstr, (im.h * .02));
            //draw_label(im, top + width, left, label, rgb);
            draw_weighted_label(im, top + width, left, label, rgb, 0.7);
            free_image(label);

            if (!strcmp(names[j], "good")) { 
                Bbox = 1;
                printf("start \n");
                if ((fp = fopen("D:\\Programming\\Capstone\\test.txt", "wt"))) {
                    printf("file open success\n");
                    fprintf(fp, "%d\n", (left + right) / 2);
                    fclose(fp);
                }
                else
                    printf("file open fail\n");
            }
        }


        if (selected_detections[i].det.mask) {
            image mask = float_to_image(14, 14, 1, selected_detections[i].det.mask);
            image resized_mask = resize_image(mask, b.w * im.w, b.h * im.h);
            image tmask = threshold_image(resized_mask, .5);
            embed_image(tmask, im, left, top);
            free_image(mask);
            free_image(resized_mask);
            free_image(tmask);
        }
    }
    free(selected_detections);
}

0 Answers
Related