tensorflow-lite: perf improvements

This commit is contained in:
Koushik Dutta
2022-11-13 09:41:33 -08:00
parent 7233334898
commit 60575a98a7
2 changed files with 28 additions and 22 deletions

View File

@@ -440,7 +440,7 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
this.extendedObjectDetect();
}
if (found.length || showAll) {
this.console.log('current detections:', this.detectionState.previousDetections.map(d => `${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;

View File

@@ -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]: