1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #include <iostream> #include <string> #include <vector> #include <fstream>
#include "opencv2/opencv.hpp"
extern "C"{ #include "darknet.h" }
image mat2image(const cv::Mat &mat){ cv::Mat dst; cv::cvtColor(mat, dst, cv::COLOR_RGB2BGR);
int w = mat.cols; int h = mat.rows; int c = mat.channels(); image im = make_image(w, h, c); unsigned char * imageData = (unsigned char *)dst.data; int step = dst.step; for (int y = 0; y < h; ++y) { for (int k = 0; k < c; ++k) { for (int x = 0; x < w; ++x) { im.data[k*w*h + y*w + x] = imageData[y*step + x*c + k] / 255.0f; } } } return im; }
cv::Mat image2Mat(image im){ image copy = copy_image(im); constrain_image(copy); if(im.c==3) rgbgr_image(copy);
int x,y,c; cv::Mat m; switch(im.c){ case 3: m = cv::Mat(im.w,im.h,CV_8UC3); break; case 4: m=cv::Mat(im.w,im.h,CV_8UC4); break; }
int step = m.step; for(y=0;y<im.h;++y){ for(x=0;x<im.w;++x){ for(c=0;c<im.c;++c){ float val=im.data[c*im.h*im.w+y*im.w+x]; m.data[y*step+x*im.c+c]=(unsigned char)(val*255); } } }
free_image(copy); cv::cvtColor(m,m,cv::COLOR_BGR2RGB); return m; }
std::vector<std::string> getClassesName(char path[]){ std::vector<std::string> names; if(!path){ return names; } std::ifstream readIn(path); std::string str; while(getline(readIn,str)){ names.push_back(str); } return names; }
int main() { char cfgfile[]="yolov3-tiny.cfg"; char weightfile[]="yolov3-tiny.weights"; char namefile[]="coco.names"; float thresh =0.5;
std::vector<std::string> names=getClassesName(namefile);
network *net = load_network(cfgfile,weightfile,0); set_batch_network(net,1);
cv::VideoCapture cap(0); cv::Mat frame;
if(!cap.isOpened()){ error("Couldn't connect to cam."); } int i; float nms = 0.45; double time;
while(1){ frame.release(); cap>>frame; if(frame.empty()){ std::cout<<"Frame is empty"<<std::endl; break; } image in = mat2image(frame); image in_s = letterbox_image(in,net->w,net->h); layer l = net->layers[net->n-1];
int nboxes=0; float *X=in_s.data; time=what_time_is_it_now(); network_predict(net,X); std::ostringstream str; str<< "Prediction spent "<<what_time_is_it_now()-time<<"seconds"; cv::putText(frame,str.str(),cv::Point(0,20),1,1,cv::Scalar(255,0,255),1,8,false);
detection *dets = get_network_boxes(net,in.w,in.h,thresh,0.5,0,1,&nboxes); if(nms) do_nms_sort(dets,nboxes,l.classes,nms);
for(i=0;i<nboxes;++i){ for(int j=0;j<l.classes;j++){ if(dets[i].prob[j]>thresh){ box b = dets[i].bbox; int left = (b.x-b.w/2.)*in.w; int right = (b.x+b.w/2.)*in.w; int top = (b.y-b.h/2.)*in.h; int bot = (b.y+b.h/2.)*in.h;
if(left < 0) left = 0; if(right > in.w-1) right = in.w-1; if(top < 0) top = 0; if(bot > in.h-1) bot = in.h-1;
cv::rectangle(frame,cv::Rect(left,top,right-left,bot-top),cv::Scalar(255,255,0),2,cv::LINE_8,0); std::ostringstream text; text<<names[j]<<": "<<dets[i].prob[j]*100<<"%"; cv::putText(frame,text.str(),cv::Point(left,top),1,1,cv::Scalar(255,255,0),2,8,false); } } } cv::imshow("result",frame);
free_detections(dets,nboxes); free_image(in_s); free_image(in);
if(cv::waitKey(20)==27){ break; } } return 0; }
|