YOLO v4 批量生成并保存图片与坐标信息

YOLO v4 批量生成并保存图片与坐标信息

YOLO v4还有在其它框架下实现的可以下载,可以参照github上给出的链接,根据自己需求进行下载:
https://github.com/AlexeyAB/darknet#yolo-v4-in-other-frameworks
我使用的是作者的C语言版本的YOLO,因为需要对一系列图像进行目标检测并且保存生成图片和label信息,所以对code进行了一些改动。
首先是darknet/src/detector.c:
test_detector和draw_object里的

draw_detections_v3(sized, dets, nboxes, thresh, names, alphabet, l.classes, 1);
save_image(im, "predictions");

改为

const char save_path[] = "F:/YOLO/results";//自己的保存图片路径
const char ch = '/'; //注意这里图片路径使用的是/,如果是\的话要写成'\\'
char *ret;
char *save_name;
ret = strrchr(filename, ch);
save_name = strtok(ret, ".");
draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output, save_name); //这里有增加save_name这个参数,需要到src/image.h中也改一下
save_name = strtok(ret, ".");
strcat(save_path, ret);
save_image(im, save_path);

image.c中:

void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output)

改为

void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output, char *save_name)

draw_detections_v3中,

static int frame_id = 0;

前加上

const char box_path[] = "F:/YOLO/results/boxes";//自己的保存label信息的文件夹
strcat(save_name,".txt");
strcat(box_path,save_name);
FILE *fw = fopen(box_path,"w+");
char buff[1024];

将label信息输出并以(class_name percentage xmin ymin xmax ymax)的格式保存:

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");
    }
}

改为

        printf("%s: %.0f%%", names[best_class],    selected_detections[i].det.prob[best_class] * 100);
        sprintf(buff, "%s %.06f ", names[best_class], selected_detections[i].det.prob[best_class]);
        fprintf(fw,buff);
        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));
            sprintf(buff, "\t %4.0f  %4.0f  %4.0f  %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.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));
            fprintf(fw, buff);
        }
        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);
                sprintf(buff, "%s: %.06f ", names[j], selected_detections[i].det.prob[j]);
                fprintf(fw, buff);
                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));
                    sprintf(buff, "\t %4.0f  %4.0f  %4.0f  %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.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));
                    fprintf(fw, buff);
                }
                else
                printf("\n");
           }
        }

如果是使用png图片的话,ctrl+F把detector.c和image.c里面的jpg都换成png就行(包括大小写)

作者在github上也提供了保存label信息的方法,暂时还没有尝试过。
https://github.com/tzutalin/labelImg

为了不改动太多源码,使用了bash脚本来进行一系列图片的生成和保存,windows下可以下载MSYS2来运行,或者安装git然后使用git bash。我这里使用的是MSYS2

#!/bin/bash
path_dir="../../f/RTTS/ImageSets/Main/test.txt" ##存有图片名字的文件,不带路径和后缀
img_dir="F:/RTTS/JPEGImages/"

bak=$IFS
IFS=$'\n'
mark=".png" ##源图为png格式,需要jpg的将其改成".jpg",或者其它的图片格式
for i in `cat $path_dir`
do
	./darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights $img_dir$i$mark -dont_show -ext_output

done
IFS=$bak
以下是调用摄像头并应用yolo v4进行检测的Python代码示例: ```python import cv2 import numpy as np # Load Yolo net = cv2.dnn.readNet("yolov4.weights", "yolov4.cfg") classes = [] with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] # Initialize webcam cap = cv2.VideoCapture(0) while True: # Read frame from webcam _, frame = cap.read() # Detect objects in the frame height, width, _ = frame.shape blob = cv2.dnn.blobFromImage(frame, 1/255, (416, 416), (0, 0, 0), swapRB=True, crop=False) net.setInput(blob) output_layers_names = net.getUnconnectedOutLayersNames() layerOutputs = net.forward(output_layers_names) # Get bounding boxes for detected objects boxes = [] confidences = [] class_ids = [] for output in layerOutputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w/2) y = int(center_y - h/2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) # Apply non-max suppression to remove overlapping bounding boxes indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) # Draw bounding boxes and labels for detected objects font = cv2.FONT_HERSHEY_PLAIN colors = np.random.uniform(0, 255, size=(len(boxes), 3)) if len(indexes) > 0: for i in indexes.flatten(): x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) confidence = str(round(confidences[i], 2)) color = colors[i] cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2) cv2.putText(frame, label + " " + confidence, (x, y+20), font, 2, (255,255,255), 2) # Display output cv2.imshow("Object Detection", frame) # Exit loop by pressing 'q' if cv2.waitKey(1) == ord('q'): break # Release resources cap.release() cv2.destroyAllWindows() ``` 在此代码中,我们使用了OpenCV的dnn模块来加载yolo v4模型及其配置文件,并从COCO数据集中获取类名。然后,我们初始化了摄像头,并在循环中读取摄像头帧。我们将每一帧输入到yolo v4模型中,以检测其中的物体,并获取每个物体的边界框,置信度和类别ID。然后,我们对这些边界框应用非最大抑制(NMS)算法,以消除重叠的边界框。最后,我们在每个检测到的物体周围绘制边界框和标签,并将输出显示在屏幕上。我们通过按下“q”键退出循环,并释放摄像头资源。
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值