diff --git a/plugins/coreml/package-lock.json b/plugins/coreml/package-lock.json index 7845796d9..42746943f 100644 --- a/plugins/coreml/package-lock.json +++ b/plugins/coreml/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/coreml", - "version": "0.1.87", + "version": "0.1.89", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/coreml", - "version": "0.1.87", + "version": "0.1.89", "devDependencies": { "@scrypted/sdk": "file:../../sdk" } diff --git a/plugins/coreml/package.json b/plugins/coreml/package.json index cbe19d7db..f8e028fb9 100644 --- a/plugins/coreml/package.json +++ b/plugins/coreml/package.json @@ -50,5 +50,5 @@ "devDependencies": { "@scrypted/sdk": "file:../../sdk" }, - "version": "0.1.87" + "version": "0.1.89" } diff --git a/plugins/coreml/src/coreml/__init__.py b/plugins/coreml/src/coreml/__init__.py index 163bd337a..63d547f65 100644 --- a/plugins/coreml/src/coreml/__init__.py +++ b/plugins/coreml/src/coreml/__init__.py @@ -83,25 +83,13 @@ class CoreMLPlugin( if model != "Default": self.storage.setItem("model", "Default") model = "scrypted_yolov9c_relu" - model_version = "v8" - mlmodel = "model" self.modelName = model print(f"model: {model}") - files = [ - f"{model}/{model}.mlpackage/Data/com.apple.CoreML/weights/weight.bin", - f"{model}/{model}.mlpackage/Data/com.apple.CoreML/{mlmodel}.mlmodel", - f"{model}/{model}.mlpackage/Manifest.json", - ] - - for f in files: - p = self.downloadFile( - f"https://huggingface.co/scrypted/plugin-models/resolve/main/coreml/{f}", - f"{model_version}/{f}", - ) - modelFile = os.path.dirname(p) - + model_path = self.downloadHuggingFaceModelLocalFallback(model) + modelFile = os.path.join(model_path, f"{model}.mlpackage") + print(model_path, modelFile) self.model = ct.models.MLModel(modelFile) self.modelspec = self.model.get_spec() diff --git a/plugins/coreml/src/coreml/face_recognition.py b/plugins/coreml/src/coreml/face_recognition.py index e1c4dae76..bee1b5f43 100644 --- a/plugins/coreml/src/coreml/face_recognition.py +++ b/plugins/coreml/src/coreml/face_recognition.py @@ -6,6 +6,7 @@ import os import asyncio import coremltools as ct import numpy as np + # import Quartz # from Foundation import NSData, NSMakeSize @@ -25,6 +26,7 @@ def cosine_similarity(vector_a, vector_b): similarity = dot_product / (norm_a * norm_b) return similarity + class CoreMLFaceRecognition(FaceRecognizeDetection): def __init__(self, plugin, nativeId: str): super().__init__(plugin, nativeId) @@ -32,26 +34,12 @@ class CoreMLFaceRecognition(FaceRecognizeDetection): self.recogExecutor = concurrent.futures.ThreadPoolExecutor(1, "recog-face") def downloadModel(self, model: str): - model_version = "v7" - mlmodel = "model" - - files = [ - f"{model}/{model}.mlpackage/Data/com.apple.CoreML/weights/weight.bin", - f"{model}/{model}.mlpackage/Data/com.apple.CoreML/{mlmodel}.mlmodel", - f"{model}/{model}.mlpackage/Manifest.json", - ] - - for f in files: - p = self.downloadFile( - f"https://huggingface.co/scrypted/plugin-models/resolve/main/coreml/{f}", - f"{model_version}/{f}", - ) - modelFile = os.path.dirname(p) - + model_path = self.downloadHuggingFaceModelLocalFallback(model) + modelFile = os.path.join(model_path, f"{model}.mlpackage") model = ct.models.MLModel(modelFile) inputName = model.get_spec().description.input[0].name return model, inputName - + async def predictDetectModel(self, input: Image.Image): def predict(): model, inputName = self.detectModel @@ -70,11 +58,12 @@ class CoreMLFaceRecognition(FaceRecognizeDetection): out_dict = model.predict({inputName: input}) results = list(out_dict.values())[0][0] return results + results = await asyncio.get_event_loop().run_in_executor( self.recogExecutor, lambda: predict() ) return results - + # def predictVision(self, input: Image.Image) -> asyncio.Future[list[Prediction]]: # buffer = input.tobytes() # myData = NSData.alloc().initWithBytes_length_(buffer, len(buffer)) diff --git a/plugins/coreml/src/coreml/text_recognition.py b/plugins/coreml/src/coreml/text_recognition.py index 52f47ade2..1ca7a3c4b 100644 --- a/plugins/coreml/src/coreml/text_recognition.py +++ b/plugins/coreml/src/coreml/text_recognition.py @@ -20,22 +20,8 @@ class CoreMLTextRecognition(TextRecognition): self.recogExecutor = concurrent.futures.ThreadPoolExecutor(1, "recog-text") def downloadModel(self, model: str): - model_version = "v8" - mlmodel = "model" - - files = [ - f"{model}/{model}.mlpackage/Data/com.apple.CoreML/weights/weight.bin", - f"{model}/{model}.mlpackage/Data/com.apple.CoreML/{mlmodel}.mlmodel", - f"{model}/{model}.mlpackage/Manifest.json", - ] - - for f in files: - p = self.downloadFile( - f"https://github.com/koush/coreml-models/raw/main/{f}", - f"{model_version}/{f}", - ) - modelFile = os.path.dirname(p) - + model_path = self.downloadHuggingFaceModelLocalFallback(model) + modelFile = os.path.join(model_path, f"{model}.mlpackage") model = ct.models.MLModel(modelFile) inputName = model.get_spec().description.input[0].name return model, inputName diff --git a/plugins/onnx/package-lock.json b/plugins/onnx/package-lock.json index 685994d49..c2de229e2 100644 --- a/plugins/onnx/package-lock.json +++ b/plugins/onnx/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/onnx", - "version": "0.1.129", + "version": "0.1.130", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/onnx", - "version": "0.1.129", + "version": "0.1.130", "devDependencies": { "@scrypted/sdk": "file:../../sdk" } diff --git a/plugins/onnx/package.json b/plugins/onnx/package.json index fb9deb85b..5a59ae6fa 100644 --- a/plugins/onnx/package.json +++ b/plugins/onnx/package.json @@ -50,5 +50,5 @@ "devDependencies": { "@scrypted/sdk": "file:../../sdk" }, - "version": "0.1.129" + "version": "0.1.130" } diff --git a/plugins/onnx/src/ort/__init__.py b/plugins/onnx/src/ort/__init__.py index f9da02aa5..fad477279 100644 --- a/plugins/onnx/src/ort/__init__.py +++ b/plugins/onnx/src/ort/__init__.py @@ -4,6 +4,7 @@ import ast import asyncio import concurrent.futures import json +import os import platform import sys import threading @@ -72,11 +73,8 @@ class ONNXPlugin( print(f"model {model}") - model_version = "v3" - onnxfile = self.downloadFile( - f"https://huggingface.co/scrypted/plugin-models/resolve/main/onnx/{model}/{model}.onnx", - f"{model_version}/{model}/{model}.onnx", - ) + model_path = self.downloadHuggingFaceModelLocalFallback(model) + onnxfile = os.path.join(model_path, f"{model}.onnx") print(onnxfile) diff --git a/plugins/onnx/src/ort/face_recognition.py b/plugins/onnx/src/ort/face_recognition.py index 15f8eb005..8d13304de 100644 --- a/plugins/onnx/src/ort/face_recognition.py +++ b/plugins/onnx/src/ort/face_recognition.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import concurrent.futures +import os import platform import sys import threading @@ -15,11 +16,8 @@ from predict.face_recognize import FaceRecognizeDetection class ONNXFaceRecognition(FaceRecognizeDetection): def downloadModel(self, model: str): - model_version = "v1" - onnxfile = self.downloadFile( - f"https://huggingface.co/scrypted/plugin-models/resolve/main/onnx/{model}/{model}.onnx", - f"{model_version}/{model}/{model}.onnx", - ) + model_path = self.downloadHuggingFaceModelLocalFallback(model) + onnxfile = os.path.join(model_path, f"{model}.onnx") print(onnxfile) compiled_models_array = [] diff --git a/plugins/onnx/src/ort/text_recognition.py b/plugins/onnx/src/ort/text_recognition.py index 7b4cb5cf9..c88ccec2f 100644 --- a/plugins/onnx/src/ort/text_recognition.py +++ b/plugins/onnx/src/ort/text_recognition.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import concurrent.futures +import os import platform import sys import threading @@ -15,12 +16,8 @@ from predict.text_recognize import TextRecognition class ONNXTextRecognition(TextRecognition): def downloadModel(self, model: str): - onnxmodel = model - model_version = "v4" - onnxfile = self.downloadFile( - f"https://github.com/koush/onnx-models/raw/main/{model}/{onnxmodel}.onnx", - f"{model_version}/{model}/{onnxmodel}.onnx", - ) + model_path = self.downloadHuggingFaceModelLocalFallback(model) + onnxfile = os.path.join(model_path, f"{model}.onnx") print(onnxfile) compiled_models_array = [] diff --git a/plugins/openvino/package-lock.json b/plugins/openvino/package-lock.json index 3def14846..fc728fd97 100644 --- a/plugins/openvino/package-lock.json +++ b/plugins/openvino/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/openvino", - "version": "0.1.193", + "version": "0.1.194", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/openvino", - "version": "0.1.193", + "version": "0.1.194", "devDependencies": { "@scrypted/sdk": "file:../../sdk" } diff --git a/plugins/openvino/package.json b/plugins/openvino/package.json index 107e6f682..76c47f587 100644 --- a/plugins/openvino/package.json +++ b/plugins/openvino/package.json @@ -50,5 +50,5 @@ "devDependencies": { "@scrypted/sdk": "file:../../sdk" }, - "version": "0.1.193" + "version": "0.1.194" } diff --git a/plugins/openvino/src/predict/__init__.py b/plugins/openvino/src/predict/__init__.py index 0a7a973cc..d51fbbcf1 100644 --- a/plugins/openvino/src/predict/__init__.py +++ b/plugins/openvino/src/predict/__init__.py @@ -21,8 +21,8 @@ from detect import DetectPlugin from predict.rectangle import Rectangle cache_dir = os.path.join(os.environ["SCRYPTED_PLUGIN_VOLUME"], "files", "hf") -os.makedirs(cache_dir, exist_ok=True) -os.environ['HF_HUB_CACHE'] = cache_dir +# os.makedirs(cache_dir, exist_ok=True) +# os.environ['HF_HUB_CACHE'] = cache_dir original_getaddrinfo = socket.getaddrinfo @@ -91,10 +91,12 @@ class PredictPlugin(DetectPlugin, scrypted_sdk.ClusterForkInterface, scrypted_sd def downloadHuggingFaceModel(self, model: str, local_files_only: bool = False) -> str: from huggingface_hub import snapshot_download plugin_suffix = self.pluginId.split('/')[1] + local_dir = os.path.join(cache_dir, plugin_suffix, model) local_path = snapshot_download( repo_id="scrypted/plugin-models", allow_patterns=f"{plugin_suffix}/{model}/*", local_files_only=local_files_only, + local_dir=local_dir, ) local_path = os.path.join(local_path, plugin_suffix, model) return local_path