概述
https://blog.csdn.net/sinat_33718563/article/details/79964758
新代码:可用,如果不想在老代码上折腾更改,可以直接根据下面的新地址拉取代码。 (注意看Readme)
--------------------更新20180703-----------------
由于以前上传的项目有点点小问题,完整更新项目及说明如下链接。
https://github.com/RongSong1993/YOLOv3-SaveVideo-New/tree/master
---------------------更新20180703---------------------
老代码:同样可用,就是保存视频时会闪烁,现在评论区出现了一个解决方案,欢迎大家尝试(拉到文末查看)。
YOLOv3保存检测视频完整项目地址: https://github.com/RongSong1993/YOLOv3_SaveVideo
最近一段时间配置运行了下YOLO3网络,官方项目地址:https://pjreddie.com/darknet/yolo/,整个配置过程比较简单,按照上面那个网站操作就可以了。但是官网项目在测试本地视频或通过摄像头(webcam)获得的视频时,默认是没有保存运行结果的,因此这里主要讲的是如何保存yolo3运行检测的视频结果。有问题可以评论留言,算法没仔细研究,主要是讲述操作流程。
假设你的项目路径为./darknet,需要改动的主要有两个文件,分别是位于./darknet/src/路径下的demo.c和image.c文件。
(1)首先在image.c文件中添加save_video函数的定义,代码及截图位置如下:
-
void save_video(image p, CvVideoWriter *mVideoWriter)
-
{
-
image copy = copy_image(p);
-
if(p.c ==
3) rgbgr_image(copy);
-
int x,y,k;
-
-
IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
-
int step = disp->widthStep;
-
for(y =
0; y < p.h; ++y){
-
for(x =
0; x < p.w; ++x){
-
for(k=
0; k < p.c; ++k){
-
disp->imageData[y*step + x*p.c + k] = (
unsigned
char)(get_pixel(copy,x,y,k)*
255);
-
}
-
}
-
}
-
cvWriteFrame(mVideoWriter,disp);
-
cvReleaseImage(&disp);
-
free_image(copy);
-
}
对应位置截图如下:
(2) 然后更改demo.c文件代码,由于改动内容有多处,因此这里帖是完整的demo.c文件代码,每一组//*********rs20180415***********之间代码就是新添加的代码内容,可自行对比,包括设置你输出检测视频的名称和帧率。
-
#include "network.h"
-
#include "detection_layer.h"
-
#include "region_layer.h"
-
#include "cost_layer.h"
-
#include "utils.h"
-
#include "parser.h"
-
#include "box.h"
-
#include "image.h"
-
#include "demo.h"
-
#include <sys/time.h>
-
-
#define DEMO 1
-
-
//*********rs20180415***********
-
#define SAVEVIDEO
-
//*********rs20180415***********
-
-
#ifdef OPENCV
-
-
//*********rs20180415***********
-
#ifdef SAVEVIDEO
-
static CvVideoWriter *mVideoWriter;
-
#endif
-
//*********rs20180415***********
-
-
static
char **demo_names;
-
static image **demo_alphabet;
-
static
int demo_classes;
-
static network *net;
-
static image buff [
3];
-
static image buff_letter[
3];
-
static
int buff_index =
0;
-
static CvCapture * cap;
-
static IplImage * ipl;
-
static
float fps =
0;
-
static
float demo_thresh =
0;
-
static
float demo_hier =
.5;
-
static
int running =
0;
-
-
static
int demo_frame =
3;
-
static
int demo_index =
0;
-
static
float **predictions;
-
static
float *avg;
-
static
int demo_done =
0;
-
static
int demo_total =
0;
-
double demo_time;
-
-
detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num);
-
-
int size_network(network *net)
-
{
-
int i;
-
int count =
0;
-
for(i =
0; i < net->n; ++i){
-
layer l = net->layers[i];
-
if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
-
count += l.outputs;
-
}
-
}
-
return count;
-
}
-
-
void remember_network(network *net)
-
{
-
int i;
-
int count =
0;
-
for(i =
0; i < net->n; ++i){
-
layer l = net->layers[i];
-
if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
-
memcpy(predictions[demo_index] + count, net->layers[i].output,
sizeof(
float) * l.outputs);
-
count += l.outputs;
-
}
-
}
-
}
-
-
detection *avg_predictions(network *net, int *nboxes)
-
{
-
int i, j;
-
int count =
0;
-
fill_cpu(demo_total,
0, avg,
1);
-
for(j =
0; j < demo_frame; ++j){
-
axpy_cpu(demo_total,
1./demo_frame, predictions[j],
1, avg,
1);
-
}
-
for(i =
0; i < net->n; ++i){
-
layer l = net->layers[i];
-
if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
-
memcpy(l.output, avg + count,
sizeof(
float) * l.outputs);
-
count += l.outputs;
-
}
-
}
-
detection *dets = get_network_boxes(net, buff[
0].w, buff[
0].h, demo_thresh, demo_hier,
0,
1, nboxes);
-
return dets;
-
}
-
-
void *detect_in_thread(void *ptr)
-
{
-
running =
1;
-
float nms =
.4;
-
-
layer l = net->layers[net->n
-1];
-
float *X = buff_letter[(buff_index+
2)%
3].data;
-
network_predict(net, X);
-
-
/*
-
if(l.type == DETECTION){
-
get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
-
} else */
-
remember_network(net);
-
detection *dets =
0;
-
int nboxes =
0;
-
dets = avg_predictions(net, &nboxes);
-
-
-
/*
-
int i,j;
-
box zero = {0};
-
int classes = l.classes;
-
for(i = 0; i < demo_detections; ++i){
-
avg[i].objectness = 0;
-
avg[i].bbox = zero;
-
memset(avg[i].prob, 0, classes*sizeof(float));
-
for(j = 0; j < demo_frame; ++j){
-
axpy_cpu(classes, 1./demo_frame, dets[j][i].prob, 1, avg[i].prob, 1);
-
avg[i].objectness += dets[j][i].objectness * 1./demo_frame;
-
avg[i].bbox.x += dets[j][i].bbox.x * 1./demo_frame;
-
avg[i].bbox.y += dets[j][i].bbox.y * 1./demo_frame;
-
avg[i].bbox.w += dets[j][i].bbox.w * 1./demo_frame;
-
avg[i].bbox.h += dets[j][i].bbox.h * 1./demo_frame;
-
}
-
//copy_cpu(classes, dets[0][i].prob, 1, avg[i].prob, 1);
-
//avg[i].objectness = dets[0][i].objectness;
-
}
-
*/
-
-
-
if (nms >
0) do_nms_obj(dets, nboxes, l.classes, nms);
-
-
printf(
"