Skip to content

Commit

Permalink
python-codecs: add gray decoding support
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Mar 17, 2023
1 parent aedb985 commit 9044e78
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 17 additions & 3 deletions plugins/python-codecs/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions sdk/types/scrypted_python/scrypted_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class HttpResponseOptions(TypedDict):

class ImageOptions(TypedDict):
crop: Any
format: Any | Any | Any
format: Any | Any | Any | Any
resize: Any
pass

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -693,7 +694,7 @@ class VideoClipOptions(TypedDict):

class VideoFrameGeneratorOptions(TypedDict):
crop: Any
format: Any | Any | Any
format: Any | Any | Any | Any
resize: Any
pass

Expand Down
3 changes: 2 additions & 1 deletion sdk/types/src/types.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9044e78

Please sign in to comment.