Skip to content

Instantly share code, notes, and snippets.

@ollewelin
Created September 14, 2016 17:42
Show Gist options
  • Save ollewelin/8e781fc5157075d58e55ca259bb8fd81 to your computer and use it in GitHub Desktop.
Save ollewelin/8e781fc5157075d58e55ca259bb8fd81 to your computer and use it in GitHub Desktop.
//Generate Positive samples
//File: test_prog.cpp
//You must make a director /positive_data/ where this program could put the pictures in
#include <stdio.h>
#include <unistd.h>
#include <ctime>
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std;
using namespace cv;
//default capture width and height
//const int FRAME_WIDTH = 640;
//const int FRAME_HEIGHT = 480;
const int FRAME_WIDTH = 128;
const int FRAME_HEIGHT = 96;
const string WindowName = "Image viewer";
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main () {
//JPG File store
int ret;
char ch;
int files_number = 2000;
// FILE *store_files[files_number];
FILE *temp_file;
char filename[20];
//-------------
int camera_start_up_frames = 100;
// FILE *fp[files_number];
namedWindow(WindowName);
time_t timer_begin,timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=10;
//set camera params
Camera.set( CV_CAP_PROP_FORMAT, CV_8UC3 );
Camera.set( CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
Camera.set( CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
//Open camera
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
//Start capture
cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time ( &timer_begin );
char keyboard;
int started = 0;
// for ( int i=0; i<files_number; i++ ) {
for ( int i=0; i<files_number; NULL) {
if(kbhit()){
keyboard = getchar();
// printf("%c\n", keyboard);
if(keyboard== ' ')
{
if(started == 1)
{
started = 0;
}
else
{
started = 1;
}
}
}
Camera.grab();
Camera.retrieve ( image);
imshow(WindowName, image);
if(started == 1)
{
cv::imwrite("temporary_file.JPG",image);
//--- Save JPG files ----
sprintf(filename, "./positive_data/pos%d.JPG", i);
ret = rename("temporary_file.JPG", filename);
if(ret == 0)
{
printf("File renamed successfully");
}
else
{
printf("Error: unable to rename the file");
}
//---------------------
//cout<<"Image saved at ./x.JPG"<<endl;
printf("Image save af file: %d", i);
printf(".JPG\n" );
}
else
{
printf("Sampling stopped press and press SPACE to start or stop sampling\n" );
}
waitKey(200);
cv::imwrite("temporary_file.JPG",image);
if(started == 1)
{
i++;//Count up samples
}
}
cout<<"Stop camera..."<<endl;
Camera.release();
//show time statistics
time ( &timer_end );
double secondsElapsed = difftime ( timer_end,timer_begin );
cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
//save image
// cv::imwrite("raspicam_cv_image.jpg",image);
cout<<"Image saved at raspicam_cv_image.jpg"<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment