Page 1 of 1

mlapi request takes too long

Posted: Sun Jun 22, 2025 1:26 am
by zm_123
Hi,
I recently installed mlapi locally (on the same machine as the event notification server). I hoped that it could speed up the detection process by loading the model once. However, I found the process took longer with mlapi than without mlapi. Please help me to understand why.

when I run zm_detect manually:

Code: Select all

sudo -u www-data /var/lib/zmeventnotification/bin/zm_detect.py --config /etc/zm/objectconfig.ini  --eventid 487487 --monitorid 22 --debug
with mlapi, the event request/response took about 10 sec (20:42:34.908467 to 20:42:45.288054) to complete and the TOTAL detection sequence was 13 sec as shown below:

Code: Select all

....
Jun 21 2025 20:42:34.908320 [DBG 2] Using URL 487487 for stream
Jun 21 2025 20:42:34.908347 [DBG 2] We will only process frames: ['snapshot', 'alarm']
Jun 21 2025 20:42:34.908366 [DBG 2] No need to start streams, we are picking images from https://x.x.x.x/zm/index.php?view=image&eid=487487
Jun 21 2025 20:42:34.908393 [DBG 2] Trying to convert snapshot to a real frame id
Jun 21 2025 20:42:34.908432 [DBG 3] No need to relogin as access token still has 86.25531593333334 minutes remaining
Jun 21 2025 20:42:34.908467 [DBG 3] make_request called with url=https://x.x.x.x/zm/api/events/487487.json payload={} type=get query={'token': 'xxxxxx'}
Jun 21 2025 20:42:45.288054 [DBG 2] At the point of analysis, Event:487487 snapshot frame id was:20,so using it
Jun 21 2025 20:42:45.288197 [DBG 3] Reading https://x.x.x.x/zm/index.php?view=image&eid=487487&fid=20
Jun 21 2025 20:42:45.288308 [DBG 3] No need to relogin as access token still has 86.08231828333334 minutes remaining
Jun 21 2025 20:42:45.288399 [DBG 3] make_request called with url=https://x.x.x.x/zm/index.php?view=image&eid=487487&fid=20 payload={} type=get query={'token': 'xxxxxx'}
Jun 21 2025 20:42:45.319819 [DBG 1] perf: Starting for frame:20
....
Jun 21 2025 20:42:48.482418 [DBG 1] perf: TOTAL detection sequence (with image loads) took: 13574.27 ms  to process 487487
without mlapi, there was virtually no delay for the request/response and the TOTAL detection sequence was 3 sec:

Code: Select all

....
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG2 Media.py:99 [Using URL 487487 for stream]
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG2 Media.py:114 [We will only process frames: ['snapshot']]
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG2 Media.py:137 [No need to start streams, we are picking images from https://x.x.x.x/zm/index.php?view=image&eid=487487]
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG3 Media.py:271 [Reading https://x.x.x.x/zm/index.php?view=image&eid=487487&fid=snapshot]
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG3 api.py:144 [No need to relogin as access token still has 119.99980506666667 minutes remaining]
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG3 api.py:272 [make_request called with url=https://x.x.x.x/zm/index.php?view=image&eid=487487&fid=snapshot payload={} type=get query={'token': xxxxxx'}]
06/21/25 20:58:02 zmesdetect_m22[1453443] DBG1 detect_sequence.py:654 [perf: Starting for frame:snapshot]
....
06/21/25 20:58:05 zmesdetect_m22[1453443] DBG1 detect_sequence.py:827 [perf: TOTAL detection sequence (with image loads) took: 2827.66 ms  to process 487487]
Any idea why?
Thanks
Edit: to add that I'm running zm v1.36.35 with Ubuntu 22.04.5 LTS

Re: mlapi request takes too long

Posted: Wed Jul 02, 2025 4:30 am
by tomparker
Hello,
If you want to keep mlapi for modularity (good idea for multi-node setups or future scaling), you could:

Increase mlapi worker processes
(if using gunicorn: --workers and --threads)

Switch mlapi to asynchronous server like uvicorn or gunicorn with async workers

Re: mlapi request takes too long

Posted: Wed Jul 02, 2025 10:18 pm
by iconnor
I can't imagine what would take that long, but at the same time... not sure who is actually using mlapi and the python apis keep changing, etc.

I will fire up my ai box and take a look.