1

I am trying to save images from a webcam when I press a key but is not working. Here I attach my code :

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
i=0

def repeat():
 global capture 
 global camera_index
 frame = cv.QueryFrame(capture)
 cv.ShowImage("w1", frame)
 c = cv.WaitKey(25)
 if(c=="n"): 
  cv.SaveImage("f"+str(i)+".jpg",frame) 
  i=i+1
while True:
    repeat()
3
  • Is is not creating the file or is the file no good? In the case of the former try adding a `print c, "Was Pressed!" before the if to see what you get! Commented Oct 18, 2013 at 11:41
  • Ok is solved. The problem was with WaitKey, but I have changed the condition: c = cv.WaitKey(25) if(c!=-1): cv.SaveImage("f"+str(i)+".jpg",frame) i=i+1 Commented Oct 18, 2013 at 11:44
  • Added as an answer to record this. Commented Oct 18, 2013 at 11:54

3 Answers 3

2

cv.WaitKey() does not return the key but does give -1 on a time out.

Solved with:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
i=0

def repeat():
 global capture 
 global camera_index
 frame = cv.QueryFrame(capture)
 cv.ShowImage("w1", frame)
 c = cv.WaitKey(25)
 if (c != -1): 
  cv.SaveImage("f"+str(i)+".jpg",frame) 
  i=i+1
while True:
    repeat()
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply press 's', to save image. following is the example in C++.

Do simply like this

char ch =  cvWaitKey(25);  // Wait for 25 ms for user to hit any key

  // Save image if s was keyboard
  if(ch=='s')
  {
     cvSaveImage(path,small);
  }
  if(ch==27) break;  // If Escape Key was hit just exit the loop

Hope this helps. :)

Comments

0

I have Opencv c++ code which can save image when any keyboard button is pressed. You can convert this opencv c++ code to opencv python.

USAGE:

Options:

Press 1 for capturing from CAM 1

Press 2 for capturing from CAM 2

Press d for capturing from both CAM 1 & CAM 2

Press a for Termination

Here I am using two camera which is having FOV > 180 degree. So two camera can capture one pattern with common field of view. Or you can capture from single camera also.

#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <unistd.h>


using namespace cv;
using namespace std;


int ct = 0;
char key;
char filename[100];
int  c = 1;
char filename1[100];
int  d = 1;

int main(int, char**)
{


    Mat frame,frame1;
    cout <<"USAGE:\n"
           "Options:\n"
           "Press 1 for capturing from CAM 1\n"
           "Press 2 for capturing from CAM 2\n"
           "Press d for capturing from both CAM 1 & CAM 2\n"
           "Press a for Termination"<<endl;

    //--- INITIALIZE VIDEOCAPTURE
    VideoCapture cap(0);
    VideoCapture cap1(1);
//    cap.set(CAP_PROP_FRAME_WIDTH,1024);
//    cap.set(CAP_PROP_FRAME_HEIGHT,768);
//    cap1.set(CAP_PROP_FRAME_WIDTH,1024);
//    cap1.set(CAP_PROP_FRAME_HEIGHT,768);


    while(true)
    {

        cap >> frame;
        cap1 >> frame1;

        double cam_1 =cap.get(CV_CAP_PROP_POS_MSEC);
        double cam_2 =cap.get(CV_CAP_PROP_POS_MSEC);

        if (frame.empty()||frame1.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }

        usleep(5);
        imshow("CAMERA 1", frame);
        imshow("CAMERA 2", frame1);

        tipka = cv::waitKey(30);

        if (key == 'd')         //CAPTURING IMAGE FROM TWO CAMERA AT A SAME TIME
        {
            sprintf(filename, "/home/user/capture_imgs/Two_cam/0-%d.jpg", c);
            sprintf(filename1, "/home/user/capture_imgs/Two_cam/1-%d.jpg", d);

            //            imshow("CAMERA 1", frame);
            //            imshow("CAMERA 2", frame1);
            imwrite(filename, frame);
            imwrite(filename1, frame1);
            cout << "Cam 1 image captured   = " << c << endl;
            cout << "Cam 2 image captured   = " << d << endl;
            c++;
            d++;
        }
        if(key == '1')      //CAPTURING IMAGE FROM CAM 1
        {
            sprintf(filename, "/home/user/capture_imgs/CAM_1/0-%d.jpg", c);
            //            imshow("CAMERA 1", frame);
            imwrite(filename, frame);
            cout << "Cam 1 image captured   = " << c << endl;
            c++;
        }
        if(key == '2')      //CAPTURING IMAGE FROM CAM 2
        {
            sprintf(filename1, "/home/user/capture_imgs/CAM_2/1-%d.jpg", d);
            //            imshow("CAMERA 2", frame1);
            imwrite(filename1, frame1);
            cout << "Cam 2 image captured   = " << d << endl;
            d++;

        }
        if (key == 'a')
        {
            cout << "Terminated..." << endl;
            usleep(1000);
            break;
        }
    }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.