More stable web-cam

This commit is contained in:
AlexeyAB
2018-03-21 00:40:01 +03:00
parent fe4f01b1d8
commit 101de2b07a
2 changed files with 26 additions and 8 deletions

View File

@ -64,6 +64,7 @@ void *fetch_in_thread(void *ptr)
in = get_image_from_stream_resize(cap, net.w, net.h, &in_img, use_webcam);
if(!in.data){
//error("Stream closed.");
printf("Stream closed.\n");
flag_exit = 1;
return;
}
@ -166,6 +167,8 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch
probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *));
for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float *)calloc(l.classes, sizeof(float *));
flag_exit = 0;
pthread_t fetch_thread;
pthread_t detect_thread;
@ -198,7 +201,7 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch
}
CvVideoWriter* output_video_writer = NULL; // cv::VideoWriter output_video;
if (out_filename)
if (out_filename && !flag_exit)
{
CvSize size;
size.width = det_img->width, size.height = det_img->height;
@ -212,7 +215,6 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch
//output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('X', 'V', 'I', 'D'), 25, size, 1);
//output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('W', 'M', 'V', '2'), 25, size, 1);
}
flag_exit = 0;
double before = get_wall_time();

View File

@ -197,16 +197,32 @@ void send_mjpeg(IplImage* ipl, int port, int timeout, int quality) {
CvCapture* get_capture_webcam(int index) {
CvCapture* cap = (CvCapture*)new cv::VideoCapture(index);
CvCapture* cap = NULL;
try {
cap = (CvCapture*)new cv::VideoCapture(index);
//((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_WIDTH, 1280);
//((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
}
catch (...) {
std::cout << " Error: Web-camera " << index << " can't be opened! \n";
}
return cap;
}
IplImage* get_webcam_frame(CvCapture *cap) {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
cv::Mat frame;
cpp_cap >> frame;
IplImage* src = cvCreateImage(cvSize(frame.cols, frame.rows), 8, frame.channels());
*src = frame;
IplImage* src = NULL;
try {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
cv::Mat frame;
if (cpp_cap.isOpened()) {
cpp_cap >> frame;
src = cvCreateImage(cvSize(frame.cols, frame.rows), 8, frame.channels());
*src = frame;
}
}
catch (...) {
std::cout << " Web-camera stoped! \n";
}
return src;
}