Refactor test.py to use VideoCapture

This commit is contained in:
Anthony Wharton 2019-05-12 18:48:50 +01:00
parent 804016d1a0
commit 200e536b5e

View file

@ -7,6 +7,7 @@ import sys
import time import time
import cv2 import cv2
import dlib import dlib
from recorders.video_capture import VideoCapture
# Get the absolute path to the current file # Get the absolute path to the current file
path = os.path.dirname(os.path.abspath(__file__)) path = os.path.dirname(os.path.abspath(__file__))
@ -20,22 +21,7 @@ if config.get("video", "recording_plugin") != "opencv":
print("Aborting") print("Aborting")
sys.exit(12) sys.exit(12)
# Start capturing from the configured webcam video_capture = VideoCapture(config)
video_capture = cv2.VideoCapture(config.get("video", "device_path"))
# Force MJPEG decoding if true
if config.getboolean("video", "force_mjpeg", fallback=False):
# Set a magic number, will enable MJPEG but is badly documented
video_capture.set(cv2.CAP_PROP_FOURCC, 1196444237)
# Set the frame width and height if requested
fw = config.getint("video", "frame_width", fallback=-1)
fh = config.getint("video", "frame_height", fallback=-1)
if fw != -1:
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, fw)
if fh != -1:
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, fh)
# Read exposure and dark_thresholds from config to use in the main loop # Read exposure and dark_thresholds from config to use in the main loop
exposure = config.getint("video", "exposure", fallback=-1) exposure = config.getint("video", "exposure", fallback=-1)
@ -109,17 +95,7 @@ try:
sec_frames = 0 sec_frames = 0
# Grab a single frame of video # Grab a single frame of video
ret, frame = video_capture.read() _, frame = video_capture.read_frame()
try:
# Convert from color to grayscale
# First processing of frame, so frame errors show up here
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
except RuntimeError:
pass
except cv2.error:
print("\nUnknown camera, please check your 'device_path' config value.\n")
raise
frame = clahe.apply(frame) frame = clahe.apply(frame)
# Make a frame to put overlays in # Make a frame to put overlays in
@ -211,8 +187,8 @@ try:
# are captured and even after a delay it does not # are captured and even after a delay it does not
# always work. Setting exposure at every frame is # always work. Setting exposure at every frame is
# reliable though. # reliable though.
video_capture.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1.0) # 1 = Manual video_capture.intenal.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1.0) # 1 = Manual
video_capture.set(cv2.CAP_PROP_EXPOSURE, float(exposure)) video_capture.intenal.set(cv2.CAP_PROP_EXPOSURE, float(exposure))
# On ctrl+C # On ctrl+C
except KeyboardInterrupt: except KeyboardInterrupt:
@ -220,5 +196,4 @@ except KeyboardInterrupt:
print("\nClosing window") print("\nClosing window")
# Release handle to the webcam # Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows() cv2.destroyAllWindows()