|
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) |