实战小项目之基于yolo的目标检测web api实现
Posted 悠悠南山下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实战小项目之基于yolo的目标检测web api实现相关的知识,希望对你有一定的参考价值。
上个月,对微服务及web service有了一些想法,看了一本app后台开发及运维的书,主要是一些概念性的东西,对service有了一些基本了解。互联网最开始的构架多是cs构架,浏览器兴起以后,变成了bs,最近几年,随着移动互联网的兴起,cs构架再次火了起来,有了一个新的概念,web service。
最近两天,想结合自己这段时间学的东西,实现一个cs构架的service接口。说一下大体流程,client上传图片到http服务器,http后台使用yolo进行图片的检测,之后将检测结果封装成json返回到client,client进行解析显示。
client
使用libcurl作为http请求工具,使用rapidjson进行结果json数据的解析
上传图片时,没有使用标准的http多媒体方式,而是使用post 二进制流的方式,比较笨,有待改进。
server
物体检测识别使用yolo c语言版本,修改原工程darknet的main,引入自己的main,实现直接检测的功能,main的流程:
导入yolo参数--必要初始化--fork子进程--安装信号--初始化fifo--sleep等待图片上传 接收信号唤醒--读取图像--预测-写入json文件--fifo写唤醒子进程
| | |
执行libevent实现的http server--eventloop监听--有文件上传结束--signal 父进程--阻塞在fifo读 读取json,http返回
具体代码
client
extern "C"{ #include <unistd.h> #include <sys/types.h> #include <time.h> #include <errno.h> #include <stdio.h> #include <signal.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/time.h> #include <fcntl.h> //iso #include <stdio.h> #include <stdlib.h> #include <string.h> //others #include "curl/curl.h" } //c++ #include <iostream> #include <string> #include <fstream> #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" #define psln(x) std::cout << #x " = " << (x) << std::endl using namespace std; size_t WriteFunction(void *input, size_t uSize, size_t uCount, void *arg) { size_t uLen = uSize * uCount; string *pStr = (string*) (arg); pStr->append((char*) (input), uLen); return uLen; } int main(int argc,char **argv){ if(argc<3){ printf("usage:./a.out uri pic\\n"); exit(-1); } CURL *pCurl = NULL; CURLcode code; code = curl_global_init(CURL_GLOBAL_DEFAULT); if (code != CURLE_OK) { cout << "curl global init err" << endl; return -1; } pCurl = curl_easy_init(); if (pCurl == NULL) { cout << "curl easy init err" << endl; return -1; } curl_slist *pHeaders = NULL; string sBuffer; string header = "username:tla001"; pHeaders = curl_slist_append(pHeaders, header.c_str()); ifstream in; in.open(argv[2], ios::in | ios::binary); if (!in.is_open()) { printf("open err\\n"); exit(-1); } in.seekg(0, ios_base::end); const size_t maxSize = in.tellg(); in.seekg(0); char * picBin = new char[maxSize]; in.read(picBin, maxSize); in.close(); cout << maxSize << endl; size_t sendSize = maxSize + sizeof(size_t); char *sendBuff = new char[sendSize]; // sprintf(sendBuff, "%d", maxSize); memcpy(sendBuff, &maxSize, sizeof(size_t)); // size_t tmp = 0; // memcpy(&tmp, sendBuff, sizeof(size_t)); // cout << "tmp=" << tmp << endl; memcpy(sendBuff + sizeof(size_t), picBin, maxSize); curl_easy_setopt(pCurl, CURLOPT_URL, argv[1]); curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pHeaders); curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 20); // curl_easy_setopt(pCurl, CURLOPT_HEADER, 1); curl_easy_setopt(pCurl, CURLOPT_POST, 1L); curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, sendBuff); curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, sendSize); curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, &WriteFunction); curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &sBuffer); code = curl_easy_perform(pCurl); if (code != CURLE_OK) { cout << "curl perform err,retcode="<<code << endl; return -1; } long retcode = 0; code = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &retcode); if (code != CURLE_OK) { cout << "curl perform err" << endl; return -1; } //cout << "[http return code]: " << retcode << endl; //cout << "[http context]: " << endl << sBuffer << endl; using rapidjson::Document; Document doc; doc.Parse<0>(sBuffer.c_str()); if (doc.HasParseError()) { rapidjson::ParseErrorCode code = doc.GetParseError(); psln(code); return -1; } using rapidjson::Value; Value &content = doc["content"]; if (content.IsArray()) { for (int i = 0; i < content.Size(); i++) { Value &v = content[i]; assert(v.IsObject()); cout<<"object "<<"["<<i+1<<"]"<<endl; if (v.HasMember("class") && v["class"].IsString()) { cout <<"\\t[class]:"<<v["class"].GetString()<<endl; } if (v.HasMember("prob") && v["prob"].IsDouble()) { cout <<"\\t[prob]:"<<v["prob"].GetDouble()<<endl; } cout<<"\\t***************************"<<endl; if (v.HasMember("left") && v["left"].IsInt()) { cout <<"\\t[left]:"<<v["left"].GetInt()<<endl; } if (v.HasMember("right") && v["right"].IsInt()) { cout <<"\\t[right]:"<<v["right"].GetInt()<<endl; } if (v.HasMember("top") && v["top"].IsInt()) { cout <<"\\t[top]:"<<v["top"].GetInt()<<endl; } if (v.HasMember("bot") && v["bot"].IsInt()) { cout <<"\\t[bot]:"<<v["bot"].GetInt()<<endl; } cout<<endl; } } delete[] picBin; delete[] sendBuff; curl_easy_cleanup(pCurl); curl_global_cleanup(); return 0; }
server
main.c
#include <time.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <signal.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include "parser.h" #include "utils.h" #include "cuda.h" #include "blas.h" #include "connected_layer.h" extern void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top); extern void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen); extern void run_voxel(int argc, char **argv); extern void run_yolo(int argc, char **argv); extern void run_detector(int argc, char **argv); extern void run_coco(int argc, char **argv); extern void run_writing(int argc, char **argv); extern void run_captcha(int argc, char **argv); extern void run_nightmare(int argc, char **argv); extern void run_dice(int argc, char **argv); extern void run_compare(int argc, char **argv); extern void run_classifier(int argc, char **argv); extern void run_regressor(int argc, char **argv); extern void run_char_rnn(int argc, char **argv); extern void run_vid_rnn(int argc, char **argv); extern void run_tag(int argc, char **argv); extern void run_cifar(int argc, char **argv); extern void run_go(int argc, char **argv); extern void run_art(int argc, char **argv); extern void run_super(int argc, char **argv); extern void run_lsd(int argc, char **argv); void average(int argc, char *argv[]) { char *cfgfile = argv[2]; char *outfile = argv[3]; gpu_index = -1; network net = parse_network_cfg(cfgfile); network sum = parse_network_cfg(cfgfile); char *weightfile = argv[4]; load_weights(&sum, weightfile); int i, j; int n = argc - 5; for(i = 0; i < n; ++i){ weightfile = argv[i+5]; load_weights(&net, weightfile); for(j = 0; j < net.n; ++j){ layer l = net.layers[j]; layer out = sum.layers[j]; if(l.type == CONVOLUTIONAL){ int num = l.n*l.c*l.size*l.size; axpy_cpu(l.n, 1, l.biases, 1, out.biases, 1); axpy_cpu(num, 1, l.weights, 1, out.weights, 1); if(l.batch_normalize){ axpy_cpu(l.n, 1, l.scales, 1, out.scales, 1); axpy_cpu(l.n, 1, l.rolling_mean, 1, out.rolling_mean, 1); axpy_cpu(l.n, 1, l.rolling_variance, 1, out.rolling_variance, 1); } } if(l.type == CONNECTED){ axpy_cpu(l.outputs, 1, l.biases, 1, out.biases, 1); axpy_cpu(l.outputs*l.inputs, 1, l.weights, 1, out.weights, 1); } } } n = n+1; for(j = 0; j < net.n; ++j){ layer l = sum.layers[j]; if(l.type == CONVOLUTIONAL){ int num = l.n*l.c*l.size*l.size; scal_cpu(l.n, 1./n, l.biases, 1); scal_cpu(num, 1./n, l.weights, 1); if(l.batch_normalize){ scal_cpu(l.n, 1./n, l.scales, 1); scal_cpu(l.n, 1./n, l.rolling_mean, 1); scal_cpu(l.n, 1./n, l.rolling_variance, 1); } } if(l.type == CONNECTED){ scal_cpu(l.outputs, 1./n, l.biases, 1); scal_cpu(l.outputs*l.inputs, 1./n, l.weights, 1); } } save_weights(sum, outfile); } void speed(char *cfgfile, int tics) { if (tics == 0) tics = 1000; network net = parse_network_cfg(cfgfile); set_batch_network(&net, 1); int i; time_t start = time(0); image im = make_image(net.w, net.h, net.c*net.batch); for(i = 0; i < tics; ++i){ network_predict(net, im.data); } double t = difftime(time(0), start); printf("\\n%d evals, %f Seconds\\n", tics, t); printf("Speed: %f sec/eval\\n", t/tics); printf("Speed: %f Hz\\n", tics/t); } void operations(char *cfgfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); int i; long ops = 0; for(i = 0; i < net.n; ++i){ layer l = net.layers[i]; if(l.type == CONVOLUTIONAL){ ops += 2l * l.n * l.size*l.size*l.c * l.out_h*l.out_w; } else if(l.type == CONNECTED){ ops += 2l * l.inputs * l.outputs; } } printf("Floating Point Operations: %ld\\n", ops); printf("Floating Point Operations: %.2f Bn\\n", (float)ops/1000000000.); } void oneoff(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); int oldn = net.layers[net.n - 2].n; int c = net.layers[net.n - 2].c; scal_cpu(oldn*c, .1, net.layers[net.n - 2].weights, 1); scal_cpu(oldn, 0, net.layers[net.n - 2].biases, 1); net.layers[net.n - 2].n = 9418; net.layers[net.n - 2].biases += 5; net.layers[net.n - 2].weights += 5*c; if(weightfile){ load_weights(&net, weightfile); } net.layers[net.n - 2].biases -= 5; net.layers[net.n - 2].weights -= 5*c; net.layers[net.n - 2].n = oldn; printf("%d\\n", oldn); layer l = net.layers[net.n - 2]; copy_cpu(l.n/3, l.biases, 1, l.biases + l.n/3, 1); copy_cpu(l.n/3, l.biases, 1, l.biases + 2*l.n/3, 1); copy_cpu(l.n/3*l.c, l.weights, 1, l.weights + l.n/3*l.c, 1); copy_cpu(l.n/3*l.c, l.weights, 1, l.weights + 2*l.n/3*l.c, 1); *net.seen = 0; save_weights(net, outfile); } void oneoff2(char *cfgfile, char *weightfile, char *outfile, int l) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights_upto(&net, weightfile, 0, net.n); load_weights_upto(&net, weightfile, l, net.n); } *net.seen = 0; save_weights_upto(net, outfile, net.n); } void partial(char *cfgfile, char *weightfile, char *outfile, int max) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights_upto(&net, weightfile, 0, max); } *net.seen = 0; save_weights_upto(net, outfile, max); } #include "convolutional_layer.h" void rescale_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } int i; for(i = 0; i < net.n; ++i){ layer l = net.layers[i]; if(l.type == CONVOLUTIONAL){ rescale_weights(l, 2, -.5); break; } } save_weights(net, outfile); } void rgbgr_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } int i; for(i = 0; i < net.n; ++i){ layer l = net.layers[i]; if(l.type == CONVOLUTIONAL){ rgbgr_weights(l); break; } } save_weights(net, outfile); } void reset_normalize_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if (weightfile) { load_weights(&net, weightfile); } int i; for (i = 0; i < net.n; ++i) { layer l = net.layers[i]; if (l.type == CONVOLUTIONAL && l.batch_normalize) { denormalize_convolutional_layer(l); } if (l.type == CONNECTED && l.batch_normalize) { denormalize_connected_layer(l); } if (l.type == GRU && l.batch_normalize) { denormalize_connected_layer(*l.input_z_layer); denormalize_connected_layer(*l.input_r_layer); denormalize_connected_layer(*l.input_h_layer); denormalize_connected_layer(*l.state_z_layer); denormalize_connected_layer(*l.state_r_layer); denormalize_connected_layer(*l.state_h_layer); } } save_weights(net, outfile); } layer normalize_layer(layer l, int n) { int j; l.batch_normalize=1; l.scales = calloc(n, sizeof(float)); for(j = 0; j < n; ++j){ l.scales[j] = 1; } l.rolling_mean = calloc(n, sizeof(float)); l.rolling_variance = calloc(n, sizeof(float)); return l; } void normalize_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } int i; for(i = 0; i < net.n; ++i){ layer l = net.layers[i]; if(l.type == CONVOLUTIONAL && !l.batch_normalize){ net.layers[i] = normalize_layer(l, l.n); } if (l.type == CONNECTED && !l.batch_normalize) { net.layers[i] = normalize_layer(l, l.outputs); } if (l.type == GRU && l.batch_normalize) { *l.input_z_layer = normalize_layer(*l.input_z_layer, l.input_z_layer->outputs); *l.input_r_layer = normalize_layer(*l.input_r_layer, l.input_r_layer->outputs); *l.input_h_layer = normalize_layer(*l.input_h_layer, l.input_h_layer->outputs); *l.state_z_layer = normalize_layer(*l.state_z_layer, l.state_z_layer->outputs); *l.state_r_layer = normalize_layer(*l.state_r_layer, l.state_r_layer->outputs); *l.state_h_layer = normalize_layer(*l.state_h_layer, l.state_h_layer->outputs); net.layers[i].batch_normalize=1; } } save_weights(net, outfile); } void statistics_net(char *cfgfile, char *weightfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if (weightfile) { load_weights(&net, weightfile); } int i; for (i = 0; i < net.n; ++i) { layer l = net.layers[i]; if (l.type == CONNECTED && l.batch_normalize) { printf("Connected Layer %d\\n", i); statistics_connected_layer(l); } if (l.type == GRU && l.batch_normalize) { printf("GRU Layer %d\\n", i); printf("Input Z\\n"); statistics_connected_layer(*l.input_z_layer); printf("Input R\\n"); statistics_connected_layer(*l.input_r_layer); printf("Input H\\n"); statistics_connected_layer(*l.input_h_layer); printf("State Z\\n"); statistics_connected_layer(*l.state_z_layer); printf("State R\\n"); statistics_connected_layer(*l.state_r_layer); printf("State H\\n"); statistics_connected_layer(*l.state_h_layer); } printf("\\n"); } } void denormalize_net(char *cfgfile, char *weightfile, char *outfile) { gpu_index = -1; network net = parse_network_cfg(cfgfile); if (weightfile) { load_weights(&net, weightfile); } int i; for (i = 0; i < net.n; ++i) { layer l = net.layers[i]; if (l.type == CONVOLUTIONAL && l.batch_normalize) { denormalize_convolutional_layer(l); net.layers[i].batch_normalize=0; } if (l.type == CONNECTED && l.batch_normalize) { denormalize_connected_layer(l); net.layers[i].batch_normalize=0; } if (l.type == GRU && l.batch_normalize) { denormalize_connected_layer(*l.input_z_layer); denormalize_connected_layer(*l.input_r_layer); denormalize_connected_layer(*l.input_h_layer); denormalize_connected_layer(*l.state_z_layer); denormalize_connected_layer(*l.state_r_layer); denormalize_connected_layer(*l.state_h_layer); l.input_z_layer->batch_normalize = 0; l.input_r_layer->batch_normalize = 0; l.input_h_layer->batch_normalize = 0; l.state_z_layer->batch_normalize = 0; l.state_r_layer->batch_normalize = 0; l.state_h_layer->batch_normalize = 0实战小项目之ffmpeg推流yolo视频实时检测YOLO_Online 将深度学习最火的目标检测做成在线服务实战经验分享