Skip to content

Commit

Permalink
openvino: do post processing inside callback rather than copy + threa…
Browse files Browse the repository at this point in the history
…d post process
  • Loading branch information
koush committed Dec 31, 2024
1 parent c19ec63 commit 98fe1d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions plugins/openvino/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/openvino/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@
"devDependencies": {
"@scrypted/sdk": "file:../../sdk"
},
"version": "0.1.152"
"version": "0.1.153"
}
63 changes: 30 additions & 33 deletions plugins/openvino/src/ov/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,38 @@ def __init__(self, nativeId: str | None = None, forked: bool = False):

self.infer_queue = ov.AsyncInferQueue(self.compiled_model)

def predict(output):
if not self.yolo:
objs = []
for values in output[0][0]:
valid, index, confidence, l, t, r, b = values
if valid == -1:
break

def torelative(value: float):
return value * self.model_dim

l = torelative(l)
t = torelative(t)
r = torelative(r)
b = torelative(b)

obj = Prediction(index - 1, confidence, Rectangle(l, t, r, b))
objs.append(obj)

return objs

if self.scrypted_yolov10:
return yolo.parse_yolov10(output[0])
if self.scrypted_yolo_nas:
return yolo.parse_yolo_nas([output[1], output[0]])
return yolo.parse_yolov9(output[0])

def callback(infer_request, future: asyncio.Future):
try:
output = infer_request.get_output_tensor(0).data
output = np.copy(output)
self.loop.call_soon_threadsafe(future.set_result, output)
objs = predict(output)
self.loop.call_soon_threadsafe(future.set_result, objs)
except Exception as e:
self.loop.call_soon_threadsafe(future.set_exception, e)

Expand Down Expand Up @@ -330,32 +357,6 @@ def get_input_format(self):
return super().get_input_format()

async def detect_once(self, input: Image.Image, settings: Any, src_size, cvss):
def predict(output):
if not self.yolo:
for values in output[0][0]:
valid, index, confidence, l, t, r, b = values
if valid == -1:
break

def torelative(value: float):
return value * self.model_dim

l = torelative(l)
t = torelative(t)
r = torelative(r)
b = torelative(b)

obj = Prediction(index - 1, confidence, Rectangle(l, t, r, b))
objs.append(obj)

return objs

if self.scrypted_yolov10:
return yolo.parse_yolov10(output[0])
if self.scrypted_yolo_nas:
return yolo.parse_yolo_nas([output[1], output[0]])
return yolo.parse_yolov9(output[0])

def prepare():
# the input_tensor can be created with the shared_memory=True parameter,
# but that seems to cause issues on some platforms.
Expand Down Expand Up @@ -388,11 +389,7 @@ def prepare():
)
f = asyncio.Future(loop=self.loop)
self.infer_queue.start_async(input_tensor, f)
output = await f
objs = await asyncio.get_event_loop().run_in_executor(
prepareExecutor, lambda: predict(output)
)

objs = await f
except:
traceback.print_exc()
raise
Expand Down

0 comments on commit 98fe1d4

Please sign in to comment.