Last active
October 4, 2017 01:34
-
-
Save garybradski/c5e7962b85272ce9125b8dee9969f985 to your computer and use it in GitHub Desktop.
Just the throw together code for running a vdieo in python together with logging etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import argparse | |
import numpy as np | |
import os | |
import logging | |
#GLOBAL | |
logging.basicConfig() #Fire up the logger | |
logger = logging.getLogger() | |
def open_files(args): | |
wait_key = args.wait_key | |
if (args.loglevel == 'OFF'): | |
wait_key = 1 | |
#OPEN VIDEO | |
vcap = cv2.VideoCapture(args.video) | |
if (vcap.isOpened()): | |
vwidth = int(vcap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) # float | |
vheight = int(vcap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) # float | |
else: | |
logger.fatal("Video (%s) could not be opened", args.video) | |
return | |
vlen = int(vcap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) | |
v_in = int(args.v_in) | |
v_out = int(args.v_out) | |
if not ((v_in >= 0) and (v_out > v_in) and (vlen > v_out)): | |
logger.critical("MUST be video len %d > v_out %d > v_in %d >= 0",vlen,v_out,v_in) | |
return -1 | |
# seek | |
assert vcap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.v_in) | |
framerate = vcap.get(cv2.cv.CV_CAP_PROP_FPS) | |
logger.info('frame.shape[:2] (%d, %d), framerate = %f',vheight, vwidth, framerate) | |
#FOR DEBUG | |
if (logger.isEnabledFor(logging.DEBUG)): | |
cv2.namedWindow('video', cv2.WINDOW_NORMAL) | |
cv2.resizeWindow('video', 960, 540) | |
#VIDEO PROCESSING | |
while True: | |
frame_count = vcap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) | |
if frame_count > v_out: | |
break | |
ret, frame = vcap.read() | |
if not ret: | |
break | |
#DEBUG DISPLAY | |
if (logger.isEnabledFor(logging.DEBUG)): | |
cv2.imshow("video", frame) | |
#key handling | |
if (logger.getEffectiveLevel() == logging.DEBUG): | |
k = cv2.waitKey(wait_key) & 0xff | |
if chr(k) == 'r': #start running | |
wait_key = 30 | |
elif chr(k) == 'p': #pause between frames | |
wait_key = 0 | |
elif k == 27: #end processing | |
break | |
else: | |
k = 0 | |
else: #Normal run mode... | |
k = cv2.waitKey(wait_key) & 0xff | |
if k == 27: #end processing | |
break | |
#CLEAN UP FOR EXIT | |
vcap.release() | |
return 0 | |
################ MAIN ###################################################### | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--video', help='Video file to segment') | |
parser.add_argument('--v_in', help='Vide start frame', type=int, default=0) | |
parser.add_argument('--v_out', help='Video end frame', type=int, default=100) | |
parser.add_argument('--seg_dir', help='Directory of segmentation masks (for now via Blitznet') | |
parser.add_argument('--edge_dir', help='Directory of edgemaps') | |
parser.add_argument( | |
'--output', default='.', help='Output path. [Default . ]') | |
parser.add_argument( | |
'--wait_key', default=0, type=int, help= | |
'OpenCV wait key time (for segmentation only) off if logging is off [default 0]' | |
) | |
parser.add_argument( | |
'--loglevel', | |
help= | |
'Logging level: NONE 100, OFF 100, DEBUG 10, INFO 20, WARNING 30, ERROR 40, CRITICAL 50. [Default: OFF 100]', | |
type=str, | |
default='DEBUG') | |
args = parser.parse_args() | |
if (args.loglevel == 'OFF'): logger.setLevel(100) | |
elif (args.loglevel == 'NONE'): logger.setLevel(100) | |
elif (args.loglevel == 'DEBUG'): logger.setLevel(logging.DEBUG) | |
elif (args.loglevel == 'INFO'): logger.setLevel(logging.INFO) | |
elif (args.loglevel == 'WARNING'): logger.setLevel(logging.WARNING) | |
elif (args.loglevel == 'ERROR'): logger.setLevel(logging.ERROR) | |
elif (args.loglevel == 'CRITICAL'): logger.setLevel(logging.CRITICAL) | |
elif (args.loglevel == 'FATAL'): logger.setLevel(logging.FATAL) | |
# logger.debug("debug mode") | |
# logger.info("info mode") | |
# logger.warning("warning mode") | |
# logger.error("error mode") | |
# logger.critical("critical mode") | |
# logger.fatal("fatal mode") | |
if (logger.isEnabledFor(logging.INFO)): | |
logger.info('Logger effective level = %s, OpenCV Version = %s', logger.getEffectiveLevel(), cv2.__version__) | |
logger.info('Video: %s, start= %d, stop= %d, seg_dir= %s, edge_dir= %s, out= %s', args.video, args.v_in, args.v_out, args.seg_dir, args.edge_dir, args.output) | |
open_files(args) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment