From 9044e782b23b832746f0e9754cebd6e9eb266dfc Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 16 Mar 2023 20:37:28 -0700 Subject: [PATCH] python-codecs: add gray decoding support --- plugins/python-codecs/src/main.py | 20 ++++++++++++++++--- .../scrypted_python/scrypted_sdk/types.py | 5 +++-- sdk/types/src/types.input.ts | 3 ++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/plugins/python-codecs/src/main.py b/plugins/python-codecs/src/main.py index ba2cf55f51..b3f07aea0c 100644 --- a/plugins/python-codecs/src/main.py +++ b/plugins/python-codecs/src/main.py @@ -219,7 +219,22 @@ async def generateVideoFramesGstreamer(mediaObject: scrypted_sdk.MediaObject, op if videoCodec == 'h264': videosrc += ' ! rtph264depay ! h264parse' - videosrc += ' ! decodebin ! queue leaky=downstream max-size-buffers=0 ! videoconvert ! video/x-raw,format=RGB' + videocaps = 'video/x-raw' + # if options and options.get('resize'): + # videocaps = 'videoscale ! video/x-raw,width={width},height={height}'.format(width=options['resize']['width'], height=options['resize']['height']) + + format = options and options.get('format') + # I420 is a cheap way to get gray out of an h264 stream without color conversion. + if format == 'gray': + format = 'I420' + bands = 1 + else: + format = 'RGB' + bands = 3 + + videocaps += ',format={format}'.format(format=format) + + videosrc += ' ! decodebin ! queue leaky=downstream max-size-buffers=0 ! videoconvert ! ' + videocaps gst, gen = createPipelineIterator(videosrc) async for gstsample in gen(): @@ -232,8 +247,7 @@ async def generateVideoFramesGstreamer(mediaObject: scrypted_sdk.MediaObject, op continue try: - # pyvips.Image.new_from_memory(info.data, width, height, 3, pyvips.BandFormat.UCHAR) - vips = pyvips.Image.new_from_memory(info.data, width, height, 3, pyvips.BandFormat.UCHAR) + vips = pyvips.Image.new_from_memory(info.data, width, height, bands, pyvips.BandFormat.UCHAR) vipsImage = VipsImage(vips) try: mo = await createVipsMediaObject(VipsImage(vips)) diff --git a/sdk/types/scrypted_python/scrypted_sdk/types.py b/sdk/types/scrypted_python/scrypted_sdk/types.py index a59ac24854..5c1805e72d 100644 --- a/sdk/types/scrypted_python/scrypted_sdk/types.py +++ b/sdk/types/scrypted_python/scrypted_sdk/types.py @@ -233,7 +233,7 @@ class HttpResponseOptions(TypedDict): class ImageOptions(TypedDict): crop: Any - format: Any | Any | Any + format: Any | Any | Any | Any resize: Any pass @@ -486,6 +486,7 @@ class ObjectDetectionGeneratorSession(TypedDict): class ObjectDetectionModel(TypedDict): classes: list[str] + inputFormat: Any | Any | Any inputSize: list[float] name: str settings: list[Setting] @@ -693,7 +694,7 @@ class VideoClipOptions(TypedDict): class VideoFrameGeneratorOptions(TypedDict): crop: Any - format: Any | Any | Any + format: Any | Any | Any | Any resize: Any pass diff --git a/sdk/types/src/types.input.ts b/sdk/types/src/types.input.ts index fa30f1d26a..4c7bf641cb 100644 --- a/sdk/types/src/types.input.ts +++ b/sdk/types/src/types.input.ts @@ -1296,6 +1296,7 @@ export interface ObjectDetectionSession extends ObjectDetectionGeneratorSession export interface ObjectDetectionModel extends ObjectDetectionTypes { name: string; inputSize?: number[]; + inputFormat?: 'gray' | 'rgb' | 'rgba'; settings: Setting[]; triggerClasses?: string[]; } @@ -1328,7 +1329,7 @@ export interface ImageOptions { width?: number, height?: number, }; - format?: 'rgba' | 'rgb' | 'jpg'; + format?: 'gray' | 'rgba' | 'rgb' | 'jpg'; } export interface Image { width: number;