mlapi request takes too long

Discussions related to the 1.36.x series of ZoneMinder
Post Reply
zm_123
Posts: 5
Joined: Thu Dec 17, 2020 2:13 pm

mlapi request takes too long

Post 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
tomparker
Posts: 2
Joined: Fri May 16, 2025 8:46 am

Re: mlapi request takes too long

Post 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
User avatar
iconnor
Posts: 3391
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: mlapi request takes too long

Post 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.
Post Reply