Add try-catch to the http_stream.cpp

pull/2352/head
AlexeyAB 6 years ago
parent c71354ab2e
commit bd91d0a908
  1. 155
      src/http_stream.cpp

@ -6,6 +6,13 @@
// on win, _WIN32 has to be defined, must link against ws2_32.lib (socks on linux are for free) // on win, _WIN32 has to be defined, must link against ws2_32.lib (socks on linux are for free)
// //
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using std::cerr;
using std::endl;
// //
// socket related abstractions: // socket related abstractions:
// //
@ -33,7 +40,7 @@ static int close_socket(SOCKET s) {
free(buf); free(buf);
int close_input = ::shutdown(s, 0); int close_input = ::shutdown(s, 0);
int result = ::closesocket(s); int result = ::closesocket(s);
printf("Close socket: out = %d, in = %d \n", close_output, close_input); cerr << "Close socket: out = " << close_output << ", in = " << close_input << " \n";
return result; return result;
} }
#else // nix #else // nix
@ -72,17 +79,11 @@ static int close_socket(SOCKET s) {
free(buf); free(buf);
int close_input = ::shutdown(s, 0); int close_input = ::shutdown(s, 0);
int result = close(s); int result = close(s);
printf("Close socket: out = %d, in = %d \n", close_output, close_input); std::cerr << "Close socket: out = " << close_output << ", in = " << close_input << " \n";
return result; return result;
} }
#endif // _WIN32 #endif // _WIN32
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using std::cerr;
using std::endl;
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp" #include "opencv2/highgui/highgui.hpp"
@ -243,7 +244,7 @@ public:
{ {
if (close_all_sockets) { if (close_all_sockets) {
int result = close_socket(s); int result = close_socket(s);
printf("MJPG_sender: close clinet: %d \n", result); cerr << "MJPG_sender: close clinet: " << result << " \n";
continue; continue;
} }
@ -262,7 +263,7 @@ public:
} }
if (close_all_sockets) { if (close_all_sockets) {
int result = close_socket(sock); int result = close_socket(sock);
printf("MJPG_sender: close acceptor: %d \n\n", result); cerr << "MJPG_sender: close acceptor: " << result << " \n\n";
} }
return true; return true;
} }
@ -271,10 +272,15 @@ public:
void send_mjpeg(IplImage* ipl, int port, int timeout, int quality) void send_mjpeg(IplImage* ipl, int port, int timeout, int quality)
{ {
static MJPG_sender wri(port, timeout, quality); try {
cv::Mat mat = cv::cvarrToMat(ipl); static MJPG_sender wri(port, timeout, quality);
wri.write(mat); cv::Mat mat = cv::cvarrToMat(ipl);
std::cout << " MJPEG-stream sent. \n"; wri.write(mat);
std::cout << " MJPEG-stream sent. \n";
}
catch (...) {
cerr << " Error in send_mjpeg() function \n";
}
} }
// ---------------------------------------- // ----------------------------------------
@ -436,14 +442,14 @@ public:
if (close_all_sockets) { if (close_all_sockets) {
int result = close_socket(s); int result = close_socket(s);
printf("JSON_sender: close clinet: %d \n", result); cerr << "JSON_sender: close clinet: " << result << " \n";
continue; continue;
} }
} }
} }
if (close_all_sockets) { if (close_all_sockets) {
int result = close_socket(sock); int result = close_socket(sock);
printf("JSON_sender: close acceptor: %d \n\n", result); cerr << "JSON_sender: close acceptor: " << result << " \n\n";
} }
return true; return true;
} }
@ -452,12 +458,17 @@ public:
void send_json(detection *dets, int nboxes, int classes, char **names, long long int frame_id, int port, int timeout) void send_json(detection *dets, int nboxes, int classes, char **names, long long int frame_id, int port, int timeout)
{ {
static JSON_sender js(port, timeout); try {
char *send_buf = detection_to_json(dets, nboxes, classes, names, frame_id, NULL); static JSON_sender js(port, timeout);
char *send_buf = detection_to_json(dets, nboxes, classes, names, frame_id, NULL);
js.write(send_buf); js.write(send_buf);
std::cout << " JSON-stream sent. \n"; std::cout << " JSON-stream sent. \n";
free(send_buf); free(send_buf);
}
catch (...) {
cerr << " Error in send_json() function \n";
}
} }
// ---------------------------------------- // ----------------------------------------
@ -468,7 +479,7 @@ CvCapture* get_capture_video_stream(char *path) {
cap = (CvCapture*)new cv::VideoCapture(path); cap = (CvCapture*)new cv::VideoCapture(path);
} }
catch (...) { catch (...) {
std::cout << " Error: video-stream " << path << " can't be opened! \n"; cerr << " Error: video-stream " << path << " can't be opened! \n";
} }
return cap; return cap;
} }
@ -482,7 +493,7 @@ CvCapture* get_capture_webcam(int index) {
//((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960); //((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
} }
catch (...) { catch (...) {
std::cout << " Error: Web-camera " << index << " can't be opened! \n"; cerr << " Error: Web-camera " << index << " can't be opened! \n";
} }
return cap; return cap;
} }
@ -520,7 +531,7 @@ int get_stream_fps_cpp(CvCapture *cap) {
#endif #endif
} }
catch (...) { catch (...) {
std::cout << " Can't get FPS of source videofile. For output video FPS = 25 by default. \n"; cerr << " Can't get FPS of source videofile. For output video FPS = 25 by default. \n";
} }
return fps; return fps;
} }
@ -533,62 +544,68 @@ image image_data_augmentation(IplImage* ipl, int w, int h,
int pleft, int ptop, int swidth, int sheight, int flip, int pleft, int ptop, int swidth, int sheight, int flip,
float jitter, float dhue, float dsat, float dexp) float jitter, float dhue, float dsat, float dexp)
{ {
cv::Mat img = cv::cvarrToMat(ipl); image out;
try {
cv::Mat img = cv::cvarrToMat(ipl);
// crop // crop
cv::Rect src_rect(pleft, ptop, swidth, sheight); cv::Rect src_rect(pleft, ptop, swidth, sheight);
cv::Rect img_rect(cv::Point2i(0, 0), img.size()); cv::Rect img_rect(cv::Point2i(0, 0), img.size());
cv::Rect new_src_rect = src_rect & img_rect; cv::Rect new_src_rect = src_rect & img_rect;
cv::Rect dst_rect(cv::Point2i(std::max<int>(0, -pleft), std::max<int>(0, -ptop)), new_src_rect.size()); cv::Rect dst_rect(cv::Point2i(std::max<int>(0, -pleft), std::max<int>(0, -ptop)), new_src_rect.size());
cv::Mat cropped(cv::Size(src_rect.width, src_rect.height), img.type()); cv::Mat cropped(cv::Size(src_rect.width, src_rect.height), img.type());
cropped.setTo(cv::Scalar::all(0)); cropped.setTo(cv::Scalar::all(0));
img(new_src_rect).copyTo(cropped(dst_rect)); img(new_src_rect).copyTo(cropped(dst_rect));
// resize // resize
cv::Mat sized; cv::Mat sized;
cv::resize(cropped, sized, cv::Size(w, h), 0, 0, INTER_LINEAR); cv::resize(cropped, sized, cv::Size(w, h), 0, 0, INTER_LINEAR);
// flip // flip
if (flip) { if (flip) {
cv::flip(sized, cropped, 1); // 0 - x-axis, 1 - y-axis, -1 - both axes (x & y) cv::flip(sized, cropped, 1); // 0 - x-axis, 1 - y-axis, -1 - both axes (x & y)
sized = cropped.clone(); sized = cropped.clone();
} }
// HSV augmentation
// CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
if (ipl->nChannels >= 3)
{
cv::Mat hsv_src;
cvtColor(sized, hsv_src, CV_BGR2HSV); // also BGR -> RGB
std::vector<cv::Mat> hsv; // HSV augmentation
cv::split(hsv_src, hsv); // CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
if (ipl->nChannels >= 3)
{
cv::Mat hsv_src;
cvtColor(sized, hsv_src, CV_BGR2HSV); // also BGR -> RGB
hsv[1] *= dsat; std::vector<cv::Mat> hsv;
hsv[2] *= dexp; cv::split(hsv_src, hsv);
hsv[0] += 179 * dhue;
cv::merge(hsv, hsv_src); hsv[1] *= dsat;
hsv[2] *= dexp;
hsv[0] += 179 * dhue;
cvtColor(hsv_src, sized, CV_HSV2RGB); // now RGB instead of BGR cv::merge(hsv, hsv_src);
}
else
{
sized *= dexp;
}
//std::stringstream window_name; cvtColor(hsv_src, sized, CV_HSV2RGB); // now RGB instead of BGR
//window_name << "augmentation - " << ipl; }
//cv::imshow(window_name.str(), sized); else
//cv::waitKey(0); {
sized *= dexp;
}
// Mat -> IplImage -> image //std::stringstream window_name;
IplImage src = sized; //window_name << "augmentation - " << ipl;
image out = ipl_to_image(&src); //cv::imshow(window_name.str(), sized);
//cv::waitKey(0);
// Mat -> IplImage -> image
IplImage src = sized;
out = ipl_to_image(&src);
}
catch (...) {
cerr << "OpenCV can't augment image: " << w << " x " << h << " \n";
out = ipl_to_image(ipl);
}
return out; return out;
} }
@ -603,7 +620,7 @@ image load_image_resize(char *filename, int w, int h, int c, image *im)
else if (c == 1) { flag = 0; img = cv::Mat(h, w, CV_8UC1); } else if (c == 1) { flag = 0; img = cv::Mat(h, w, CV_8UC1); }
else if (c == 3) { flag = 1; img = cv::Mat(h, w, CV_8UC3); } else if (c == 3) { flag = 1; img = cv::Mat(h, w, CV_8UC3); }
else { else {
fprintf(stderr, "OpenCV can't force load with %d channels\n", c); cerr << "OpenCV can't force load with " << c << " channels\n";
} }
//throw std::runtime_error("runtime_error"); //throw std::runtime_error("runtime_error");
cv::Mat loaded_image = cv::imread(filename, flag); cv::Mat loaded_image = cv::imread(filename, flag);
@ -617,7 +634,7 @@ image load_image_resize(char *filename, int w, int h, int c, image *im)
out = ipl_to_image(&tmp2); out = ipl_to_image(&tmp2);
} }
catch (...) { catch (...) {
fprintf(stderr, "OpenCV can't load image %s channels\n", filename); cerr << "OpenCV can't load image %s " << filename << " \n";
out = make_image(w, h, c); out = make_image(w, h, c);
*im = make_image(w, h, c); *im = make_image(w, h, c);
} }

Loading…
Cancel
Save