Skip to content

Instantly share code, notes, and snippets.

@johanste
Created July 17, 2020 22:59
Show Gist options
  • Save johanste/c1d418a706baaaee0dd3b14b9980ac52 to your computer and use it in GitHub Desktop.
Save johanste/c1d418a706baaaee0dd3b14b9980ac52 to your computer and use it in GitHub Desktop.
Mix of decorators and module level function to compose multiple capabilities (Python)
import carbon
video_stream = ...
# Module level function lets you pick a stream, "features" and go
# (this assumes there is a default configuration for each feature/category/model)
for event in carbon.recognize(video_stream, features=['faceDetection', 'vehicleDetection']):
print(event)
# Slightly more involved scenario with custom configurations and a dedicated session...
# The top level recognize would also take specific configuration in addition to names of features
face_detection_configuration = carbon.FaceDetection(configuration_property1 = ...)
vehicle_detection_configuration = carbon.VehicleDetection(some_other_configuration_property = ...)
session = carbon.Session(features=[face_detection_configuration, vehicle_detection_configuration])
@session.on_event('faceDetected')
def handle_face_detected(event):
print(event)
@session.on_event('vehicleDetected')
def handle_on_vehicle_detected(event):
print(event)
# Now, I can also reach in to the session (since it is - if you use the "normal" flow bound here)
# Alternatively we can have a session parameter or a session.current that one can use...
# This is riffing on the "start/stop/add new features as you go along scenario"
session.stop_feature('faceDetection') # No longer interested in faces
session.start_feature('dogDetection') # But I do like dogs...
# Now, if you don't like the recognizer pattern, you can also bind event handlers directly
# This is equivalent to what the decorator does "under the hood"
session.add_handler('puppyDetection', lambda event: print('Yay, puppy!'))
session.recognize(video_stream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment