Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created September 24, 2012 00:04
Show Gist options
  • Save pklaus/3773505 to your computer and use it in GitHub Desktop.
Save pklaus/3773505 to your computer and use it in GitHub Desktop.
Webcam Capture with OpenCV's Python Interface cv2 and with its C++ interface
all: video
UNAME := $(shell uname)
O_CFLAGS := $(shell pkg-config --cflags opencv)
O_LDFLAGS := $(shell pkg-config --libs opencv)
CC = g++
# Not really needed right now but might be useful:
ifeq ($(UNAME), Linux)
## On Ubuntu / Debian
endif
ifeq ($(UNAME), Darwin)
## On Mac OS X
endif
video: video.cpp
$(CC) -o video video.cpp $(O_CFLAGS) $(O_LDFLAGS)
clean:
-rm video
.PHONY: clean
# -*- coding: utf-8 -*-
""" A collection of useful mathematical tools and functions. """
import numpy as np
def scoreatpercentile(a, per, limit=()):
"""
Calculate the score at the given percentile_ `per` of the sequence `a`.
Largely the same as http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.scoreatpercentile.html .
.. _percentile: http://en.wikipedia.org/wiki/Percentile"""
values = np.sort(a, axis=0)
if limit:
values = values[(limit[0] <= values) & (values <= limit[1])]
idx = per /100. * (values.shape[0] - 1)
if (idx % 1 == 0):
score = values[idx]
else:
score = values[int(idx)] + (idx % 1) * (values[int(idx) + 1] - values[int(idx)])
return score
#!/usr/bin/env python
# http://johnroach.info/2011/03/02/image-capturing-from-webcam-using-opencv-and-pygame-in-python/
import pygame
import Image
from pygame.locals import *
import sys
import cv2
import numpy
cap = cv2.VideoCapture(0)
i=0
def get_image():
ret, im = cap.read()
#convert numpy array to PIL image
im = numpy.array(im)
return Image.fromarray(im)
fps = 25.0
pygame.init()
window = pygame.display.set_mode((1280,720))
pygame.display.set_caption("WebCAM Demo")
screen = pygame.display.get_surface()
while True:
events = pygame.event.get()
for event in events:
if event.type == QUIT or event.type == KEYDOWN:
sys.exit(0)
im = get_image()
if i>100:
#allowing the camera to focus auto focus is really annoying
im.save("image_"+str(i)+"", "JPEG")
i=i+1
pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
screen.blit(pg_img, (0,0))
pygame.display.flip()
pygame.time.delay(int(1000 * 1.0/fps))
/*
* starter_video.cpp
*
* Created on: Nov 23, 2010
* Author: Ethan Rublee
*
* A starter sample for using opencv, get a video stream and display the images
* easy as CV_PI right?
*
*
* Also check http://laconsigna.wordpress.com/2011/04/29/1d-histogram-on-opencv/
*
*/
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace cv;
using namespace std;
//hide the local functions in an anon namespace
namespace
{
void help(char** av)
{
cout << "\nThis program justs gets you started reading images from video\n"
"Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
<< "space -- save frame\n\n"
<< "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
<< "\tThe program captures frames from a camera connected to your computer.\n"
<< "\tTo find the video device number, try ls /dev/video* \n"
<< "\tYou may also pass a video file, like my_vide.avi instead of a device number"
<< endl;
}
int process(VideoCapture& capture)
{
int n = 0;
char filename[200];
string window_name = "video | q or esc to quit";
cout << "press space to save a picture. q or esc to quit" << endl;
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
// namedWindow( "H-S Histogram", 1 );
namedWindow("Histogram", CV_WINDOW_AUTOSIZE );
Mat histImage;
Mat frame;
int COMPUTE_EVERY = 1; //20;
for (int j = 0; j != -1; j++)
{
capture >> frame;
if (frame.empty())
break;
if (j % COMPUTE_EVERY == 0)
{
/// Example from http://opencv.willowgarage.com/documentation/cpp/histograms.html
//Mat hsv;
//cvtColor(frame, hsv, CV_BGR2HSV);
//// let's quantize the hue to 30 levels
//// and the saturation to 32 levels
//int hbins = 30, sbins = 32;
//int histSize[] = {hbins, sbins};
//// hue varies from 0 to 179, see cvtColor
//float hranges[] = { 0, 180 };
//// saturation varies from 0 (black-gray-white) to
//// 255 (pure spectrum color)
//float sranges[] = { 0, 256 };
//const float* ranges[] = { hranges, sranges };
//MatND hist;
//// we compute the histogram from the 0-th and 1-st channels
//int channels[] = {0, 1};
//calcHist( &hsv, 1, channels, Mat(), // do not use mask
// hist, 2, histSize, ranges,
// true, // the histogram is uniform
// false );
//double maxVal=0;
//minMaxLoc(hist, 0, &maxVal, 0, 0);
//int scale = 10;
//Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
//for( int h = 0; h < hbins; h++ )
// for( int s = 0; s < sbins; s++ )
// {
// float binVal = hist.at<float>(h, s);
// int intensity = cvRound(binVal*255/maxVal);
// rectangle( histImg, Rect(Point(h*scale, s*scale),
// Point( (h+1)*scale - 1, (s+1)*scale - 1)),
// Scalar::all(intensity),
// CV_FILLED );
// }
/// Example from http://docs.opencv.org/2.4.2/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split( frame, bgr_planes );
/// Establish the number of bins
int histSize = 64; //256;
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256} ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
// Draw the histograms for B, G and R
//int hist_w = 512; int hist_h = 400;
//int hist_w = 1024; int hist_h = 400;
int hist_w = 1152; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/(histSize+1) );
histImage = Mat( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
/// Draw for each channel
for( int i = 0; i < histSize; i++ )
{
//cout << "[" << i << "] b: " << b_hist.at<float>(i-1) << " g: " << g_hist.at<float>(i-1) << " r: " << r_hist.at<float>(i-1) << endl;
rectangle( histImage, Point( bin_w*(i)+1, hist_h - cvRound(b_hist.at<float>(i)) ) ,
Point( bin_w*(i)+3, hist_h ),
Scalar( 255, 0, 0), 2, 8, 0 );
rectangle( histImage, Point( bin_w*(i)+5, hist_h - cvRound(g_hist.at<float>(i)) ) ,
Point( bin_w*(i)+7, hist_h ),
Scalar( 0, 255, 0), 2, 8, 0 );
rectangle( histImage, Point( bin_w*(i)+9, hist_h - cvRound(r_hist.at<float>(i)) ) ,
Point( bin_w*(i)+11, hist_h ),
Scalar( 0, 0, 255), 2, 8, 0 );
//line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
// Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
// Scalar( 255, 0, 0), 2, 8, 0 );
//line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
// Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
// Scalar( 0, 255, 0), 2, 8, 0 );
//line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
// Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
// Scalar( 0, 0, 255), 2, 8, 0 );
}
}
Mat gray;
cvtColor(frame,gray,CV_RGB2GRAY);
vector<string> codes;
Mat corners;
imshow(window_name, frame);
/// Show sum of image
Scalar ch_img_sum = sum(frame);
double img_sum = ch_img_sum[0] + ch_img_sum[1] + ch_img_sum[2];
if (j % 4 == 0) cout << "Sum of values: " << scientific << img_sum << endl;
if (j % COMPUTE_EVERY == 0)
{
/// Display
imshow("Histogram", histImage );
//imshow( "H-S Histogram", histImg );
}
char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
switch (key)
{
case 'q':
case 'Q':
case 27: //escape key
return 0;
case ' ': //Save an image
sprintf(filename, "filename%.3d.jpg", n++);
imwrite(filename, frame);
cout << "Saved " << filename << endl;
break;
default:
break;
}
}
return 0;
}
}
int main(int ac, char** av)
{
if (ac != 2)
{
help(av);
return 1;
}
std::string arg = av[1];
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
capture.open(atoi(arg.c_str()));
if (!capture.isOpened())
{
cerr << "Failed to open a video device or video file!\n" << endl;
help(av);
return 1;
}
return process(capture);
}
#!/usr/bin/env python
# -*- encoding: UTF8 -*-
'''
Capturing live video (from a webcam) using OpenCV.
In version control on <https://gist.github.com/3773505>.
Stripped-down version of the one found on
<http://code.opencv.org/projects/opencv/repository/revisions/master/entry/samples/python2/video.py>.
Modified by Philipp Klaus, philipp.l.klaus AT web.de
Usage:
video.py [--shotdir <shot path>] [source]
source is an integer number for camera capture (or a movie file).
Keys:
ESC - exit
SPACE - save current frame to <shot path> directory
'''
import cv2
properties = {
'CV_CAP_PROP_POS_MSEC' : cv2.cv.CV_CAP_PROP_POS_MSEC,
'CV_CAP_PROP_POS_FRAMES' : cv2.cv.CV_CAP_PROP_POS_FRAMES,
'CV_CAP_PROP_POS_AVI_RATIO' : cv2.cv.CV_CAP_PROP_POS_AVI_RATIO,
'CV_CAP_PROP_FRAME_WIDTH' : cv2.cv.CV_CAP_PROP_FRAME_WIDTH,
'CV_CAP_PROP_FRAME_HEIGHT' : cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,
'CV_CAP_PROP_FPS' : cv2.cv.CV_CAP_PROP_FPS,
'CV_CAP_PROP_FOURCC' : cv2.cv.CV_CAP_PROP_FOURCC,
'CV_CAP_PROP_FRAME_COUNT' : cv2.cv.CV_CAP_PROP_FRAME_COUNT,
'CV_CAP_PROP_FORMAT' : cv2.cv.CV_CAP_PROP_FORMAT,
'CV_CAP_PROP_MODE' : cv2.cv.CV_CAP_PROP_MODE,
'CV_CAP_PROP_BRIGHTNESS' : cv2.cv.CV_CAP_PROP_BRIGHTNESS,
'CV_CAP_PROP_CONTRAST' : cv2.cv.CV_CAP_PROP_CONTRAST,
'CV_CAP_PROP_SATURATION' : cv2.cv.CV_CAP_PROP_SATURATION,
'CV_CAP_PROP_HUE' : cv2.cv.CV_CAP_PROP_HUE,
'CV_CAP_PROP_GAIN' : cv2.cv.CV_CAP_PROP_GAIN,
'CV_CAP_PROP_EXPOSURE' : cv2.cv.CV_CAP_PROP_EXPOSURE,
'CV_CAP_PROP_CONVERT_RGB' : cv2.cv.CV_CAP_PROP_CONVERT_RGB,
#'CV_CAP_PROP_WHITE_BALANCE' : cv2.cv.CV_CAP_PROP_WHITE_BALANCE,
'CV_CAP_PROP_RECTIFICATION' : cv2.cv.CV_CAP_PROP_RECTIFICATION,
}
def create_capture(source = 0):
'''source: <int> or '<int>|<filename>|synth [:<param_name>=<value> [:...]]'
'''
source = str(source).strip()
chunks = source.split(':')
# handle drive letter ('c:', ...)
if len(chunks) > 1 and len(chunks[0]) == 1 and chunks[0].isalpha():
chunks[1] = chunks[0] + ':' + chunks[1]
del chunks[0]
source = chunks[0]
try: source = int(source)
except ValueError: pass
params = dict( s.split('=') for s in chunks[1:] )
cap = cv2.VideoCapture(source)
if 'size' in params:
w, h = map(int, params['size'].split('x'))
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, w)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, h)
if cap is None or not cap.isOpened():
print 'Warning: unable to open video source: ', source
for name, value in properties.items():
print "%s: %s" % (name, cap.get(value) )
return cap
if __name__ == '__main__':
import sys
import getopt
print __doc__
args, sources = getopt.getopt(sys.argv[1:], '', 'shotdir=')
args = dict(args)
shotdir = args.get('--shotdir', '.')
if len(sources) == 0:
sources = [ 0 ]
cv2.namedWindow("capture 0", cv2.WINDOW_NORMAL) # instead of cv2.WINDOW_AUTOSIZE
#cv2.namedWindow("capture 0", )
#cv2.setWindowProperty("capture 0", cv2.CV_WND_PROP_FULLSCREEN, cv2.CV_WINDOW_FULLSCREEN)
### the above doesn't work but the following does ( values from
### http://pyopencv.googlecode.com/svn-history/r842/trunk/src_gen/highgui_h.py ) :
cv2.setWindowProperty("capture 0", 0, 1)
caps = map(create_capture, sources)
shot_idx = 0
# Image analysis
i = 0
from mathtools import scoreatpercentile
while True:
imgs = []
for i, cap in enumerate(caps):
ret, img = cap.read()
imgs.append(img)
### Possible image modifications:
#img = cv2.flip(img, 1)
#img = img/2
if i%7 == 0:
print("Sum over all pixels: %E 95 percentile value: %E" % (img.sum(), scoreatpercentile(img.sum(axis=2).flatten(),95) ) )
cv2.imshow('capture %d' % i, img)
ch = 0xFF & cv2.waitKey(1)
if ch == 27 or ch == ord('q'):
break
if ch == ord(' '):
for i, img in enumerate(imgs):
fn = '%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx)
cv2.imwrite(fn, img)
print fn, 'saved'
shot_idx += 1
cv2.destroyAllWindows()
#!/usr/bin/env python
# -*- encoding: UTF8 -*-
'''
Capturing live video (from a webcam) using OpenCV's old Python interface.
Better use the more up to date Python interface cv2 like here:
<https://gist.github.com/3773505#file_video.py>
This is a stripped-down version of the one found on
<http://www.developerstation.org/2010/08/reading-data-from-webcam-on-python.html>
Modified by Philipp Klaus, philipp.l.klaus AT web.de
'''
import cv
dims = (1280, 720) # webcam dimensions
camcapture = cv.CreateCameraCapture(0)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, dims[0])
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, dims[1]);
if not camcapture:
print "Error opening WebCAM"
sys.exit(1)
while 1:
frame = cv.QueryFrame(camcapture)
if frame is None:
break
cv.ShowImage('Camera Image', frame)
if 0xFF & cv.WaitKey(1) == 27:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment