Add try-catch to the http_stream.cpp

pull/2352/head
AlexeyAB 6 years ago
parent c71354ab2e
commit bd91d0a908
  1. 55
      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)
//
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using std::cerr;
using std::endl;
//
// socket related abstractions:
//
@ -33,7 +40,7 @@ static int close_socket(SOCKET s) {
free(buf);
int close_input = ::shutdown(s, 0);
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;
}
#else // nix
@ -72,17 +79,11 @@ static int close_socket(SOCKET s) {
free(buf);
int close_input = ::shutdown(s, 0);
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;
}
#endif // _WIN32
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using std::cerr;
using std::endl;
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
@ -243,7 +244,7 @@ public:
{
if (close_all_sockets) {
int result = close_socket(s);
printf("MJPG_sender: close clinet: %d \n", result);
cerr << "MJPG_sender: close clinet: " << result << " \n";
continue;
}
@ -262,7 +263,7 @@ public:
}
if (close_all_sockets) {
int result = close_socket(sock);
printf("MJPG_sender: close acceptor: %d \n\n", result);
cerr << "MJPG_sender: close acceptor: " << result << " \n\n";
}
return true;
}
@ -271,11 +272,16 @@ public:
void send_mjpeg(IplImage* ipl, int port, int timeout, int quality)
{
try {
static MJPG_sender wri(port, timeout, quality);
cv::Mat mat = cv::cvarrToMat(ipl);
wri.write(mat);
std::cout << " MJPEG-stream sent. \n";
}
catch (...) {
cerr << " Error in send_mjpeg() function \n";
}
}
// ----------------------------------------
class JSON_sender
@ -436,14 +442,14 @@ public:
if (close_all_sockets) {
int result = close_socket(s);
printf("JSON_sender: close clinet: %d \n", result);
cerr << "JSON_sender: close clinet: " << result << " \n";
continue;
}
}
}
if (close_all_sockets) {
int result = close_socket(sock);
printf("JSON_sender: close acceptor: %d \n\n", result);
cerr << "JSON_sender: close acceptor: " << result << " \n\n";
}
return true;
}
@ -452,6 +458,7 @@ public:
void send_json(detection *dets, int nboxes, int classes, char **names, long long int frame_id, int port, int timeout)
{
try {
static JSON_sender js(port, timeout);
char *send_buf = detection_to_json(dets, nboxes, classes, names, frame_id, NULL);
@ -459,6 +466,10 @@ void send_json(detection *dets, int nboxes, int classes, char **names, long long
std::cout << " JSON-stream sent. \n";
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);
}
catch (...) {
std::cout << " Error: video-stream " << path << " can't be opened! \n";
cerr << " Error: video-stream " << path << " can't be opened! \n";
}
return cap;
}
@ -482,7 +493,7 @@ CvCapture* get_capture_webcam(int index) {
//((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
}
catch (...) {
std::cout << " Error: Web-camera " << index << " can't be opened! \n";
cerr << " Error: Web-camera " << index << " can't be opened! \n";
}
return cap;
}
@ -520,7 +531,7 @@ int get_stream_fps_cpp(CvCapture *cap) {
#endif
}
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;
}
@ -533,6 +544,8 @@ image image_data_augmentation(IplImage* ipl, int w, int h,
int pleft, int ptop, int swidth, int sheight, int flip,
float jitter, float dhue, float dsat, float dexp)
{
image out;
try {
cv::Mat img = cv::cvarrToMat(ipl);
// crop
@ -587,8 +600,12 @@ image image_data_augmentation(IplImage* ipl, int w, int h,
// Mat -> IplImage -> image
IplImage src = sized;
image out = ipl_to_image(&src);
out = ipl_to_image(&src);
}
catch (...) {
cerr << "OpenCV can't augment image: " << w << " x " << h << " \n";
out = ipl_to_image(ipl);
}
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 == 3) { flag = 1; img = cv::Mat(h, w, CV_8UC3); }
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");
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);
}
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);
*im = make_image(w, h, c);
}

Loading…
Cancel
Save