Refactor compare.py to use VideoCapture

This commit is contained in:
Anthony Wharton 2019-05-12 18:57:26 +01:00
parent 140b4f3d67
commit 1555ffe82f

View file

@ -18,6 +18,7 @@ import cv2
import dlib
import numpy as np
import _thread as thread
from recorders.video_capture import VideoCapture
def init_detector(lock):
@ -46,13 +47,6 @@ def init_detector(lock):
timings["ll"] = time.time() - timings["ll"]
lock.release()
def stop(status):
"""Stop the execution and close video stream"""
video_capture.release()
sys.exit(status)
# Make sure we were given an username to tast against
if len(sys.argv) < 2:
sys.exit(12)
@ -113,36 +107,7 @@ thread.start_new_thread(init_detector, (lock, ))
# Start video capture on the IR camera
timings["ic"] = time.time()
# Check if the user explicitly set ffmpeg as recorder
if config.get("video", "recording_plugin") == "ffmpeg":
# Set the capture source for ffmpeg
from recorders.ffmpeg_reader import ffmpeg_reader
video_capture = ffmpeg_reader(config.get("video", "device_path"), config.get("video", "device_format"))
elif config.get("video", "recording_plugin") == "pyv4l2":
# Set the capture source for pyv4l2
from recorders.pyv4l2_reader import pyv4l2_reader
video_capture = pyv4l2_reader(config.get("video", "device_path"), config.get("video", "device_format"))
else:
# Start video capture on the IR camera through OpenCV
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
# 1196444237 is "GPJM" in ASCII
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)
# Capture a single frame so the camera becomes active
# This will let the camera adjust its light levels while we're importing for faster scanning
video_capture.grab()
video_capture = VideoCapture(config)
# Read exposure from config to use in the main loop
exposure = config.getint("video", "exposure", fallback=-1)
@ -158,7 +123,7 @@ del lock
# Fetch the max frame height
max_height = config.getfloat("video", "max_height", fallback=0.0)
# Get the height of the image
height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT) or 1
height = video_capture.internal.get(cv2.CAP_PROP_FRAME_HEIGHT) or 1
# Calculate the amount the image has to shrink
scaling_factor = (max_height / height) or 1
@ -178,24 +143,10 @@ while True:
# Stop if we've exceded the time limit
if time.time() - timings["fr"] > timeout:
stop(11)
sys.exit(11)
# Grab a single frame of video
ret, frame = video_capture.read()
if frames == 1 and ret is False:
print("Could not read from camera")
exit(12)
try:
# Convert from color to grayscale
# First processing of frame, so frame errors show up here
gsframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
except RuntimeError:
gsframe = frame
except cv2.error:
print("\nUnknown camera, please check your 'device_path' config value.\n")
raise
frame, gsframe = video_capture.read_frame()
# Create a histogram of the image with 8 values
hist = cv2.calcHist([gsframe], [0], None, [8], [0, 256])
@ -255,7 +206,7 @@ while True:
print_timing("Total time", "tt")
print("\nResolution")
width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH) or 1
width = video_capture.fw or 1
print(" Native: %dx%d" % (height, width))
# Save the new size for diagnostics
scale_height, scale_width = frame.shape[:2]
@ -269,7 +220,7 @@ while True:
print("Winning model: %d (\"%s\")" % (match_index, models[match_index]["label"]))
# End peacefully
stop(0)
sys.exit(0)
if exposure != -1:
# For a strange reason on some cameras (e.g. Lenoxo X1E)
@ -277,5 +228,5 @@ while True:
# are captured and even after a delay it does not
# always work. Setting exposure at every frame is
# reliable though.
video_capture.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_AUTO_EXPOSURE, 1.0) # 1 = Manual
video_capture.intenal.set(cv2.CAP_PROP_EXPOSURE, float(exposure))