memory-fix

This commit is contained in:
mohit
2021-06-24 20:08:33 +05:30
parent 961e1c236e
commit ed41be6358
12 changed files with 162 additions and 485 deletions
+15
View File
@@ -0,0 +1,15 @@
classes1 : 13 #number of classes
conf_thresh1 : 0.3 #threshold on the condifence of the bbox
net1 : ../demo/yolo4x_fp16.rt
classes2 : 13 #number of classes
conf_thresh2 : 0.3 #threshold on the condifence of the bbox
net2 : ../demo/yolo4x_fp16.rt
classes3 : 13 #number of classes
conf_thresh3 : 0.3 #threshold on the condifence of the bbox
net3 : ../demo/yolo4x_fp16.rt
classes4 : 13 #number of classes
conf_thresh4 : 0.3 #threshold on the condifence of the bbox
net4 : ../demo/yolo4x_fp16.rt
classes5 : 13 #number of classes
conf_thresh5 : 0.3 #threshold on the condifence of the bbox
net5 : ../demo/yolo4x_fp16.rt
View File

Before

Width:  |  Height:  |  Size: 147 KiB

After

Width:  |  Height:  |  Size: 147 KiB

-7
View File
@@ -1,7 +0,0 @@
classes : 13 #number of classes
map_points : 0 #number of recall points (0 for all, 101 for COCO, 11 PascalVOC)
map_levels : 10 #number of IoU step for the AP
map_step : 0.05 #step of IoU
IoU_thresh : 0.5 #starting IoU threshold
conf_thresh : 0.001 #threshold on the condifence of the bbox
verbose : false #print on screen information
+123 -61
View File
@@ -26,6 +26,7 @@
//#include "CenternetDetection.h"
//#include "MobilenetDetection.h"
#include "evaluation.h"
#include "tkdnn.h"
#include <chrono>
#include <cstdint>
#include <iostream>
@@ -35,6 +36,12 @@ using namespace cv;
#include <iostream>
#include <string>
#include "image.h"
void free_image(image m)
{
if(m.data){
free(m.data);
}
}
image make_empty_image(int w, int h, int c)
{
image out;
@@ -106,21 +113,38 @@ cv::Mat image_to_mat(image img)
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 *config_filename = "../config/config.yaml";
const char * net1="../config/yolo4x_fp16.rt";
const char * net2="../config/yolo4x_fp16.rt";
const char * net3="../config/yolo4x_fp16.rt";
const char * net4="../config/yolo4x_fp16.rt";
const char * net5="../config/yolo4x_fp16.rt";
int classes1 , classes2 , classes3 , classes4 , classes5,len;
char * img_data;
bool show = false;
bool verbose;
int classes, map_points, map_levels ,len;
string ustring;
float map_step, IoU_thresh, conf_thresh;
tk::dnn::Yolo3Detection yolo;
tk::dnn::DetectionNN *detNN;
int n_classes = classes;
float conf_thresh1 , conf_thresh2 , conf_thresh3 , conf_thresh4 , conf_thresh5;
tk::dnn::Yolo3Detection yolo1;
tk::dnn::DetectionNN *detNN1;
tk::dnn::Yolo3Detection yolo2;
tk::dnn::DetectionNN *detNN2;
tk::dnn::Yolo3Detection yolo3;
tk::dnn::DetectionNN *detNN3;
tk::dnn::Yolo3Detection yolo4;
tk::dnn::DetectionNN *detNN4;
unsigned char * sockData;
std::vector<cv::Mat> batch_frames;
std::vector<cv::Mat> batch_dnn_input;
std::vector<std::string> classesNames1;
std::vector<std::string> classesNames2;
std::vector<std::string> classesNames3;
std::vector<std::string> classesNames4;
std::vector<tk::dnn::Frame> images;
std::vector<tk::dnn::box> detected_bbox;
tk::dnn::Frame f;
std::vector<tk::dnn::box> detected_bbox1;
std::vector<tk::dnn::box> detected_bbox2;
std::vector<tk::dnn::box> detected_bbox3;
std::vector<tk::dnn::box> detected_bbox4;
//read parametersi
handler::handler(utility::string_t url):m_listener(url)
{
@@ -132,18 +156,21 @@ string name_from_path(string path)
{
return path.substr(path.find_last_of("/\\")+1);
}
void handler::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, classes1,conf_thresh1, classes2,conf_thresh2
, classes3,conf_thresh3, classes4,conf_thresh4,classes5,conf_thresh5);
//extract network name from rt path
std::string net_name;
removePathAndExtension(net, net_name);
std::cout<<"Network: "<<net_name<<std::endl;
int n_classes = classes;
detNN = &yolo;
detNN->init(net, n_classes, 1, conf_thresh);
detNN1 = &yolo1;
detNN1->init(net1, classes1, 1, conf_thresh1);
classesNames1=detNN1-> classesNames;
detNN2 = &yolo2;
detNN2->init(net2, classes2, 1, conf_thresh2);
classesNames2=detNN2-> classesNames;
detNN3 = &yolo3;
detNN3->init(net3, classes3, 1, conf_thresh4);
classesNames3=detNN3-> classesNames;
detNN4 = &yolo4;
detNN4->init(net4, classes4, 1, conf_thresh4);
classesNames4=detNN4-> classesNames;
return;}
@@ -168,52 +195,89 @@ string name_from_path(string path)
}).wait();
//printSize(ustring);
BOOST_LOG_TRIVIAL(info) << "[" << name_from_path(string(__FILE__)) << " " << __LINE__ << "] " << "Detection Started";
unsigned char * sockData = (unsigned char *)ustring.c_str();
unsigned char *data = stbi_load_from_memory(sockData, len, &w, &h, &channels, 0);
im = load_image_file(data, channels, 0, 0, w, h);
// free(data);
//free(sockData);
unsigned char *idata;
sockData = (unsigned char *)ustring.c_str();
idata = stbi_load_from_memory(sockData, len, &w, &h, &channels, 0);
im = load_image_file(idata, channels, 0, 0, w, h);
batch_dnn_input.clear();
batch_frames.clear();
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::vector<cv::Mat> batch_dnn_input;
batch_dnn_input.push_back(frame.clone());
//inference
detected_bbox.clear();
detNN->update(batch_dnn_input,1);
detNN->draw(batch_frames);
detected_bbox = detNN->detected;
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::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);
detected_bbox1.clear();
detNN1->update(batch_dnn_input,1);
std::cout<<batch_dnn_input.rows<<" "<<batch_dnn_input.cols<<"\n";
detected_bbox1 = detNN1->detected;
for(auto d1:detected_bbox1){
json::value detection;
std::cout<<"1"<<" "<< d1.cl << " "<< d1.prob << " "<< d1.x << " "<< d1.y << " "<< d1.w << " "<< d1.h <<"\n";
detection["label"] = json::value::string(classesNames1[d1.cl]);
detection["x"] = json::value::number(d1.x);
detection["y"] = json::value::number(d1.y);
detection["w"] = json::value::number(d1.w);
detection["h"] = json::value::number(d1.h);
detection["prob"] = json::value::number(d1.prob);
jsonArray.push_back(detection);
std::cout<< d.cl << " "<< d.prob << " "<< b.x << " "<< b.y << " "<< b.w << " "<< b.h <<"\n";
}
detected_bbox2.clear();
detNN2->update(batch_dnn_input,1);
std::cout<<batch_dnn_input.rows<<" "<<batch_dnn_input.cols<<"\n";
detected_bbox2 = detNN2->detected;
for(auto d2:detected_bbox2){
std::cout<<"2"<<" "<< d2.cl << " "<< d2.prob << " "<< d2.x << " "<< d2.y << " "<< d2.w << " "<< d2.h <<"\n";
json::value detection;
detection["label"] = json::value::string(classesNames2[d2.cl]);
detection["x"] = json::value::number(d2.x);
detection["y"] = json::value::number(d2.y);
detection["w"] = json::value::number(d2.w);
detection["h"] = json::value::number(d2.h);
detection["prob"] = json::value::number(d2.prob);
jsonArray.push_back(detection);
}
detected_bbox3.clear();
detNN3->update(batch_dnn_input,1);
detected_bbox3 = detNN3->detected;
for(auto d3:detected_bbox3){
std::cout<< "3"<<" "<<d3.cl << " "<< d3.prob << " "<< d3.x << " "<< d3.y << " "<< d3.w << " "<< d3.h <<"\n";
json::value detection;
detection["label"] = json::value::string(classesNames3[d3.cl]);
detection["x"] = json::value::number(d3.x);
detection["y"] = json::value::number(d3.y);
detection["w"] = json::value::number(d3.w);
detection["h"] = json::value::number(d3.h);
detection["prob"] = json::value::number(d3.prob);
jsonArray.push_back(detection);
}
detected_bbox4.clear();
detNN4->update(batch_dnn_input,1);
detected_bbox4 = detNN4->detected;
for(auto d4:detected_bbox4){
std::cout<<"4"<<" "<<d4.cl<< " "<< d4.prob << " "<< d4.x << " "<< d4.y << " "<< d4.w << " "<< d4.h <<"\n";
json::value detection;
detection["label"] = json::value::string(classesNames4[d4.cl]);
detection["x"] = json::value::number(d4.x);
detection["y"] = json::value::number(d4.y);
detection["w"] = json::value::number(d4.w);
detection["h"] = json::value::number(d4.h);
detection["prob"] = json::value::number(d4.prob);
jsonArray.push_back(detection);
}
try{
response["detections"] = json::value::array(jsonArray); //JSON Response
// free(jsonArray);
request.reply(status_codes::OK,response.serialize());
@@ -224,11 +288,9 @@ string name_from_path(string path)
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;
free(idata);
free_image(im);
return ;
}
+7 -3
View File
@@ -31,9 +31,12 @@ struct PR
void print();
};
void readmAPParams( const char* config_filename, int& classes, int& map_points,
int& map_levels, float& map_step, float& IoU_thresh,
float& conf_thresh, bool& verbose);
void readmAPParams( const char* config_filename, int& classes1,float& conf_thresh1
, int& classes2,float& conf_thresh2
, int& classes3,float& conf_thresh3
, int& classes4,float& conf_thresh4
, int& classes5,float& conf_thresh5
);
/**
* This method computes the mean Average Precision for a set of detections and
@@ -114,3 +117,4 @@ void printJsonCOCOFormat(std::ofstream *out_file, const std::string image_path,
}}
#endif /*EVALUATION_H*/
-130
View File
@@ -1,130 +0,0 @@
#include <iostream>
#include <signal.h>
#include <stdlib.h> /* srand, rand */
#ifdef __linux__
#include <unistd.h>
#endif
#include <mutex>
#include "utils.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>
uint64_t timeSinceEpochMillisec() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
int baggage() {
std::cout << timeSinceEpochMillisec() << std::endl;
char ntype = 'y';
const char *config_filename = "../demo/config.yaml";
const char * net = "../demo/yolo4_fp32.rt";
const char * img_path = "../demo/demo.jpg";
bool show = false;
bool verbose;
int classes, map_points, map_levels;
float map_step, IoU_thresh, conf_thresh;
//read parameters
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;
// instantiate detector
tk::dnn::Yolo3Detection yolo;
// tk::dnn::CenternetDetection cnet;
// tk::dnn::MobilenetDetection mbnet;
tk::dnn::DetectionNN *detNN;
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;
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);
// 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;
tk::dnn::Frame f;
// f.lFilename = l_filename;
// f.iFilename = l_filename;
//convertFilename(f.iFilename, "labels", "images", ".txt", ".jpg");
// read frame
//if(!fileExist(f.iFilename.c_str()))
// FatalError("Wrong image file path.");
cv::Mat frame = cv::imread(img_path, cv::IMREAD_COLOR);
std::vector<cv::Mat> batch_frames;
batch_frames.push_back(frame);
int height = frame.rows;
int width = frame.cols;
// 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";
// 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);
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);
}
std::cout << timeSinceEpochMillisec() << std::endl;
return 0;
}
+1
View File
@@ -118,6 +118,7 @@ void Yolo3Detection::postprocess(const int bi, const bool mAP){
float x1 = (b.x+b.w/2.);
float y0 = (b.y-b.h/2.);
float y1 = (b.y+b.h/2.);
std::cout<<b.x<<"\n";
// convert to image coords
x0 = x_ratio*x0;
+16 -11
View File
@@ -1,6 +1,5 @@
#include "evaluation.h"
#include <fstream>
namespace tk { namespace dnn {
void Frame::print() const{
@@ -16,17 +15,23 @@ void PR::print(){
std::cout<<"precision: "<<precision<<" recall: "<<recall<<" tp: "<<tp<<" fp:"<<fp<<" fn:"<<fn<<std::endl;
}
void readmAPParams( const char* config_filename, int& classes, int& map_points,
int& map_levels, float& map_step, float& IoU_thresh,
float& conf_thresh, bool& verbose) {
void readmAPParams( const char* config_filename, int& classes1,float& conf_thresh1
, int& classes2,float& conf_thresh2
, int& classes3,float& conf_thresh3
, int& classes4,float& conf_thresh4
, int& classes5,float& conf_thresh5
) {
YAML::Node config = YAML::LoadFile(config_filename);
classes = config["classes"].as<int>();
map_points = config["map_points"].as<int>();
map_levels = config["map_levels"].as<int>();
map_step = config["map_step"].as<float>();
IoU_thresh = config["IoU_thresh"].as<float>();
conf_thresh = config["conf_thresh"].as<float>();
verbose = config["verbose"].as<bool>();
classes1 = config["classes1"].as<int>();
conf_thresh1 = config["conf_thresh1"].as<float>();
classes2 = config["classes2"].as<int>();
conf_thresh2 = config["conf_thresh2"].as<float>();
classes3 = config["classes3"].as<int>();
conf_thresh3 = config["conf_thresh3"].as<float>();
classes4 = config["classes4"].as<int>();
conf_thresh4 = config["conf_thresh4"].as<float>();
classes5 = config["classes5"].as<int>();
conf_thresh5 = config["conf_thresh5"].as<float>();
}
/* Credits to https://github.com/AlexeyAB/darknet/blob/master/src/detector.c*/
-69
View File
@@ -1,69 +0,0 @@
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
@@ -1,13 +0,0 @@
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
@@ -1,89 +0,0 @@
#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
@@ -1,102 +0,0 @@
#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;
}