dockerapi

This commit is contained in:
root
2021-06-09 08:27:32 +00:00
parent bd0c692628
commit 097c38a109
15 changed files with 12408 additions and 27 deletions
+115 -23
View File
@@ -1,11 +1,13 @@
#define STB_IMAGE_IMPLEMENTATION
#include <iostream>
#include <signal.h>
#include <stdlib.h> /* srand, rand */
#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"
#include "baggageDetect.hpp"
@@ -30,12 +32,87 @@
#include <cstdint>
#include <iostream>
using namespace std;
using namespace cv;
#include <fstream>
#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;
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*)calloc(h * w * c, sizeof(float));
return out;
}
int check_mistakes = 0;
image load_image_file(unsigned char *image_data, int channels, int antilog, int gray, int width, int height)
{
int w, h, c;
unsigned char *data = image_data;
w = width;
h = height;
c = channels;
if (!image_data) {
if (check_mistakes) getchar();
return make_image(10, 10, 3);
}
if (channels) c = channels;
int i,j,k;
image im = make_image(w, h, 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)image_data[src_index]/255.;
}
}
}
//free(data);
return im;
}
cv::Mat image_to_mat(image img)
{
int channels = img.c;
int width = img.w;
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) {
float val = img.data[c*img.h*img.w + y*img.w + x];
mat.data[y*step + x*img.c + c] = (unsigned char)(val * 255);
}
}
}
return mat;
}
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_path;
char * img_data;
bool show = false;
bool verbose;
int classes, map_points, map_levels;
@@ -59,8 +136,8 @@ string name_from_path(string path)
{
return path.substr(path.find_last_of("/\\")+1);
}
void init_bag(){//tk::dnn::readmAPParams(config_filename, classes, map_points, map_levels, map_step,
//IoU_thresh, conf_thresh, verbose);
void handler::init_bag(){tk::dnn::readmAPParams(config_filename, classes, map_points, map_levels, map_step,
IoU_thresh, conf_thresh, verbose);
//extract network name from rt path
@@ -79,19 +156,16 @@ string name_from_path(string path)
// std::ifstream all_labels(labels_path);
// std::cout << timeSinceEpochMillisec() << std::endl;
std::string l_filename;
//std::vector<tk::dnn::Frame> images;
//std::vector<tk::dnn::box> detected_bbox;
std::cout<<"Reading groundtruth and generating detections"<<std::endl;
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();
// init_bag();
// const char * img_path=
//tk::dnn::Frame f;
@@ -107,30 +181,47 @@ string name_from_path(string path)
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";
string image_name = (string)http_get_vars["name"];
string ustring;
string ustring;
//reading binary data and storing it in a pointer
// std::vector<std::string> img_data=request.extract_vector();
request.extract_vector().then([image_name, &ustring, &len](vector<unsigned char> v) {
ustring = {v.begin(),v.end()};
len = ustring.size();
}).wait();
//printSize(ustring);
BOOST_LOG_TRIVIAL(info) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << "Detection Started";
// img_path=(unsigned char *)ustring.c_str();
//reading binary data and storing it in a pointer
// std::string body = request.extract_string().get();
//string img_data= (string)http_get_vars[:];
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);
std::cout<<ustring<<"hi\n";
cv::Mat frame = cv::imread(ustring, cv::IMREAD_COLOR);
// cv::Mat frame=cv::imdecode((unsigned char *)ustring.c_str())
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();
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;
@@ -189,3 +280,4 @@ string name_from_path(string path)
// std::cout << timeSinceEpochMillisec() << std::endl;
return ;
}