Skip to content

Instantly share code, notes, and snippets.

@vinaykumarhegde
Last active December 22, 2016 05:02
Show Gist options
  • Save vinaykumarhegde/10463450dbe608df538171965bf9e53a to your computer and use it in GitHub Desktop.
Save vinaykumarhegde/10463450dbe608df538171965bf9e53a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import cv2
from sys import argv, exit
import subprocess
def set_exposure(device,exposure):
'''
Sets the exposure values for Logitech C250 camera
@device: device name string (Ex: /dev/video0)
@exposure: valid exposure value [3,2047]. 0 for auto exposure
Returns:
0 - failure
1 - success
'''
print "Setting exposure {} on camaera {}".format(exposure,device)
# This doesn't work on Logitech Cameras
# cap.set(cv2.CAP_PROP_EXPOSURE,float(exposure))
if int(exposure) == 0:
#Set to auto exposure
try:
print "Setting auto exposure"
_out=subprocess.check_call(["v4l2-ctl -d {} --set-ctrl=exposure_auto=3".format(device)],shell=True)
except subprocess.CalledProcessError:
print "Could not set to auto exposure"
return 0
else:
print "Debug: {}".format(_out)
return 1
else:
try:
_out=subprocess.check_output(["v4l2-ctl -d {} --set-ctrl=exposure_absolute={}".format(device,exposure)],shell=True)
print "Debug: {}".format(_out)
except subprocess.CalledProcessError:
print "Couldn't set Exposure Manually. Trying to set camera to manual exposure mode"
try:
_out=subprocess.check_output(["v4l2-ctl -d {} --set-ctrl=exposure_auto=1".format(device)],shell=True)
print "Debug: {}".format(_out)
_out=subprocess.check_output(["v4l2-ctl -d {} --set-ctrl=exposure_absolute={}".format(device,exposure)],shell=True)
print "Debug: {}".format(_out)
except subprocess.CalledProcessError:
print "Could not set exposure."
return 0
else:
return 1
else:
return 1
if __name__ == '__main__':
if len(argv) != 3:
print "Usage: {} <camera> <exposure>".format(argv[0])
exit(1)
device=argv[1]
exposure=argv[2]
# Set device exposure
if not set_exposure(device,exposure):
print "Failed to set exposure"
exit(1)
print "Opening device: {}".format(device)
cap = cv2.VideoCapture(device)
if not cap.isOpened():
print "Unable to open the camera"
exit(1)
cv2.namedWindow('Image')
cv2.moveWindow('Image',100,100)
print "Showing image; Press q to exit"
while (True):
_,c = cap.read()
cv2.imshow('Image',c)
q=cv2.waitKey(30)
if q == ord('q'):
break
print "Exit"
cap.release()
cv2.destroyAllWindows()
@vinaykumarhegde
Copy link
Author

For Auto Exposure

		_out=subprocess.check_output(["v4l2-ctl -d {} --set-ctrl=exposure_auto=3".format(device)],shell=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment