将预测保存到json或txt文件中,还将输出视频文件保存在yolov3对象检测中-Google Colab [英] Save prediction to json or txt file and also save output video file in yolov3 object detection - Google colab
问题描述
https://github.com/AlexeyAB/darknet/上的对象检测正在运行,并且输出将保存到.avi
文件.我还想将预测保存到json
或txt
文件中.
Object detection on https://github.com/AlexeyAB/darknet/ is working and the output is saved to an .avi
file. I also want to save the predictions to a json
or txt
file.
这是我运行的代码:
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights -dont_show test_vid.mp4 -i 0 -out result.json -out_filename output.avi -ext_output -dont_show
但是仅保存视频输出.我希望将预测也保存到json
或txt
文件中.我在这里做错了什么?还有其他方法吗?
But only the video output is saved. I want the predictions also to be saved to json
or txt
file. What am I doing wrong here? Is there any other way to do it?
我是计算机视觉的新手,因此需要一些帮助.谢谢
I'm new to computer vision and need some help with this. Thanks
推荐答案
查看演示在这里:
void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes, int avgframes,
int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int dontdraw_bbox, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec, char *http_post_host,
int benchmark, int benchmark_layers)
它没有名为-out
的参数.
如果要使用演示,则在现有代码中,您有两个选择:
- 将结果保存到视频文件:
-out_filename res.avi
- 使用软件浏览器或Web浏览器通过网络在线获取结果:
-json_port 8070 -mjpeg_port 8090
- Save results to video file:
-out_filename res.avi
- Get results online over the network by using your soft or Web-browser:
-json_port 8070 -mjpeg_port 8090
对于现有代码,-out
仅随detector test
一起提供.来自此函数定义:
With existing code -out
is provided with detector test
only. From this function definition:
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh,
float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile, int letter_box, int benchmark_layers)
要处理图像列表data/train.txt
并将检测结果保存到result.json
文件:
To process a list of images data/train.txt
and save results of detection to result.json
file:
./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt
请注意,这是为了对一组输入图像进行检测并将结果保存到json
.
Note that, this is meant for doing detection on set of input images and save results to json
.
在所有可能的地方此处命令以及标志和参数,它们的用法得到了很好的解释.
Check here for all possible commands along with flags and arguments, their usage is explained well.
如果要对输入视频进行检测并将预测另存为json
,则有两个选择:
If you want to run detection on input video and save predictions as json
, you have two options:
- 使用opencv将视频转换为输入图像集,并使用以下命令:
./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt
- 更改代码以在演示中包含
-out
功能:
您需要将此参数包含在 demo中的演示函数中.h , yolo.c , detector.c , demo.c-2 :
You need to include this argument to demo function in demo.h, yolo.c, detector.c, demo.c - 1 and demo.c - 2:
`char *outfile`
将以下代码段添加到demo.c
:
FILE* json_file = NULL;
if (outfile) {
json_file = fopen(outfile, "wb");
if(!json_file) {
error("fopen failed");
}
char *tmp = "[\n";
fwrite(tmp, sizeof(char), strlen(tmp), json_file);
}
在此处添加此代码段:>
if (json_file) {
if (json_buf) {
char *tmp = ", \n";
fwrite(tmp, sizeof(char), strlen(tmp), json_file);
}
++json_image_id;
json_buf = detection_to_json(dets, nboxes, l.classes, names, json_image_id, input);
fwrite(json_buf, sizeof(char), strlen(json_buf), json_file);
free(json_buf);
}
关闭json
文件此处:
if (json_file) {
char *tmp = "\n]";
fwrite(tmp, sizeof(char), strlen(tmp), json_file);
fclose(json_file);
}
这篇关于将预测保存到json或txt文件中,还将输出视频文件保存在yolov3对象检测中-Google Colab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!