How to take an snapshot from a ethernet cam?
Hello everybody,
I've got a code where I show a video from a camera, but now I want to take a snapshot when a key is pressed, but I don't know exactly what function I need to use and how. Could anyone help me please?
Thanks. I post my code.
int main( ) {
VideoCapture vcap;
const std::string VideoStreamAddress ="rtsp://192.168.1.10/MediaInput/mpeg4";
if (!vcap.open(VideoStreamAddress)) {
std::cout << "Error abriendo stream de video o archivo" << std::endl;
return 0;
}
while (true) {
if (!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
imshow("original", image);
if (waitKey(1) >= 0){
//TAKE A PHOTO
TakePhoto();
}
}
return 0;
}
Hi again, The code to take snapshots it works fine, I can take a photo when a key is pressed, but now I'm trying to recognize text in that photo using tesseract engine and I'm having an error like this:
Error: Illegal min or max specification! signal_termination_handler:Error:Signal_termination_handler called:Code 5002
I've only just added the code for text recognition within the waitkey if as follows:
if (waitKey(10) >= 0){
imwrite( format("foto_%3d.png",cuenta++), image); //take snapshot when key is pressed
// initilize tesseract OCR engine
tesseract::TessBaseAPI *myOCR = new tesseract::TessBaseAPI();
if (myOCR->Init(NULL, "spa")) {
fprintf( stderr, " Could not intialize tesseract.\n" );
exit(1);
}
// read image
namedWindow("tesseract-opencv", 0);
Mat imagen = imread(format("/home/adrian/workspace/foto_%3d.png",cuenta), 0);
// set region of interest (ROI), i.e. regions that contain text
Rect text1ROI(162, 110, 157 , 129);
// recognize text
myOCR->TesseractRect( imagen.data, 1, imagen.step1(), text1ROI.x, text1ROI.y, text1ROI.width, text1ROI.height);
const char *text1 = myOCR->GetUTF8Text();
// remove "newline"
string t1(text1);
t1.erase( std::remove(t1.begin(), t1.end(), '\n'), t1.end() );
// print found text
printf("text: \n");
printf( "%s",t1.c_str() );
printf("\n");
}
Any idea of that error????
Thanks
// have a counter var above the while loop:
int count = 0;
// then:
if ( waitKey(10) >=0 ) { imwrite( format("shot_%03d.png", count++), image); }
Thank you berak! It works fine!
[concerning your edit:]
you don't have to read it from disk again:
Mat imagen = imread(format("/home/adrian/workspace/foto_%3d.png",cuenta), 0);
cuenta got increased above, right ? so it's no more the same number... and imagen will most likely be empty ( it does not exist )
just convert the original image to gray:
`Mat imagen; cvtConvert(image,imagen,CV_BGR2GRAY);
and use that instead.
the actual error does not seem to come from opencv, more from tesseract, so please check the args here carefully:
myOCR->TesseractRect( imagen.data, 1, imagen.step1(), text1ROI.x, text1ROI.y,
I know cuenta is not the same value now, I have changed that just after post the code here.