surrortg.image_recognition

Classes

surrortg.image_recognition.async_video_capture

class CapComm(value)

Bases: enum.Enum

For communication between AsyncVideoCapture and VideoCaptureProcess

INIT_SUCCESS = 1
INIT_FAILURE = 2
FRAME_REQUEST = 3
RELEASE_REQUEST = 4
RELEASED = 5
class VideoCaptureProcess(source, conn, apiPreference)

Bases: object

Separated cv2.VideoCapture process class

Should be started with multiprocessing.Process(… daemon=True), so it won’t block exit if the main process fails.

Before usage: pip install numpy opencv-contrib-python

Parameters
  • source (String/Int) – Camera id or path

  • conn (multiprocessing.connection.Connection) – multiprocessing.connection.Pipe() one end of the connection

run()
class AsyncVideoCapture

Bases: object

Non-blocking video capture without an internal buffer

Based on cv2.VideoCapture class. Does not handle video files. Use factory method ‘await AsyncVideoCapture.create(source, …)’ instead of __init__

async classmethod create(source, init_timeout=2, read_timeout=2, release_timeout=2, process_class=<class 'surrortg.image_recognition.async_video_capture.VideoCaptureProcess'>, apiPreference=<Mock name='mock.CAP_V4L2' id='140433094787928'>)

Factory method for AsyncVideoCapture, use this instead of __init__

Parameters
  • source (String/Int) – Camera id or path

  • init_timeout – Max time to wait for VideoCapture init, otherwise RuntimeError will be raised, defaults to 2

  • read_timeout (int, optional) – Max time to wait for frame in seconds, after the timeout VideoCapture will be released and reinitialized, defaults to 2

  • release_timeout (int, optional) – Max time to wait for VideoCapture release, otherwise SIGKILL will be sent, defaults to 2

  • process_class (VideoCaptureProcess, optional) – Video capture process class implementation, option mainly for easier testing

  • apiPreference (cv2 VideoCaptureAPI, optional) – backend apiPreference for cv2.VideoCapture, defaults to cv2.CAP_V4L2

async read()

Retries until able to return a new frame or released

Returns None if released

Returns

the next frame or None

Return type

numpy.ndarray/None

frames()

Async generator method for getting frames

Yield

next frame by using read()

Return type

async_generator

async release()

Release resources. If frames() generator was used, will log FPS

surrortg.image_recognition.pixel_detect

get_pixel_detector(pixels, close=25)

Get a detector function for spesific pixels

Parameters
  • pixels ([(tuple,tuple)]) – list of pixels as ((x, y), (r, g, b))

  • close (int, optional) – how close rgb value is a match, defaults to 25

main(frame, name)

Print example code for ‘get_pixel_detector’ function

Usage: python pixel_detect.py <path_to_frame> <detectable_name>