Rebase Python example from ioir123ju/tkDNN/master/

This commit is contained in:
onyalcin
2020-08-22 12:53:59 -07:00
9 changed files with 424 additions and 4 deletions
+100
View File
@@ -0,0 +1,100 @@
#include "darknetTR.h"
bool gRun;
bool SAVE_RESULT = false;
void sig_handler(int signo) {
std::cout<<"request gateway stop\n";
gRun = false;
}
extern "C"
{
void copy_image_from_bytes(image im, unsigned char *pdata)
{
// unsigned char *data = (unsigned char*)pdata;
// int i, k, j;
int w = im.w;
int h = im.h;
int c = im.c;
// for (k = 0; k < c; ++k) {
// for (j = 0; j < h; ++j) {
// for (i = 0; i < w; ++i) {
// int dst_index = i + w * j + w * h*k;
// int src_index = k + c * i + c * w*j;
// im.data[dst_index] = (float)data[src_index] / 255.;
// }
// }
// }
memcpy(im.data, pdata, h * w * c);
}
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
image make_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = (float*)xcalloc(h * w * c, sizeof(float));
return out;
}
tk::dnn::Yolo3Detection* load_network(char* net_cfg, int n_classes, int n_batch)
{
std::string net;
net = net_cfg;
tk::dnn::Yolo3Detection *detNN = new tk::dnn::Yolo3Detection;
detNN->init(net, n_classes, n_batch);
return detNN;
}
#include <typeinfo>
void do_inference(tk::dnn::Yolo3Detection *net, image im)
{
std::vector<cv::Mat> batch_dnn_input;
cv::Mat frame(im.h, im.w, CV_8UC3, (unsigned char*)im.data);
batch_dnn_input.push_back(frame);
net->update(batch_dnn_input, 1);
}
detection* get_network_boxes(tk::dnn::Yolo3Detection *net, float thresh, int batch_num, int *pnum)
{
std::vector<std::vector<tk::dnn::box>> batchDetected;
batchDetected = net->get_batch_detected();
int nboxes =0;
std::vector<std::string> classesName = net->get_classesName();
detection* dets = (detection*)xcalloc(batchDetected[batch_num].size(), sizeof(detection));
for (int i = 0; i < batchDetected[batch_num].size(); ++i)
{
if (batchDetected[batch_num][i].prob > thresh)
{
dets[nboxes].cl = batchDetected[batch_num][i].cl;
strcpy(dets[nboxes].name,classesName[dets[nboxes].cl].c_str());
dets[nboxes].bbox.x = batchDetected[batch_num][i].x;
dets[nboxes].bbox.y = batchDetected[batch_num][i].y;
dets[nboxes].bbox.w = batchDetected[batch_num][i].w;
dets[nboxes].bbox.h = batchDetected[batch_num][i].h;
dets[nboxes].prob = batchDetected[batch_num][i].prob;
nboxes += 1;
}
}
if (pnum) *pnum = nboxes;
return dets;
}
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef DEMO_H
#define DEMO_H
#include <iostream>
#include <signal.h>
#include <stdlib.h> /* srand, rand */
#include <unistd.h>
#include <mutex>
#include <malloc.h>
#include "CenternetDetection.h"
#include "MobilenetDetection.h"
#include "Yolo3Detection.h"
#include "utils.h"
extern "C"
{
typedef struct {
int w;
int h;
int c;
float *data;
} image;
typedef struct {
float x, y, w, h;
}BOX;
typedef struct {
int cl;
BOX bbox;
float prob;
char name[20];
}detection;
tk::dnn::Yolo3Detection* load_network(char* net_cfg, int n_classes, int n_batch);
}
#endif /* DETECTIONNN_H*/
+2 -2
View File
@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) {
int n_batch = 1;
if(argc > 5)
n_batch = atoi(argv[5]);
bool show = true;
bool show = false;
if(argc > 6)
show = atoi(argv[6]);
float conf_thresh=0.3;
@@ -112,7 +112,7 @@ int main(int argc, char *argv[]) {
}
if(!frame.data)
break;
//inference
detNN->update(batch_dnn_input, n_batch);
detNN->draw(batch_frame);