def _cam_setup(self, source, w, h, fps, exposure):
cap = cv2.VideoCapture(source)
if not cap.isOpened():
return None
atexit.register(cap.release)
# Set width/height (note: this fails in OpenCV 2.4.x but works with 3.2)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
# Read a frame, just in case it's needed before setting params
rval, _ = cap.read()
if not rval:
cap.release()
return None
# Hacks using v4l2-ctl to set capture parameters we can't control through OpenCV
v4l2args = []
# Change AWB setting once to make sure new settings are actually used
# Not sure why this is required, but it appears to work.
# Without this, white balance is still somehow automatic even
# after setting it to manual below.
self._v4l2_call("--set-ctrl=white_balance_auto_preset=1")
# Set FPS (OpenCV requests 30, hardcoded)
v4l2args.append("-p %d" % fps)
# Set exposure (shutter speed/ISO)
# exposure_time_absolute is given in multiples of 0.1ms.
# Make sure fps above is not set too high (exposure time
# will be adjusted automatically to allow higher frame rate)
v4l2args.append("--set-ctrl=auto_exposure=1") # 0=auto, 1=manual
v4l2args.append("--set-ctrl=exposure_time_absolute=%d" % exposure)
v4l2args.append("--set-ctrl=white_balance_auto_preset=0")
v4l2args.append("--set-ctrl=red_balance=1000")
v4l2args.append("--set-ctrl=blue_balance=1000")
self._v4l2_call(" ".join(v4l2args))
logging.info("Set exposure via v4l2-ctl. Capturing/dumping frames so settings take effect before tracking starts.")
for _ in range(5):
cap.read()
return cap
评论列表
文章目录