diff --git a/plugins/coreml/package-lock.json b/plugins/coreml/package-lock.json index d863ad585..287e04c01 100644 --- a/plugins/coreml/package-lock.json +++ b/plugins/coreml/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/coreml", - "version": "0.1.11", + "version": "0.1.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/coreml", - "version": "0.1.11", + "version": "0.1.12", "devDependencies": { "@scrypted/sdk": "file:../../sdk" } diff --git a/plugins/coreml/package.json b/plugins/coreml/package.json index a6684b0a0..361b073b5 100644 --- a/plugins/coreml/package.json +++ b/plugins/coreml/package.json @@ -41,5 +41,5 @@ "devDependencies": { "@scrypted/sdk": "file:../../sdk" }, - "version": "0.1.11" + "version": "0.1.12" } diff --git a/plugins/opencv/src/opencv/__init__.py b/plugins/opencv/src/opencv/__init__.py index b62e40b26..1e3bf7fbc 100644 --- a/plugins/opencv/src/opencv/__init__.py +++ b/plugins/opencv/src/opencv/__init__.py @@ -9,7 +9,7 @@ import imutils import numpy as np import scrypted_sdk from PIL import Image -from scrypted_sdk.types import (ObjectDetectionGeneratorSession, +from scrypted_sdk.types import (ObjectDetectionGeneratorSession,ObjectDetectionSession, ObjectDetectionResult, ObjectsDetected, Setting, VideoFrame) @@ -116,35 +116,37 @@ class OpenCVPlugin(DetectPlugin): blur = int(settings.get('blur', blur)) return area, threshold, interval, blur - def detect(self, frame, settings: Any, detection_session: OpenCVDetectionSession, src_size, convert_to_src_size) -> ObjectsDetected: + def detect(self, frame, detection_session: ObjectDetectionSession, src_size, convert_to_src_size) -> ObjectsDetected: + session: OpenCVDetectionSession = detection_session['settings']['session'] + settings = detection_session and detection_session.get('settings', None) area, threshold, interval, blur = self.parse_settings(settings) gray = frame - detection_session.curFrame = cv2.GaussianBlur( - gray, (blur, blur), 0, dst=detection_session.curFrame) + session.curFrame = cv2.GaussianBlur( + gray, (blur, blur), 0, dst=session.curFrame) detections: List[ObjectDetectionResult] = [] detection_result: ObjectsDetected = {} detection_result['detections'] = detections detection_result['inputDimensions'] = src_size - if detection_session.previous_frame is None: - detection_session.previous_frame = detection_session.curFrame - detection_session.curFrame = None + if session.previous_frame is None: + session.previous_frame = session.curFrame + session.curFrame = None return detection_result - detection_session.frameDelta = cv2.absdiff( - detection_session.previous_frame, detection_session.curFrame, dst=detection_session.frameDelta) - tmp = detection_session.curFrame - detection_session.curFrame = detection_session.previous_frame - detection_session.previous_frame = tmp + session.frameDelta = cv2.absdiff( + session.previous_frame, session.curFrame, dst=session.frameDelta) + tmp = session.curFrame + session.curFrame = session.previous_frame + session.previous_frame = tmp - _, detection_session.thresh = cv2.threshold( - detection_session.frameDelta, threshold, 255, cv2.THRESH_BINARY, dst=detection_session.thresh) - detection_session.dilated = cv2.dilate( - detection_session.thresh, None, iterations=2, dst=detection_session.dilated) + _, session.thresh = cv2.threshold( + session.frameDelta, threshold, 255, cv2.THRESH_BINARY, dst=session.thresh) + session.dilated = cv2.dilate( + session.thresh, None, iterations=2, dst=session.dilated) fcontours = cv2.findContours( - detection_session.dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + session.dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(fcontours) @@ -205,24 +207,16 @@ class OpenCVPlugin(DetectPlugin): detection_session.cap = None return super().end_session(detection_session) - async def generateObjectDetections(self, videoFrames: Any, session: ObjectDetectionGeneratorSession = None) -> Any: - try: - ds = OpenCVDetectionSession() - videoFrames = await scrypted_sdk.sdk.connectRPCObject(videoFrames) - async for videoFrame in videoFrames: - detected = await self.run_detection_videoframe(videoFrame, session and session.get('settings'), ds) - yield { - '__json_copy_serialize_children': True, - 'detected': detected, - 'videoFrame': videoFrame, - } - finally: - try: - await videoFrames.aclose() - except: - pass + async def generateObjectDetections(self, videoFrames: Any, detection_session: ObjectDetectionGeneratorSession = None) -> Any: + if not detection_session: + detection_session = {} + if not detection_session.get('settings'): + detection_session['settings'] = {} + settings = detection_session['settings'] + settings['session'] = OpenCVDetectionSession() + return super().generateObjectDetections(videoFrames, detection_session) - async def run_detection_videoframe(self, videoFrame: VideoFrame, settings: Any, detection_session: OpenCVDetectionSession) -> ObjectsDetected: + async def run_detection_videoframe(self, videoFrame: VideoFrame, detection_session: ObjectDetectionSession) -> ObjectsDetected: width = videoFrame.width height = videoFrame.height @@ -267,5 +261,5 @@ class OpenCVPlugin(DetectPlugin): def convert_to_src_size(point): return point[0] * scale, point[1] * scale mat = np.ndarray((height, width, 1), buffer=buffer, dtype=np.uint8) - detections = self.detect(mat, settings, detection_session, (videoFrame.width, videoFrame.height), convert_to_src_size) + detections = self.detect(mat, detection_session, (videoFrame.width, videoFrame.height), convert_to_src_size) return detections diff --git a/plugins/tensorflow-lite/package-lock.json b/plugins/tensorflow-lite/package-lock.json index be19531bc..7c289b92a 100644 --- a/plugins/tensorflow-lite/package-lock.json +++ b/plugins/tensorflow-lite/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/tensorflow-lite", - "version": "0.1.11", + "version": "0.1.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/tensorflow-lite", - "version": "0.1.11", + "version": "0.1.12", "devDependencies": { "@scrypted/sdk": "file:../../sdk" } diff --git a/plugins/tensorflow-lite/package.json b/plugins/tensorflow-lite/package.json index 9967134ab..0459ba6ee 100644 --- a/plugins/tensorflow-lite/package.json +++ b/plugins/tensorflow-lite/package.json @@ -44,5 +44,5 @@ "devDependencies": { "@scrypted/sdk": "file:../../sdk" }, - "version": "0.1.11" + "version": "0.1.12" } diff --git a/plugins/tensorflow/package-lock.json b/plugins/tensorflow/package-lock.json index be19531bc..7c289b92a 100644 --- a/plugins/tensorflow/package-lock.json +++ b/plugins/tensorflow/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/tensorflow-lite", - "version": "0.1.11", + "version": "0.1.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/tensorflow-lite", - "version": "0.1.11", + "version": "0.1.12", "devDependencies": { "@scrypted/sdk": "file:../../sdk" } diff --git a/plugins/tensorflow/package.json b/plugins/tensorflow/package.json index 84a753314..e17db5a6b 100644 --- a/plugins/tensorflow/package.json +++ b/plugins/tensorflow/package.json @@ -41,5 +41,5 @@ "devDependencies": { "@scrypted/sdk": "file:../../sdk" }, - "version": "0.1.11" + "version": "0.1.12" }