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
+2 -2
View File
@@ -56,7 +56,7 @@ find_package(yaml-cpp REQUIRED)
#-------------------------------------------------------------------------------
# Build Libraries
#-------------------------------------------------------------------------------
file(GLOB tkdnn_SRC "src/*.cpp")
file(GLOB tkdnn_SRC "src/*.cpp",src/*.c)
set(tkdnn_LIBS kernels ${CUDA_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES} ${CUDNN_LIBRARIES} ${OpenCV_LIBS} yaml-cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
@@ -85,7 +85,7 @@ set(Casablanca_LIBRARIES "-lboost_log -lboost_log_setup -lboost_thread -lboost_s
# If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know
# when to ask CMake to regenerate.
file(GLOB_RECURSE SOURCE_FILES "main.cpp" "handler.cpp" "src/*.cpp")
file(GLOB_RECURSE SOURCE_FILES "main.cpp" "handler.cpp" "src/*.cpp","src/*.c")
add_executable(baggageAPI ${SOURCE_FILES})
set(Casablanca_LIBRARIES "-lboost_log -lboost_log_setup -lboost_thread -lboost_system -lcrypto -lssl -lcpprest -lpthread" )
+248
View File
@@ -0,0 +1,248 @@
#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 <vector>
#include <random>
#include <climits>
#include <algorithm>
#include <functional>
#include <string>
#include <fstream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "Yolo3Detection.h"
//#include "CenternetDetection.h"
//#include "MobilenetDetection.h"
#include "evaluation.h"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include<sys/socket.h> //socket
#include<sys/types.h>
#include<netinet/in.h>
using namespace std;
using namespace cv;
#define PORT 8080
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 480
void error(const char *msg)
{
perror(msg);
exit(1);
} int sockfd, newsockfd, portno, n, imgSize, bytes=0, IM_HEIGHT, IM_WIDTH;;
socklen_t clilen;
char buffer[256];
// struct sockaddr_in serv_addr, cli_addr;
// sockfd=socket(AF_INET, SOCK_STREAM, 0);
cv::Mat img;
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;
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;
std::vector<tk::dnn::box> detected_bbox;
tk::dnn::Frame f;
void 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
std::string net_name;
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 infr(cv::Mat image){
cv::Mat frame = image;
cv::imwrite("/home/baggageai/files/build/test.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;
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;
// save detections labels
for(auto d:detected_bbox){
//convert detected bb in the same format as label
//<x_center>/<image_width> <y_center>/<image_width> <width>/<image_width> <height>/<image_width>
tk::dnn::BoundingBox b;
b.x = (d.x + d.w/2) / width;
b.y = (d.y + d.h/2) / height;
b.w = d.w / width;
b.h = d.h / height;
b.prob = d.prob;
b.cl = d.cl;
//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["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
// std::cout << timeSinceEpochMillisec() << std::endl;
return ;
}
int main()
{
// int sockfd, newsockfd, portno, n, imgSize, bytes=0, IM_HEIGHT, IM_WIDTH;;
// socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
//init_bag()
// cv::Mat img;
sockfd=socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0) error("ERROR opening socket");
bzero((char*)&serv_addr, sizeof(serv_addr));
portno = PORT;
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);
if(bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr))<0) error("ERROR on binding");
listen(sockfd,5);
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd<0) error("ERROR on accept");
uchar sock[3];
cout << sock <<endl;
cout << sock+3 <<endl;
// bzero(buffer,1024);
// n = read(newsockfd, buffer, 1023);
// if(n<0) error("ERROR reading from socket");
//printf("Here is the message: %s\n", buffer);
// n=write(newsockfd, "I got your message", 18);
// if(n<0) error("ERROR writing to socket");
bool running = true;
while(running)
{ std::cout<<"t"<<"\n";
IM_HEIGHT = FRAME_HEIGHT;
IM_WIDTH = FRAME_WIDTH;
img = Mat::zeros(FRAME_HEIGHT, FRAME_WIDTH, CV_8UC3);
imgSize = img.total()*img.elemSize();
uchar sockData[imgSize];
std::cout<<"t2"<<"\n";
for(int i=0;i<imgSize;i+=bytes)
if ((bytes=recv(newsockfd, sockData+i, imgSize-i,0))==-1) error("recv failed");
int ptr=0;
for(int i=0;i<img.rows;++i)
for(int j=0;j<img.cols;++j)
{
img.at<Vec3b>(i,j) = Vec3b(sockData[ptr+0],sockData[ptr+1],sockData[ptr+2]);
ptr=ptr+3;
}
std::cout<<"t3"<<"\n";
int height = img.cols;
std::cout<<height;
infr(img)
// namedWindow( "Server", CV_WINDOW_AUTOSIZE );// Create a window for display.
// imshow( "Server", img );
// char key = waitKey(30);
// running = key;
//esc
// if(key==27) running =false;
}
close(newsockfd);
close(sockfd);
return 0;
}
}
+1 -1
View File
@@ -27,5 +27,5 @@ COPY --chown=baggageai:baggageai . files/
#RUN aws s3 cp s3://dim-bai-s3-dev-developer-space/smiths_29_objects/libBaggageAI.so files/
WORKDIR /home/baggageai/files
#RUN chmod 777 run.sh
RUN chmod 777 run.sh
#ENTRYPOINT ["./run.sh"]
+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 ;
}
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -17,7 +17,7 @@ class handler
pplx::task<void>open(){return m_listener.open();}
pplx::task<void>close(){return m_listener.close();}
static void init_bag();
protected:
private:
+108
View File
@@ -0,0 +1,108 @@
#ifndef IMAGE_H
#define IMAGE_H
#include "darknet.h"
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <string.h>
#include <math.h>
//#include "image_opencv.h"
//#include "box.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
typedef struct {
int w;
int h;
int c;
float *data;
} image;
*/
float get_color(int c, int x, int max);
void flip_image(image a);
void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b);
void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b);
void draw_bbox(image a, box bbox, int w, float r, float g, float b);
void draw_label(image a, int r, int c, image label, const float *rgb);
void write_label(image a, int r, int c, image *characters, char *string, float *rgb);
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes);
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output);
image image_distance(image a, image b);
void scale_image(image m, float s);
// image crop_image(image im, int dx, int dy, int w, int h);
image random_crop_image(image im, int w, int h);
image random_augment_image(image im, float angle, float aspect, int low, int high, int size);
void random_distort_image(image im, float hue, float saturation, float exposure);
//LIB_API image resize_image(image im, int w, int h);
//LIB_API void copy_image_from_bytes(image im, char *pdata);
void fill_image(image m, float s);
void letterbox_image_into(image im, int w, int h, image boxed);
//LIB_API image letterbox_image(image im, int w, int h);
// image resize_min(image im, int min);
image resize_max(image im, int max);
void translate_image(image m, float s);
void normalize_image(image p);
image rotate_image(image m, float rad);
void rotate_image_cw(image im, int times);
void embed_image(image source, image dest, int dx, int dy);
void saturate_image(image im, float sat);
void exposure_image(image im, float sat);
void distort_image(image im, float hue, float sat, float val);
void saturate_exposure_image(image im, float sat, float exposure);
void hsv_to_rgb(image im);
//LIB_API void rgbgr_image(image im);
void constrain_image(image im);
void composite_3d(char *f1, char *f2, char *out, int delta);
int best_3d_shift_r(image a, image b, int min, int max);
image grayscale_image(image im);
image threshold_image(image im, float thresh);
image collapse_image_layers(image source, int border);
image collapse_images_horz(image *ims, int n);
image collapse_images_vert(image *ims, int n);
void show_image(image p, const char *name);
void show_image_normalized(image im, const char *name);
void save_image_png(image im, const char *name);
void save_image(image p, const char *name);
void show_images(image *ims, int n, char *window);
void show_image_layers(image p, char *name);
void show_image_collapsed(image p, char *name);
void print_image(image m);
//LIB_API image make_image(int w, int h, int c);
image make_random_image(int w, int h, int c);
image make_empty_image(int w, int h, int c);
image float_to_image_scaled(int w, int h, int c, float *data);
image float_to_image(int w, int h, int c, float *data);
image copy_image(image p);
void copy_image_inplace(image src, image dst);
image load_image(char *filename, int w, int h, int c);
image load_image_stb_resize(char *filename, int w, int h, int c);
image load_image_new(unsigned char *image_data, int len, int channels, int antiLog, int gray, int width, int height);
image load_image_file(unsigned char *image_data, int channels, int antilog, int gray, int width, int height);
//LIB_API image load_image_color(char *filename, int w, int h);
image **load_alphabet();
//float get_pixel(image m, int x, int y, int c);
//float get_pixel_extend(image m, int x, int y, int c);
//void set_pixel(image m, int x, int y, int c, float val);
//void add_pixel(image m, int x, int y, int c, float val);
float bilinear_interpolate(image im, float x, float y, int c);
image get_image_layer(image m, int l);
//LIB_API void free_image(image m);
void test_resize(char *filename);
#ifdef __cplusplus
}
#endif
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+1
View File
@@ -73,6 +73,7 @@ int main(int argc, char *argv[])
#endif
{
init_logging();
handler::init_bag();
utility::string_t port = U("8080");
if(argc == 2)
{
+1749
View File
File diff suppressed because it is too large Load Diff
+69
View File
@@ -0,0 +1,69 @@
cmake_minimum_required(VERSION 3.5)
set(PROJ_NAME tkDNN)
project (tkDNN)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
if(UNIX)
####
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -Wno-deprecated-declarations -Wno-unused-variable ")
endif()
if(WIN32)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "/O2 /FS /EHsc")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
#add extras for baggage
endif(WIN32)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/tkDNN)
# project specific flags
if(DEBUG)
add_definitions(-DDEBUG)
endif()
add_definitions(-DTKDNN_PATH="${CMAKE_CURRENT_SOURCE_DIR}")
#-------------------------------------------------------------------------------
# CUDA
#-------------------------------------------------------------------
#-------------------------------------------------------------------------------
# External Libraries
#------------------------------------------------------------------------------
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOPENCV")
# gives pr
#-------------------------------------------------------------------------------
# Build Libraries
#----------------------------------------------------------------------------
file(GLOB_RECURSE SOURCE_FILES "client_img.cpp")
add_executable(client ${SOURCE_FILES})
#set(Casablanca_LIBRARIES "-lboost_log -lboost_log_setup -lboost_thread -lboost_system -lcrypto -lssl -lcpprest -lpthread" )
set(tkdnn_LIBS ${OpenCV_LIBS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# Link BaggageAI library' include folder.
include_directories(${OPENCV_INCLUDE_DIRS} ${CMAKE_CXX_FLAGS})
# Define BaggageAI library' shared library.
#add_library(${BAGGAGEAI_LIB_NAME} SHARED IMPORTED)
# Set a path to BaggageAI library' shared library
#set_property(TARGET ${BAGGAGEAI_LIB_NAME} PROPERTY IMPORTED_LOCATION "${BAGGAGEAI_PATH}/libBaggageAI.so")
# Link all libraries together.
target_link_libraries(client ${tkdnn_LIBS})
#static
#add_executable(demo demo/inf.cpp)
#target_link_libraries(demo tkDNN)
#-------------------------------------------------------------------------------
# Install
#-------------------------------------------------------------------------------
#if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
# set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install"
# CACHE PATH "default install path" FORCE)
#endif()
message("install dir:" ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" # source directory
DESTINATION "share/tkDNN/cmake/" # target directory
)
+13
View File
@@ -0,0 +1,13 @@
DEBUG= -g
OPENCV= -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_video
PKG_CONF_CFLAG= 'pkg-config --cflags opencv --libs opencv'
PKGCONF_LIBS= 'pkg-config --libs opencv'
client:
clear
g++ -std=c++11 $(PKG_CONF_CFLAG) -o client client.cpp $(PKGCONF_LIBS)
server:
clear
g++ -std=c++11 $(PKG_CONF_CFLAG) -o server server.cpp $(PKGCONF_LIBS)
+89
View File
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define LOCALHOST "127.0.0.1"
#define PORT 8080
#define FRAME_WIDTH 608
#define FRAME_HEIGHT 608
using namespace std;
using namespace cv;
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main()
{
int sockfd, portno, n, imgSize, IM_HEIGHT, IM_WIDTH;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
Mat cameraFeed;
portno = PORT;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
server = gethostbyname(LOCALHOST);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
// VideoCapture capture;
// capture.open(0);
// while(true)
// {
/* store image to matrix && test frame */
// cv::Mat frame=cv::imread(demo.jpg);
cameraFeed = cv::imread("demo.jpg", cv::IMREAD_COLOR);
int height = cameraFeed.rows;
int width = cameraFeed.cols;
std::cout<<width;
// Mat cropped = Mat(cameraFeed, Rect(width/2 - width/7,
// height/2 - height/9,
// 2*width/7, 2*height/7));
// cameraFeed = frame;
IM_HEIGHT = FRAME_HEIGHT;
IM_WIDTH = FRAME_WIDTH;
resize(cameraFeed, cameraFeed, Size( IM_WIDTH , IM_HEIGHT ));
imgSize=cameraFeed.total()*cameraFeed.elemSize();
n = send(sockfd, cameraFeed.data, imgSize, 0);
std::cout<<n;
if (n < 0) error("ERROR writing to socket");
//}
close(sockfd);
return 0;
}
+102
View File
@@ -0,0 +1,102 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include<sys/socket.h> //socket
#include<sys/types.h>
#include<netinet/in.h>
using namespace std;
using namespace cv;
#define PORT 7200
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 480
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main()
{
int sockfd, newsockfd, portno, n, imgSize, bytes=0, IM_HEIGHT, IM_WIDTH;;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
Mat img;
sockfd=socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0) error("ERROR opening socket");
bzero((char*)&serv_addr, sizeof(serv_addr));
portno = PORT;
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);
if(bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr))<0) error("ERROR on binding");
listen(sockfd,5);
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd<0) error("ERROR on accept");
uchar sock[3];
cout << sock <<endl;
cout << sock+3 <<endl;
// bzero(buffer,1024);
// n = read(newsockfd, buffer, 1023);
// if(n<0) error("ERROR reading from socket");
//printf("Here is the message: %s\n", buffer);
// n=write(newsockfd, "I got your message", 18);
// if(n<0) error("ERROR writing to socket");
bool running = true;
while(running)
{ std::cout<<"t"<<"\n";
IM_HEIGHT = FRAME_HEIGHT;
IM_WIDTH = FRAME_WIDTH;
img = Mat::zeros(FRAME_HEIGHT, FRAME_WIDTH, CV_8UC3);
imgSize = img.total()*img.elemSize();
uchar sockData[imgSize];
std::cout<<"t2"<<"\n";
for(int i=0;i<imgSize;i+=bytes)
if ((bytes=recv(newsockfd, sockData+i, imgSize-i,0))==-1) error("recv failed");
int ptr=0;
for(int i=0;i<img.rows;++i)
for(int j=0;j<img.cols;++j)
{
img.at<Vec3b>(i,j) = Vec3b(sockData[ptr+0],sockData[ptr+1],sockData[ptr+2]);
ptr=ptr+3;
}
std::cout<<"t3"<<"\n";
int height = img.cols;
std::cout<<height;
// namedWindow( "Server", CV_WINDOW_AUTOSIZE );// Create a window for display.
// imshow( "Server", img );
char key = waitKey(30);
running = key;
//esc
if(key==27) running =false;
}
close(newsockfd);
close(sockfd);
return 0;
}