def Init(self): glutKeyboardFunc(self.keyboard) glutSpecialFunc(self.special) if 'darwin' not in os.uname()[0].lower(): capture = self.getCapture() b = 0.213794155745 c = 0.131609063828 #cv.SetCaptureProperty(capture,CVtypes.CV_CAP_PROP_BRIGHTNESS,0.514000152471) #cv.SetCaptureProperty(capture,CVtypes.CV_CAP_PROP_CONTRAST,0.161806668155) cv.SetCaptureProperty(capture, CVtypes.CV_CAP_PROP_BRIGHTNESS, b) cv.SetCaptureProperty(capture, CVtypes.CV_CAP_PROP_CONTRAST, c)
def special(self, key, x, y): capture = self.getCapture() b = cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_BRIGHTNESS) c = cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_CONTRAST) if key == GLUT_KEY_DOWN: b -= .1 elif key == GLUT_KEY_UP: b += .1 elif key == GLUT_KEY_LEFT: c -= .01 elif key == GLUT_KEY_RIGHT: c += .01 cv.SetCaptureProperty(capture, CVtypes.CV_CAP_PROP_BRIGHTNESS, b) cv.SetCaptureProperty(capture, CVtypes.CV_CAP_PROP_CONTRAST, c) b = cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_BRIGHTNESS) c = cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_CONTRAST) print 'Brightness:', b print 'Contrast:', c print
class WebcamDetector: def __init__(self, resolution=(640, 480), show_cam=False): self.setup(resolution, show_cam) def __del__(self): # release the capture device cv.ReleaseCapture(self.capture) if self.show_cam: # destroy the camera window cv.DestroyWindow('Camera') pass def setup(self, (width, height), show_cam): """Setup the webcam device and different windows.""" device = 0 # assume we want the first device capture = cv.CreateCameraCapture(0) # set the width/height of the captured image # this won't work on Windows (*sigh*) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, width) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, height) # get the width/height of the captured image # this won't work on Windows (*sigh*) self.width = cv.GetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH) self.height = cv.GetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT) if self.width == 0 and self.height == 0: # Windows self.width, self.height = 320, 240 # set to default # check if capture device is OK if not capture: print "Error opening capture device" self.capture = capture self.show_cam = show_cam if self.show_cam: cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE)
def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) # create windows cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE) # create capture device device = 0 # assume we want first device capture = cv.CreateCameraCapture(0) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480) # check if capture device is OK if not capture: print "Error opening capture device" sys.exit(1) while 1: # do forever # capture the current frame frame = cv.QueryFrame(capture) if frame is None: break # mirror cv.Flip(frame, None, 1) # face detection detect(frame) # display webcam image cv.ShowImage('Camera', frame) # handle events k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
try: # try to get the device number from the command line device = int(sys.argv[1]) # got it ! so remove it from the arguments del sys.argv[1] except (IndexError, ValueError): # no device number on the command line, assume we want the 1st device device = 0 if len(sys.argv) == 1: # no argument on the command line, try to use the camera capture = cv.CreateCameraCapture(device) # set the wanted image size from the camera cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 320) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 240) else: # we have an argument on the command line, # we can assume this is a file name, so open it capture = cv.CreateFileCapture(sys.argv[1]) # check that capture device is OK if not capture: print "Error opening capture device" sys.exit(1) # create an image to put in the histogram histimg = cv.CreateImage(cv.Size(320, 240), 8, 3) # init the image of the histogram to black