Skip to content

Instantly share code, notes, and snippets.

View johanste's full-sized avatar

Johan Stenberg (MSFT) johanste

  • Microsoft Corporation
  • Redmond, WA
View GitHub Profile
@johanste
johanste / RecognizerAsDispatcher.py
Last active July 15, 2020 03:56
Decorator instead of event subscribers/handlers
import collections
import json
import typing
class RecognitionCancelledError(Exception):
...
ConditionalHandler = collections.namedtuple('ConditionalHandler', 'condition handler')
class Recognizer:
@johanste
johanste / pull_loop.py
Created July 15, 2020 20:21
Pull events from a stream of detected features
data = ...
recognizer = Recognizer(...)
for event in recognizer.recognize():
if event.type == 'speakerRecognized':
...
elif event.type == 'done':
...
@johanste
johanste / carbonsession.py
Created July 17, 2020 22:59
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...
@johanste
johanste / metaregistergridevent.py
Created July 23, 2020 01:33
Registering event types for event grid messages...
class AiEventMeta(type):
def __init__(cls, name, bases, nmspc):
super().__init__(name, bases, nmspc)
if not hasattr(cls, 'event_type_registry'):
setattr(cls, 'event_type_registry', {})
if hasattr(cls, 'EVENT_TYPE_NAME'):
cls.event_type_registry[cls.EVENT_TYPE_NAME] = cls
class AiEvent(metaclass=AiEventMeta):
@johanste
johanste / computation.json
Last active July 31, 2020 16:51
Training/openapi example for service description
{
"swagger": "2.0",
"info": {
"title": "Example service",
"version": "1.1"
},
"paths": {
"/ComputeNodes": {
"parameters": [
{
@johanste
johanste / example.json
Last active October 5, 2020 22:28
Polymorphic swagger
{
"swagger": "2.0",
"info": {
"title": "Example of polymorphic models, hypothetical document recognition style",
"version": "1.0"
},
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {
"/analyze": {
@johanste
johanste / openapi.py
Created December 4, 2020 22:53
Swagger as TypedDict (WIP)
import typing
class InfoFragment(typing.TypedDict):
title: str
version: str
OptionalInAttribute = typing.TypedDict(
@johanste
johanste / overloads-in-swagger-2.0.json
Last active January 13, 2021 21:55
Show how to use the (autorest) x-ms-paths extension to overload operations for the same path.
{
"swagger": "2.0",
"info": {
"title": "Show x-ms-paths",
"version": "1.0"
},
"x-ms-paths": {
/* Note - fake query parameter to make the path unique. Not actually used in the API.
You can also differentiate using a different path parameter name if the path is parameterized
*/
@johanste
johanste / overrideresourceproperties.json
Created February 8, 2021 23:13
More specific "derived" swagger type
{
"swagger": "2.0",
"info": {
"title": "Examples of scenarios - #1, more constrained children",
"version": "1.0"
},
"paths": [],
"definitions": {
"GenericResource": {
"type": "object",
@johanste
johanste / llc.py
Created March 4, 2021 04:23
Prototype generic llc request builder
import json
import typing
import purl
HeadersType = typing.Union[typing.Sequence[typing.Tuple[str, typing.Any]], typing.Mapping[str, typing.Any], None]
JsonBodyType = typing.Union[typing.Mapping[str, "JsonBodyType"], str, int, float, bool, None]
ParamType2 = typing.Union[typing.Sequence[typing.Tuple[str, typing.Any]], typing.Mapping[str, typing.Any]]
ParamType = typing.Union[typing.Sequence[typing.Tuple[str, typing.Any]], typing.Mapping[str, typing.Any]]