From dc5f4b4e9edf533590a2326fb1340ee59819b3b6 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 21 Jan 2022 23:59:38 -0800 Subject: [PATCH] plugins: cleanup package sizes --- common/src/wrtc-convertors.ts | 24 +++--- plugins/core/package-lock.json | 4 +- plugins/core/package.json | 2 +- plugins/homekit/package-lock.json | 4 +- plugins/homekit/package.json | 2 +- plugins/homekit/src/battery.ts | 3 +- plugins/homekit/src/common.ts | 4 +- plugins/homekit/src/hap.ts | 8 +- plugins/homekit/src/main.ts | 2 +- plugins/homekit/src/types/camera.ts | 7 +- .../src/types/camera/camera-recording.ts | 4 +- plugins/homekit/src/types/doorbell.ts | 3 +- plugins/rtsp/src/rtsp.ts | 2 +- .../tensorflow-lite/src/detect/__init__.py | 10 +-- .../tensorflow-lite/src/tflite/__init__.py | 76 ++++--------------- .../src/tflite/third_party/sort/__init__.py | 1 - 16 files changed, 56 insertions(+), 100 deletions(-) delete mode 120000 plugins/tensorflow-lite/src/tflite/third_party/sort/__init__.py diff --git a/common/src/wrtc-convertors.ts b/common/src/wrtc-convertors.ts index 51bf00086..f542ff3b1 100644 --- a/common/src/wrtc-convertors.ts +++ b/common/src/wrtc-convertors.ts @@ -5,16 +5,19 @@ import { listenZero } from "./listen-cluster"; import { ffmpegLogInitialOutput } from "./media-helpers"; let wrtc: any; -try { - wrtc = require('wrtc'); -} -catch (e) { - console.warn('loading wrtc failed. trying @koush/wrtc fallback.'); - wrtc = require('@koush/wrtc'); -} +function initalizeWebRtc() { + if (wrtc) + return; + try { + wrtc = require('wrtc'); + } + catch (e) { + console.warn('loading wrtc failed. trying @koush/wrtc fallback.'); + wrtc = require('@koush/wrtc'); + } -Object.assign(global, wrtc); -const { RTCVideoSource, RTCAudioSource } = wrtc.nonstandard; + Object.assign(global, wrtc); +} interface RTCSession { pc: RTCPeerConnection; @@ -90,6 +93,7 @@ export function addBuiltins(console: Console, mediaManager: MediaManager) { ], }; + initalizeWebRtc(); const pc = new RTCPeerConnection(configuration); const id = Math.random().toString(); const session: RTCSession = { @@ -106,6 +110,8 @@ export function addBuiltins(console: Console, mediaManager: MediaManager) { } } + const { RTCVideoSource, RTCAudioSource } = wrtc.nonstandard; + const videoSource = new RTCVideoSource(); pc.addTrack(videoSource.createTrack()); diff --git a/plugins/core/package-lock.json b/plugins/core/package-lock.json index d9a3ab908..9811ce6c4 100644 --- a/plugins/core/package-lock.json +++ b/plugins/core/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/core", - "version": "0.0.179", + "version": "0.0.181", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/core", - "version": "0.0.179", + "version": "0.0.181", "license": "Apache-2.0", "dependencies": { "@koush/wrtc": "^0.5.0", diff --git a/plugins/core/package.json b/plugins/core/package.json index 2ad698a00..466b4f796 100644 --- a/plugins/core/package.json +++ b/plugins/core/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/core", - "version": "0.0.179", + "version": "0.0.181", "description": "Scrypted Core plugin. Provides the UI, websocket, and engine.io APIs.", "author": "Scrypted", "license": "Apache-2.0", diff --git a/plugins/homekit/package-lock.json b/plugins/homekit/package-lock.json index e6780e49b..4e2a89e56 100644 --- a/plugins/homekit/package-lock.json +++ b/plugins/homekit/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/homekit", - "version": "0.0.171", + "version": "0.0.172", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/homekit", - "version": "0.0.171", + "version": "0.0.172", "dependencies": { "hap-nodejs": "file:../../external/HAP-NodeJS", "lodash": "^4.17.21", diff --git a/plugins/homekit/package.json b/plugins/homekit/package.json index 4f1daaf8d..c05593d11 100644 --- a/plugins/homekit/package.json +++ b/plugins/homekit/package.json @@ -40,5 +40,5 @@ "@types/qrcode": "^1.4.1", "@types/url-parse": "^1.4.3" }, - "version": "0.0.171" + "version": "0.0.172" } diff --git a/plugins/homekit/src/battery.ts b/plugins/homekit/src/battery.ts index 3cedb9bd1..4f01aa8e0 100644 --- a/plugins/homekit/src/battery.ts +++ b/plugins/homekit/src/battery.ts @@ -1,6 +1,5 @@ import { Battery, ScryptedDevice, ScryptedInterface } from "@scrypted/sdk"; -import { Accessory, Characteristic } from "./hap"; -import { Battery as HAPBattery, BatteryLevel, StatusLowBattery } from "hap-nodejs/src/lib/definitions"; +import { Battery as HAPBattery, StatusLowBattery, Accessory, Characteristic } from "./hap"; import { bindCharacteristic } from "./common"; export function maybeAddBatteryService(device: ScryptedDevice & Battery, accessory: Accessory) { diff --git a/plugins/homekit/src/common.ts b/plugins/homekit/src/common.ts index e7fa5d83c..e14984465 100644 --- a/plugins/homekit/src/common.ts +++ b/plugins/homekit/src/common.ts @@ -1,8 +1,6 @@ import { EventDetails, EventListener, EventListenerRegister, Refresh, ScryptedDevice, ScryptedDeviceType, ScryptedInterface } from '@scrypted/sdk'; -import { CharacteristicEventTypes, WithUUID } from 'hap-nodejs'; -import { Characteristic } from 'hap-nodejs/src'; -import { Accessory, Service, SnapshotRequest } from './hap'; +import { Accessory, Service, SnapshotRequest, Characteristic, CharacteristicEventTypes, WithUUID } from './hap'; export interface DummyDevice { interfaces?: string[]; diff --git a/plugins/homekit/src/hap.ts b/plugins/homekit/src/hap.ts index 640c28130..203ee93bb 100644 --- a/plugins/homekit/src/hap.ts +++ b/plugins/homekit/src/hap.ts @@ -1 +1,7 @@ -export * from '../../../external/HAP-NodeJS/src/index'; +export * from 'hap-nodejs/src/index'; +export * from "hap-nodejs/src/lib/definitions"; +export * from 'hap-nodejs/src/lib/util/eventedhttp'; +export * from 'hap-nodejs/src/lib/definitions'; +export * from 'hap-nodejs/src'; +export * from 'hap-nodejs/src/lib/camera'; +export * from 'hap-nodejs/src/lib/camera/RecordingManagement'; diff --git a/plugins/homekit/src/main.ts b/plugins/homekit/src/main.ts index 6e9308afb..8c1bf0244 100644 --- a/plugins/homekit/src/main.ts +++ b/plugins/homekit/src/main.ts @@ -9,7 +9,7 @@ import { randomBytes } from 'crypto'; import qrcode from 'qrcode'; import packageJson from "../package.json"; import { randomPinCode } from './pincode'; -import { EventedHTTPServer } from 'hap-nodejs/dist/lib/util/eventedhttp'; +import { EventedHTTPServer } from './hap'; import { getHAPUUID, initializeHapStorage } from './hap-utils'; const { systemManager, deviceManager } = sdk; diff --git a/plugins/homekit/src/types/camera.ts b/plugins/homekit/src/types/camera.ts index fc8632355..1b85c9881 100644 --- a/plugins/homekit/src/types/camera.ts +++ b/plugins/homekit/src/types/camera.ts @@ -11,16 +11,13 @@ import { once } from 'events'; import debounce from 'lodash/debounce'; import { CameraRecordingDelegate } from '../hap'; -import { AudioRecordingCodec, AudioRecordingCodecType, AudioRecordingSamplerate, CameraRecordingOptions } from 'hap-nodejs/src/lib/camera/RecordingManagement'; import { ffmpegLogInitialOutput } from '@scrypted/common/src/media-helpers'; import { RtpDemuxer } from '../rtp/rtp-demuxer'; import { HomeKitRtpSink, startRtpSink } from '../rtp/rtp-ffmpeg-input'; -import { ContactSensor, OccupancySensor } from 'hap-nodejs/src/lib/definitions'; import { handleFragmentsRequests, iframeIntervalSeconds } from './camera/camera-recording'; import { createSnapshotHandler } from './camera/camera-snapshot'; import { evalRequest } from './camera/camera-transcode'; -import { CharacteristicEventTypes, DataStreamConnection, Service, WithUUID } from 'hap-nodejs/src'; -import { RecordingManagement } from 'hap-nodejs/src/lib/camera'; +import { AudioRecordingCodec, AudioRecordingCodecType, AudioRecordingSamplerate, CameraRecordingOptions, RecordingManagement, OccupancySensor, CharacteristicEventTypes, DataStreamConnection, Service, WithUUID } from '../hap'; import { defaultObjectDetectionContactSensorTimeout } from '../camera-mixin'; import os from 'os'; @@ -204,7 +201,7 @@ addSupportedType({ video: { }, }, selectedStream || {}); - const bitrate = request.video.max_bit_rate * 1000; + const bitrate = 10000000;//request.video.max_bit_rate * 1000; reconfigured.video.bitrate = bitrate; device.setVideoStreamOptions(reconfigured); diff --git a/plugins/homekit/src/types/camera/camera-recording.ts b/plugins/homekit/src/types/camera/camera-recording.ts index bcc4b4bfa..56736a0a6 100644 --- a/plugins/homekit/src/types/camera/camera-recording.ts +++ b/plugins/homekit/src/types/camera/camera-recording.ts @@ -1,15 +1,13 @@ import { FFMpegInput, MotionSensor, ScryptedDevice, ScryptedMimeTypes, VideoCamera, AudioSensor, MediaStreamOptions } from '@scrypted/sdk' -import { H264Level, H264Profile } from '../../hap'; import net from 'net'; import sdk from '@scrypted/sdk'; -import { AudioRecordingCodecType, AudioRecordingSamplerateValues, CameraRecordingConfiguration } from 'hap-nodejs/src/lib/camera/RecordingManagement'; +import { AudioRecordingCodecType, AudioRecordingSamplerateValues, CameraRecordingConfiguration } from '../../hap'; import { FFMpegFragmentedMP4Session, startFFMPegFragmetedMP4Session } from '@scrypted/common/src/ffmpeg-mp4-parser-session'; import { evalRequest } from './camera-transcode'; import { parseFragmentedMP4 } from '@scrypted/common/src/stream-parser'; -import { levelToFfmpeg, profileToFfmpeg } from './camera-utils'; const { log, mediaManager, deviceManager } = sdk; diff --git a/plugins/homekit/src/types/doorbell.ts b/plugins/homekit/src/types/doorbell.ts index 0536e59d8..5ac8d72fd 100644 --- a/plugins/homekit/src/types/doorbell.ts +++ b/plugins/homekit/src/types/doorbell.ts @@ -1,8 +1,7 @@ import { BinarySensor, ScryptedDevice, ScryptedDeviceType, ScryptedInterface } from '@scrypted/sdk' -import { ContactSensor, StatelessProgrammableSwitch } from 'hap-nodejs/src/lib/definitions'; +import { Characteristic, CharacteristicEventTypes, CharacteristicGetCallback, Service, ContactSensor, StatelessProgrammableSwitch } from '../hap'; import { addSupportedType, bindCharacteristic, DummyDevice, HomeKitSession, supportedTypes } from '../common' -import { Characteristic, CharacteristicEventTypes, CharacteristicGetCallback, Service } from '../hap'; import { makeAccessory } from './common'; addSupportedType({ diff --git a/plugins/rtsp/src/rtsp.ts b/plugins/rtsp/src/rtsp.ts index da82033fb..44dd8a13d 100644 --- a/plugins/rtsp/src/rtsp.ts +++ b/plugins/rtsp/src/rtsp.ts @@ -61,7 +61,7 @@ export class RtspCamera extends CameraBase { // Note the trailing colon. // issue: https://github.com/koush/scrypted/issues/134 const parsedUrl = url.parse(vso.url); - this.console.log('rtsp stream url', parsedUrl.toString()); + this.console.log('rtsp stream url', vso.url); const username = this.storage.getItem("username"); const password = this.storage.getItem("password"); if (username) { diff --git a/plugins/tensorflow-lite/src/detect/__init__.py b/plugins/tensorflow-lite/src/detect/__init__.py index 7c698b6e4..a12c0e9e4 100644 --- a/plugins/tensorflow-lite/src/detect/__init__.py +++ b/plugins/tensorflow-lite/src/detect/__init__.py @@ -14,7 +14,7 @@ import time import os import binascii from urllib.parse import urlparse -import multiprocessing +import threading from pipeline import run_pipeline from gi.repository import Gst @@ -37,7 +37,7 @@ class PipelineValve: allowPacketCounter: int def __init__(self, gst, name) -> None: self.allowPacketCounter = 1 - self.mutex = multiprocessing.Lock() + self.mutex = threading.Lock() valve = gst.get_by_name(name + "Valve") self.pad = valve.get_static_pad("src") self.name = name @@ -91,7 +91,7 @@ class DetectionSession: self.future = Future() self.running = False self.attached = False - self.mutex = multiprocessing.Lock() + self.mutex = threading.Lock() self.valve: PipelineValve = None self.last_sample = time.time() @@ -120,7 +120,7 @@ class DetectPlugin(scrypted_sdk.ScryptedDeviceBase, ObjectDetection): def __init__(self, nativeId: str | None = None): super().__init__(nativeId=nativeId) self.detection_sessions: Mapping[str, DetectionSession] = {} - self.session_mutex = multiprocessing.Lock() + self.session_mutex = threading.Lock() def detection_event(self, detection_session: DetectionSession, detection_result: ObjectsDetected, event_buffer: bytes = None): detection_result['detectionId'] = detection_session.id @@ -211,7 +211,7 @@ class DetectPlugin(scrypted_sdk.ScryptedDeviceBase, ObjectDetection): detection_session.future.add_done_callback( lambda _: self.end_session(detection_session)) - if detection_session and time.time() - detection_session.last_sample > 30: + if detection_session and time.time() - detection_session.last_sample > 30 and not mediaObject: print('detection session has not received a sample in 30 seconds, terminating', detection_session.id) ending = True diff --git a/plugins/tensorflow-lite/src/tflite/__init__.py b/plugins/tensorflow-lite/src/tflite/__init__.py index 39b60f559..a83500b36 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 scrypted_sdk.types import ObjectDetectionModel, ObjectDetectionResult, ObjectsDetected, Setting -from .third_party.sort import Sort -import multiprocessing +import threading import io from .common import * from PIL import Image @@ -12,17 +11,15 @@ from pycoral.utils.edgetpu import list_edge_tpus from pycoral.utils.edgetpu import make_interpreter import tflite_runtime.interpreter as tflite import re -import numpy as np import scrypted_sdk from typing import Any, List from detect import DetectionSession, DetectPlugin -class TrackerDetectionSession(DetectionSession): +class TensorFlowLiteSession(DetectionSession): def __init__(self) -> None: super().__init__() - self.tracker = Sort() def parse_label_contents(contents: str): @@ -57,7 +54,7 @@ class TensorFlowLitePlugin(DetectPlugin): 'fs/mobilenet_ssd_v2_coco_quant_postprocess.tflite').read() self.interpreter = tflite.Interpreter(model_content=model) self.interpreter.allocate_tensors() - self.mutex = multiprocessing.Lock() + self.mutex = threading.Lock() async def getDetectionModel(self) -> ObjectDetectionModel: _, height, width, channels = self.interpreter.get_input_details()[ @@ -79,58 +76,19 @@ class TensorFlowLitePlugin(DetectPlugin): d['settings'] = [setting] return d - def create_detection_result(self, objs, size, tracker: Sort = None, convert_to_src_size=None): + def create_detection_result(self, objs, size, convert_to_src_size=None): detections: List[ObjectDetectionResult] = [] detection_result: ObjectsDetected = {} detection_result['detections'] = detections detection_result['inputDimensions'] = size - tracker_detections = [] - for obj in objs: - element = [] - element.append(obj.bbox.xmin) - element.append(obj.bbox.ymin) - element.append(obj.bbox.xmax) - element.append(obj.bbox.ymax) - element.append(obj.score) - tracker_detections.append(element) - - tracker_detections = np.array(tracker_detections) - trdata = [] - trackerFlag = False - if tracker and tracker_detections.any(): - trdata = tracker.update(tracker_detections) - trackerFlag = True - - if trackerFlag and (np.array(trdata)).size: - for td in trdata: - x0, y0, x1, y1, trackID = td[0].item(), td[1].item( - ), td[2].item(), td[3].item(), td[4].item() - overlap = 0 - for ob in objs: - dx0, dy0, dx1, dy1 = ob.bbox.xmin, ob.bbox.ymin, ob.bbox.xmax, ob.bbox.ymax - area = (min(dx1, x1)-max(dx0, x0)) * \ - (min(dy1, y1)-max(dy0, y0)) - if (area > overlap): - overlap = area - obj = ob - - detection: ObjectDetectionResult = {} - detection['id'] = str(trackID) - detection['boundingBox'] = ( - obj.bbox.xmin, obj.bbox.ymin, obj.bbox.xmax - obj.bbox.xmin, obj.bbox.ymax - obj.bbox.ymin) - detection['className'] = self.labels.get(obj.id, obj.id) - detection['score'] = obj.score - detections.append(detection) - else: - for obj in objs: - detection: ObjectDetectionResult = {} - detection['boundingBox'] = ( - obj.bbox.xmin, obj.bbox.ymin, obj.bbox.xmax - obj.bbox.xmin, obj.bbox.ymax - obj.bbox.ymin) - detection['className'] = self.labels.get(obj.id, obj.id) - detection['score'] = obj.score - detections.append(detection) + detection: ObjectDetectionResult = {} + detection['boundingBox'] = ( + obj.bbox.xmin, obj.bbox.ymin, obj.bbox.xmax - obj.bbox.xmin, obj.bbox.ymax - obj.bbox.ymin) + detection['className'] = self.labels.get(obj.id, obj.id) + detection['score'] = obj.score + detections.append(detection) if convert_to_src_size: detections = detection_result['detections'] @@ -155,29 +113,25 @@ class TensorFlowLitePlugin(DetectPlugin): 'score_threshold', score_threshold)) return score_threshold - def run_detection_jpeg(self, detection_session: TrackerDetectionSession, image_bytes: bytes, settings: Any) -> ObjectsDetected: + def run_detection_jpeg(self, detection_session: TensorFlowLiteSession, image_bytes: bytes, settings: Any) -> ObjectsDetected: stream = io.BytesIO(image_bytes) image = Image.open(stream) _, scale = common.set_resized_input( self.interpreter, image.size, lambda size: image.resize(size, Image.ANTIALIAS)) - tracker = None - if detection_session: - tracker = detection_session.tracker - score_threshold = self.parse_settings(settings) with self.mutex: self.interpreter.invoke() objs = detect.get_objects( self.interpreter, score_threshold=score_threshold, image_scale=scale) - return self.create_detection_result(objs, image.size, tracker=tracker) + return self.create_detection_result(objs, image.size) def get_detection_input_size(self, src_size): return input_size(self.interpreter) - def run_detection_gstsample(self, detection_session: TrackerDetectionSession, gstsample, settings: Any, src_size, convert_to_src_size) -> ObjectsDetected: + def run_detection_gstsample(self, detection_session: TensorFlowLiteSession, gstsample, settings: Any, src_size, convert_to_src_size) -> ObjectsDetected: score_threshold = self.parse_settings(settings) gst_buffer = gstsample.get_buffer() @@ -186,7 +140,7 @@ class TensorFlowLitePlugin(DetectPlugin): objs = detect.get_objects( self.interpreter, score_threshold=score_threshold) - return self.create_detection_result(objs, src_size, detection_session.tracker, convert_to_src_size) + return self.create_detection_result(objs, src_size, convert_to_src_size) def create_detection_session(self): - return TrackerDetectionSession() + return TensorFlowLiteSession() diff --git a/plugins/tensorflow-lite/src/tflite/third_party/sort/__init__.py b/plugins/tensorflow-lite/src/tflite/third_party/sort/__init__.py deleted file mode 120000 index 36e20806f..000000000 --- a/plugins/tensorflow-lite/src/tflite/third_party/sort/__init__.py +++ /dev/null @@ -1 +0,0 @@ -../../../../../../external/sort/sort.py \ No newline at end of file