Skip to content

Instantly share code, notes, and snippets.

@chrisvoncsefalvay
Last active November 16, 2016 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvoncsefalvay/31a304621a85a859fedd87846ddb9e49 to your computer and use it in GitHub Desktop.
Save chrisvoncsefalvay/31a304621a85a859fedd87846ddb9e49 to your computer and use it in GitHub Desktop.
LiveCapture

LiveCapture 📸

LiveCapture is a tool to experiment with computer vision, in particular with image processing. The workflow is quite simple.

  1. You write a processing function, which takes a logger and an image.
  2. You run the script including the processing function.
  3. From a live view of the camera, you capture a frame using the Esc key.
  4. The capture is then run through your processing functions. The results are logged in log.html using visual-logging.

Usage

  1. Save live_capture.py somewhere in your project folder. I have it under misc/capture. Allow it to be imported as, say, misc.live_capture by calling it in the __init__.py files.
  2. In your processing script (template: process.py), import vlogging.VisualRecord and misc.live_capture.
  3. Declare a function that consumes an image and a logger.
  4. Use calls to the logger and pass in a vlogging.VisualRecord object to record images.
import cv2
from datetime import datetime
from vlogging import VisualRecord
import logging
def create_capture_frame(img):
"""
Creates the capture frame.
:param img: incoming image buffer
:type img: numpy.ndarray
:return: outgoing image buffer
:rtype: numpy.ndarray
"""
# Add safe zone rectangle
_w = img.shape[1]
_h = img.shape[0]
_lu = (int(_w/10), int(_h/10))
_rb = (_w-_lu[0], _h-_lu[1])
cv2.rectangle(img, _lu, _rb,
color=(0,255,0), thickness=1)
# Add crosshairs
font = cv2.FONT_HERSHEY_SIMPLEX
middle = (int(_w / 2), int(_h / 2))
cv2.putText(img, text="+", org=middle, fontFace=font, fontScale=3, color=(0, 255, 0))
cv2.putText(img, "< Press 'Esc' to capture frame >",
org=(int(_w/3)-int(_w/24), int(_h/20)),
fontFace=font,
fontScale=1.0,
color=(20,20,215))
timestamp = datetime.utcnow().strftime("%d%H%M%SZ %b %y").upper()
cv2.putText(img, timestamp,
org=(_w - int(_w/2) - int(_w/8), _h-(_h/20)),
fontFace=font,
fontScale=1.0,
color=(0,255,0))
return img
def live_capture(vc=None, vd=None, processor_function=None, logger=None):
"""
Creates a live capture pathway.
1) A live camera display is shown.
2) When pressing `q`, the current frame is captured.
3) `processor_function` is called on the frame.
4) `processor_function(frame)` is returned.
It is highly recommended to have a visual logger included and specify it under `logger`.
:param vc: VideoCapture object
:type vc: cv2.VideoCapture | None
:param vd: video device ID
:type vd: int | None
:param processor_function: processing function
:type processor_function: function
:param logger: logger (must be capable of visual logging)
:type logger: logging.Logger
:return: image
:rtype: numpy.ndarray
"""
if vc:
_vc = vc
elif vd:
_vc = cv2.VideoCapture(vd)
else:
_vc = cv2.VideoCapture(0)
# Show capture frame
_res = None
while True:
_, imgbuff = _vc.read()
acquired_image = (imgbuff)
processed_image = create_capture_frame(acquired_image.copy())
cv2.imshow("Press q to capture image", processed_image)
k = cv2.waitKey(30) & 0xFF
if k == 27:
logger.info(VisualRecord(title="Original capture", imgs=processed_image))
_vc.release()
cv2.destroyAllWindows()
_res = acquired_image
break
return processor_function(_res, logger)
# coding=utf-8
import logging
import argparse
import cv2
from vlogging import VisualRecord
from misc import live_capture
def $PROCESS$(img, logger):
"""
$DESCRIBE$
"""
return img
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=False, help='Path to the image')
ap.add_argument('-l', '--logfile', required=False, help='Logfile name', default='log')
args=vars(ap.parse_args())
$PROCESS$_logger = logging.getLogger('$PROCESS$_logger')
fh = logging.FileHandler('{}.html'.format(args['logfile']), mode='w')
$PROCESS$_logger.setLevel(logging.DEBUG)
$PROCESS$_logger.addHandler(fh)
if args['image']:
live_capture(image=args['image'], processor_function=$PROCESS$, logger=$PROCESS$_logger)
else:
live_capture(vd=0, processor_function=$PROCESS$, logger=$PROCESS$_logger)
logger.debug(VisualRecord("$MESSAGE$",
$IMGVAR$,
$SUBTITLE$,
fmt="$FMT$"))
logger.info(VisualRecord("$MESSAGE$",
$IMGVAR$,
$SUBTITLE$,
fmt="$FMT$"))
logger.warning(VisualRecord("$MESSAGE$",
$IMGVAR$,
$SUBTITLE$,
fmt="$FMT$"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment