docker_api

This commit is contained in:
root
2021-06-11 06:24:36 +00:00
parent a672254faa
commit 961e1c236e
3 changed files with 35 additions and 84 deletions
View File
View File

Before

Width:  |  Height:  |  Size: 147 KiB

After

Width:  |  Height:  |  Size: 147 KiB

+35 -84
View File
@@ -5,8 +5,6 @@
#ifdef __linux__
#include <unistd.h>
#endif
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "stb_image.h"
#include <mutex>
#include "utils.h"
@@ -37,13 +35,6 @@ using namespace cv;
#include <iostream>
#include <string>
#include "image.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include<sys/socket.h> //socket
#include<sys/types.h>
#include<netinet/in.h>
image make_empty_image(int w, int h, int c)
{
image out;
@@ -97,7 +88,7 @@ cv::Mat image_to_mat(image img)
int height = img.h;
cv::Mat mat = cv::Mat(height, width, CV_8UC(channels));
int step = mat.step;
for (int y = 0; y < img.h; ++y) {
for (int x = 0; x < img.w; ++x) {
for (int c = 0; c < img.c; ++c) {
@@ -107,19 +98,24 @@ cv::Mat image_to_mat(image img)
}
}
return mat;
}
}
std::vector<std::string> classesNames;
image im;
cv::Mat frame;
cv::Mat gray;
int h=0;
int w=0;
int channels;
char ntype = 'y';
const char *config_filename = "../demo/config.yaml";
const char * net = "../demo/yolo4_fp32.rt";
// const char * img_path = "../demo/demo.jpg";
char * img_data;
bool show = false;
bool verbose;
int classes, map_points, map_levels;
int classes, map_points, map_levels ,len;
string ustring;
float map_step, IoU_thresh, conf_thresh;
tk::dnn::Yolo3Detection yolo;
// tk::dnn::CenternetDetection cnet;
// tk::dnn::MobilenetDetection mbnet;
tk::dnn::DetectionNN *detNN;
int n_classes = classes;
std::vector<tk::dnn::Frame> images;
@@ -145,48 +141,27 @@ string name_from_path(string path)
removePathAndExtension(net, net_name);
std::cout<<"Network: "<<net_name<<std::endl;
//open files (if needed)
//std::ofstream times, memory, coco_json;
int n_classes = classes;
// float conf_threshold=0.001;
detNN = &yolo;
detNN->init(net, n_classes, 1, conf_thresh);
//read images
// std::ifstream all_labels(labels_path);
// std::cout << timeSinceEpochMillisec() << std::endl;
std::string l_filename;
if(show)
cv::namedWindow("detection", cv::WINDOW_NORMAL);
return;}
// init_bag();
// int images_done;
// for (images_done=0 ; std::getline(all_labels, l_filename) && images_done < n_images ; ++images_done) {
// std::cout <<COL_ORANGEB<< "Images done:\t" << images_done<< "\n"<<COL_END;
void handler::handle_post(http_request request){
// init_bag();
// const char * img_path=
//tk::dnn::Frame f;
void handler::handle_post(http_request request){
BOOST_LOG_TRIVIAL(info) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << request.to_string();
map<utility::string_t, utility::string_t> http_get_vars = uri::split_query(request.request_uri().query());
map<utility::string_t, utility::string_t>::iterator it = http_get_vars.find("name");
// std::cout<<request<<"\n";
//If 'name' is not in the query.
int len;
// int len;
if(it == http_get_vars.end())
{
BOOST_LOG_TRIVIAL(error) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << "Image name not passed in query.";
request.reply(status_codes::UnprocessableEntity,"Please pass image name in the query.");
return;
}https://github.com/baggageai/baggageai-code-one.git
std::cout<<http_get_vars["name"]<<"\n";
// std::cout<<http_get_vars["name"]<<"\n";
string image_name = (string)http_get_vars["name"];
string ustring;
//reading binary data and storing it in a pointer
// std::vector<std::string> img_data=request.extract_vector();
// string ustring;
request.extract_vector().then([image_name, &ustring, &len](vector<unsigned char> v) {
ustring = {v.begin(),v.end()};
len = ustring.size();
@@ -194,45 +169,25 @@ string name_from_path(string path)
//printSize(ustring);
BOOST_LOG_TRIVIAL(info) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << "Detection Started";
unsigned char * sockData = (unsigned char *)ustring.c_str();
// std::cout<<ch<<"char"<<len<<"len"<<"\n";
// cv::Mat frame = cv::imread("demo.jpg", cv::IMREAD_COLOR);
int channels;
int h=0,w=0;
image im;
unsigned char *data = stbi_load_from_memory(sockData, len, &w, &h, &channels, 0);
im = load_image_file(data, channels, 0, 0, w, h);
// float *data = stbi_loadf_from_memory(ch, len, &w, &h, &channels, 0);
std::cout<<"imagedata"<<h<<"w"<<w<<"ch"<<channels<<"\n";
string outputPath = "/home/baggageai/files/build/testp.png";
// Write pictures
// stbi_write_png(outputPath.c_str(), w, h, channels, data, 0);
//cv::Mat frame=image_to_mat(data,h,w,channels);
// cv::Mat frame = cv::Mat(data,h, w, CV_16UC(channels));
// cv::imwrite("/home/baggageai/files/build/test.png", img);
// cv::Mat gray;
cv::Mat gray=image_to_mat(im);
cv::Mat frame;
cv::Mat in[] = {gray, gray,gray};
cv::merge(in, 3, frame);
cv::imwrite("/home/baggageai/files/build/test1.png", frame);
std::cout<<frame.channels();
// free(data);
//free(sockData);
gray=image_to_mat(im);
// free(im);
cv::Mat in[] = {gray, gray,gray};
cv::merge(in, 3, frame);
std::vector<cv::Mat> batch_frames;
batch_frames.push_back(frame);
int height = frame.rows;
int width = frame.cols;
std::cout<<height<<"width"<<width<<"\n";
// if(!frame.data)
// break;
std::vector<cv::Mat> batch_dnn_input;
batch_dnn_input.push_back(frame.clone());
std::cout<<"test1"<<"\n";
//inference
detected_bbox.clear();
detNN->update(batch_dnn_input,1);
detNN->draw(batch_frames);
detected_bbox = detNN->detected;
std::cout<<"test2"<<"\n";
try{
json::value response;
vector<json::value> jsonArray;
@@ -247,36 +202,32 @@ cv::merge(in, 3, frame);
b.h = d.h / height;
b.prob = d.prob;
b.cl = d.cl;
//f.det.push_back(b);
f.det.push_back(b);
json::value detection;
detection["label"] = json::value::number(b.cl);
detection["x"] = json::value::number(b.x);
detection["y"] = json::value::number(b.y);
detection["w"] = json::value::number(b.w);
detection["h"] = json::value::number(b.h);
detection["label"] = json::value::string(string("battery"));
detection["x"] = json::value::number(d.x);
detection["y"] = json::value::number(d.y);
detection["w"] = json::value::number(d.w);
detection["h"] = json::value::number(d.h);
detection["prob"] = json::value::number(b.prob);
jsonArray.push_back(detection);
std::cout<< d.cl << " "<< d.prob << " "<< b.x << " "<< b.y << " "<< b.w << " "<< b.h <<"\n";
if(show)// draw rectangle for detection
cv::rectangle(batch_frames[0], cv::Point(d.x, d.y), cv::Point(d.x + d.w, d.y + d.h), cv::Scalar(0, 0, 255), 2);
}
//images.push_back(f);
if(show){
cv::imshow("detection", batch_frames[0]);
cv::waitKey(0);
}
}
response["detections"] = json::value::array(jsonArray); //JSON Response
// free(jsonArray);
request.reply(status_codes::OK,response.serialize());
// free(detectboxes);
// free(detected_bbox);
BOOST_LOG_TRIVIAL(info) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << "Detection Completed and Response sent";
}
catch (exception const& e) {
BOOST_LOG_TRIVIAL(error) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << e.what();
request.reply(status_codes::BadRequest, e.what());
}
// free(data);
// free(sockData);
frame.release();
gray.release();
// std::cout << timeSinceEpochMillisec() << std::endl;
return ;
}