From 60575a98a74a63b002dbef6e8041be5591f2ca40 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 13 Nov 2022 09:41:33 -0800 Subject: [PATCH] tensorflow-lite: perf improvements --- plugins/objectdetector/src/main.ts | 2 +- .../tensorflow-lite/src/tflite/__init__.py | 48 +++++++++++-------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/plugins/objectdetector/src/main.ts b/plugins/objectdetector/src/main.ts index 8d28a5ed0..836e67b1e 100644 --- a/plugins/objectdetector/src/main.ts +++ b/plugins/objectdetector/src/main.ts @@ -440,7 +440,7 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase `${d.detection.className} (${d.detection.score})`).join(', ')); + this.console.log('current detections:', this.detectionState.previousDetections.map(d => `${d.detection.className} (${d.detection.score}, ${d.detection.boundingBox?.join(', ')})`).join(', ')); } return newOrBetterDetection; diff --git a/plugins/tensorflow-lite/src/tflite/__init__.py b/plugins/tensorflow-lite/src/tflite/__init__.py index d4856436f..6f843a3a1 100644 --- a/plugins/tensorflow-lite/src/tflite/__init__.py +++ b/plugins/tensorflow-lite/src/tflite/__init__.py @@ -1,7 +1,6 @@ from __future__ import annotations -from lib2to3.pytree import convert from typing_extensions import TypedDict -from scrypted_sdk.types import ObjectDetectionModel, ObjectDetectionResult, ObjectsDetected, Setting, MediaStreamDestination +from scrypted_sdk.types import ObjectDetectionModel, ObjectDetectionResult, ObjectsDetected, Setting import threading import io from .common import * @@ -406,25 +405,31 @@ class TensorFlowLitePlugin(DetectPlugin, scrypted_sdk.BufferConverter, scrypted_ return ret, RawImage(image) - detections: List[ObjectDetectionResult] = [] - while len(ret['detections']): - d = ret['detections'].pop() - found = False - for c in detections: - same, box = is_same_detection(d, c) - if same: - # encompass this box and score - d['boundingBox'] = box - d['score'] = max(d['score'], c['score']) - # remove from current detections list - detections = list(filter(lambda r: r != c, detections)) - # run dedupe again with this new larger item - ret['detections'].append(d) - found = True - break + detections: List[ObjectDetectionResult] - if not found: - detections.append(d) + def dedupe_detections(): + nonlocal detections + detections = [] + while len(ret['detections']): + d = ret['detections'].pop() + found = False + for c in detections: + same, box = is_same_detection(d, c) + if same: + # encompass this box and score + d['boundingBox'] = box + d['score'] = max(d['score'], c['score']) + # remove from current detections list + detections = list(filter(lambda r: r != c, detections)) + # run dedupe again with this new larger item + ret['detections'].append(d) + found = True + break + + if not found: + detections.append(d) + + dedupe_detections() for detection in detections: if detection['score'] >= second_score_threshold: @@ -459,7 +464,8 @@ class TensorFlowLitePlugin(DetectPlugin, scrypted_sdk.BufferConverter, scrypted_ ret['detections'].extend(filtered[:1]) detection_session.previousDetections = ret['detections'] - print(ret['detections']) + dedupe_detections() + ret['detections'] = detections return ret, RawImage(image) def run_detection_gstsample(self, detection_session: TensorFlowLiteSession, gstsample, settings: Any, src_size, convert_to_src_size) -> Tuple[ObjectsDetected, Image.Image]: