# Object detection on multipole streams

**URL:** <https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107>\
**Category:** Jetson Xavier NX\
**Created:** [September 1, 2023, 4:46am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107 "2023-09-01T04:46:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 1, 2023, 4:46am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/1 "2023-09-01T04:46:43Z")

</div>

Hi,

I’m following the hello ai world tutorials.

I want to perform object detection on multipole video streams.

Initially i would like to do this with webcams (for testing), but my final goal is to be able to do it with rtsp streams.

How can i achieve this?  
What should be the approach?

Thanks you very much.

---

<div class="post-metadata">

**Author:** ![dusty\_nv](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dusty_nv/32/14043_2.png) [@dusty\_nv](https://forums.developer.nvidia.com/u/dusty_nv)\
**Post date:** [September 1, 2023, 6:43pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/4 "2023-09-01T18:43:51Z")

</div>

Hi @oraday, detectNet from jetson-inference is stateless (except for the tracker), so you can use one detectNet across multiple video streams. You can create multiple videoSource objects, one for each camera, then process each of them with detectNet. Some psuedocode based on [detectNet.py](https://github.com/dusty-nv/jetson-inference/blob/master/python/examples/detectnet.py):

```bash
from jetson_inference import detectNet
from jetson_utils import videoSource

cameras = [
    videoSource("/dev/video0", options={'width': 1280, 'height': 720}),
    videoSource("/dev/video0", options={'width': 1280, 'height': 720}),
]

net = detectNet("ssd-mobilenet-v2")

while True:
    for i in range(len(cameras)):
        img = cameras[i].Capture(timeout=0) # use timeout=0 so it returns immediately if no image ready from that camera

        if img is None: # continue to the next camera if no image ready
            continue

        detections = net.Detect(img)

        print(f"Camera {i} detections:', detections)

```

That said, DeepStream will give you optimal multi-stream performance, batching, better tracking, ect.

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 2, 2023, 7:22am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/5 "2023-09-02T07:22:33Z")

</div>

Thank you, @dusty_nv.

Are you suggesting that it’s worth investing time in learning the DeepStream SDK if I aim to develop real production applications?

Does working with DeepStream ultimately lead to the creation of more efficient and faster applications compared to working with ‘jetson-inference’?

I’ve started going through the DeepStream SDK information on this site, but I haven’t come across any basic tutorials for beginners. I’m struggling to understand how I should approach this. What are your suggestions?

Additionally, I’ve attempted to develop code directly on my Jetson Xavier NX, but it’s rather slow when searching for information on the internet. Should I do my development on a more powerful computer with DeepStream or jetson-inference and then transfer it to my Xavier NX? What would be the best practical approach for development?

I know these are a bunch of questions, but I would greatly appreciate your insights.

Thanks!

---

<div class="post-metadata">

**Author:** ![dusty\_nv](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dusty_nv/32/14043_2.png) [@dusty\_nv](https://forums.developer.nvidia.com/u/dusty_nv)\
**Post date:** [September 2, 2023, 2:15pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/6 "2023-09-02T14:15:39Z")

</div>

> [@oraday](#):
>
> Are you suggesting that it’s worth investing time in learning the DeepStream SDK if I aim to develop real production applications?

I think it probably depends on how many streams and models you want to run, and how optimized you want the application to be. DeepStream can be a lot faster, is more closely integrated with TAO Toolkit for INT8 models (although jetson-inference does support TAO detection models), and DeepStream supports DLA.

> [@](#):
>
> Does working with DeepStream ultimately lead to the creation of more efficient and faster applications compared to working with ‘jetson-inference’?

For more complex multi-stream applications, certainly yes. Also it has a lot better tracking (if you are using tracking). jetson-inference predates DeepStream and started as a tutorial to make it easy to learn about DNNs (hence it’s aim is to provide the easiest-possible C++/Python APIs for doing 1x stream in realtime on Jetson Nano)

> [@oraday](#):
>
> I’ve started going through the DeepStream SDK information on this site, but I haven’t come across any basic tutorials for beginners. I’m struggling to understand how I should approach this. What are your suggestions?

I’d suggest the DeepStream Python examples, and looking at the notebooks in this DLI course:

- [GitHub - NVIDIA-AI-IOT/deepstream\_python\_apps: DeepStream SDK Python bindings and sample applications · GitHub](https://github.com/NVIDIA-AI-IOT/deepstream_python_apps)
- [https://courses.nvidia.com/courses/course-v1:DLI+S-IV-02+V2/](https://courses.nvidia.com/courses/course-v1:DLI+S-IV-02+V2/)

If you only want to do a couple of streams, you can always try my approach above with jetson-inference as it might be quick for you to code. However ultimately if you want it to scale bigger, you might want to invest time into learning DeepStream.

> [@oraday](#):
>
> Additionally, I’ve attempted to develop code directly on my Jetson Xavier NX, but it’s rather slow when searching for information on the internet. Should I do my development on a more powerful computer with DeepStream or jetson-inference and then transfer it to my Xavier NX? What would be the best practical approach for development?

Personally, I always SSH into my Jetson boards. I use MobaXTerm (SSH client) on Windows, which has a nice remote file browser. Also, I know people use Visual Studio Code remotely over SSH. So you are still developing/compiling “on” the Jetson, it’s just being done “from” your PC.

The exception is when you are getting started with camera stuff, it can be nice to still have a display attached to your Jetson so you can easily view the video streams (until you get into more remote streaming stuff like RTP/RTSP/WebRTC)

Hope that helps!

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 2, 2023, 2:27pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/7 "2023-09-02T14:27:39Z")

</div>

Thank you very much dusty.

What is your opinion on the graph composer compared to direct python coding with deep-stream?

Would it be easier for beginners to start with the graph composer to play with the basic stuff?

Would eventually I will find my self using deep-stream with python (and not with the graph composer)?

---

<div class="post-metadata">

**Author:** ![dusty\_nv](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dusty_nv/32/14043_2.png) [@dusty\_nv](https://forums.developer.nvidia.com/u/dusty_nv)\
**Post date:** [September 2, 2023, 5:40pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/8 "2023-09-02T17:40:22Z")

</div>

I haven’t personally tried the Graph Composer but it seems like an awesome tool, and if you were starting new probably a good idea to check it out!

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 3, 2023, 1:52pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/9 "2023-09-03T13:52:19Z")

</div>

Hi again Dusty,

I tried the following code.

I’m getting detection boxes overlays on both display windows, regardless what was the source of the detection.

i.e. I want to get detection from the first videoSource displayed only on the relevant videoOutput (and not both outputs).

I tried few things that didn’t work. So I decided to ask you how to approach this.

Thanks!

```auto
from jetson_inference import detectNet
from jetson_utils import videoSource, videoOutput
import time

start = time.time()
fpsFilt=0

net = detectNet("ssd-mobilenet-v2", threshold=0.35)

net.SetTrackingEnabled(True)
net.SetTrackingParams(minFrames=3, dropFrames=15, overlapThreshold=0.5)

display = [
    videoOutput("display://0"),
    videoOutput("display://0"),
]

cameras = [
    videoSource("/dev/video0"),
    videoSource("/dev/video2"),
]

isStreaming = True

while isStreaming:

    for i in range(len(cameras)):

        if not display[i].IsStreaming():
            isStreaming = False

        img = cameras[i].Capture(timeout=0) # use timeout=0 so it returns immediately if no image ready from that camera
        
        if img is None: # continue to the next camera if no image ready
            continue

        detections = net.Detect(img, overlay='box,labels,track,conf')

        for detection in detections:
            class_name = net.GetClassDesc(detection.ClassID)
            print(f"Detected '{class_name}'")
            if detection.TrackStatus >= 0: # actively tracking
                print(f"object {detection.TrackID} at ({detection.Left}, {detection.Top}) has been tracked for {detection.TrackFrames} frames")
            else: # if tracking was lost, this object will be dropped the next frame
                print(f"object {detection.TrackID} has lost tracking")  

        print(detections)

        display[i].Render(img)
    
    end = time.time()
    dt = end - start
    fps = 1 / dt
    fpsFilt = .9*fpsFilt + .1*fps

    print("Global iteration time {:.0f} FPS".format(fpsFilt))

    start = time.time()

```

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 5, 2023, 10:52am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/10 "2023-09-05T10:52:20Z")

</div>

Hi Dusty,

Any comment on my previous post?

---

<div class="post-metadata">

**Author:** ![dusty\_nv](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dusty_nv/32/14043_2.png) [@dusty\_nv](https://forums.developer.nvidia.com/u/dusty_nv)\
**Post date:** [September 5, 2023, 2:16pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/11 "2023-09-05T14:16:09Z")

</div>

@oraday if you are using tracking, use two separate detectNet objects (one for each camera)

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 7, 2023, 1:04pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/12 "2023-09-07T13:04:16Z")

</div>

I tried the attached code.

It works most of the time, but once in a while i get an error:

“[gstreamer] gstCamera::Capture() – an error occurred retrieving the next image buffer  
Traceback (most recent call last):  
File “/home/nx/Desktop/pyPro/detection-tracking-multiple-streams.py”, line 38, in   
img = cameras[i].Capture()  
Exception: jetson.utils – videoSource failed to capture image”.

Any idea why, and what can be done to avoid or override this?

Thanks!

```auto
from jetson_inference import detectNet
from jetson_utils import videoSource, videoOutput
import time

start = time.time()
fpsFilt=0

net = [detectNet("ssd-mobilenet-v2", threshold=0.35),
       detectNet("ssd-mobilenet-v2", threshold=0.35)] 

net[0].SetTrackingEnabled(True)
net[0].SetTrackingParams(minFrames=3, dropFrames=15, overlapThreshold=0.5)

net[1].SetTrackingEnabled(True)
net[1].SetTrackingParams(minFrames=3, dropFrames=15, overlapThreshold=0.5)

display = [
    videoOutput("display://0"),
    videoOutput("display://0"),
]

cameras = [
    videoSource("/dev/video0"),
    videoSource("/dev/video2"),
]

isStreaming = True

while isStreaming:

    for i in range(len(cameras)):

        if not display[i].IsStreaming():
            isStreaming = False

        # img = cameras[i].Capture(timeout=0) # use timeout=0 so it returns immediately if no image ready from that camera
        img = cameras[i].Capture()
        
        if img is None: # continue to the next camera if no image ready
            continue

        detections = net[i].Detect(img, overlay='box,labels,track,conf')

        for detection in detections:
            class_name = net[i].GetClassDesc(detection.ClassID)
            print(f"Detected '{class_name}'")
            if detection.TrackStatus >= 0: # actively tracking
                print(f"object {detection.TrackID} at ({detection.Left}, {detection.Top}) has been tracked for {detection.TrackFrames} frames")
            else: # if tracking was lost, this object will be dropped the next frame
                print(f"object {detection.TrackID} has lost tracking")  

        print(detections)

        display[i].Render(img)
    
    end = time.time()
    dt = end - start
    fps = 1 / dt
    fpsFilt = .9*fpsFilt + .1*fps

    print("Global iteration time {:.0f} FPS".format(fpsFilt))

    start = time.time()

```

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 10, 2023, 12:30pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/13 "2023-09-10T12:30:59Z")

</div>

Any idea what might be the problem and how to bypass it such it won’t break the code?

---

<div class="post-metadata">

**Author:** ![dusty\_nv](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dusty_nv/32/14043_2.png) [@dusty\_nv](https://forums.developer.nvidia.com/u/dusty_nv)\
**Post date:** [September 11, 2023, 1:32pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/14 "2023-09-11T13:32:30Z")

</div>

> [@oraday](#):
>
> “[gstreamer] gstCamera::Capture() – an error occurred retrieving the next image buffer  
> Traceback (most recent call last):  
> File “/home/nx/Desktop/pyPro/detection-tracking-multiple-streams.py”, line 38, in  
> img = cameras[i].Capture()

@oraday not sure - is there other debug output from the log? Could be a camera issue or connection dropping a frame. Does it continue capturing frames after that? If so, I would just let it continue running.

---

<div class="post-metadata">

**Author:** ![oraday](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@oraday](https://forums.developer.nvidia.com/u/oraday)\
**Post date:** [September 13, 2023, 5:15am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/15 "2023-09-13T05:15:09Z")

</div>

There is no other log information.  
And it’s not capturing frames. It breaks the code and stops.  
I would be happy if it would not break the code and continue running after frames drop.  
Any way to achieve this?  
Thanks again!

---

<div class="post-metadata">

**Author:** ![fantastic\_fan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fantastic\_fan](https://forums.developer.nvidia.com/u/fantastic_fan)\
**Post date:** [September 20, 2023, 8:38am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/16 "2023-09-20T08:38:28Z")

</div>

Hi. @dusty_nv  
I just got the same problem, it could run well for a while(maybe 2 min),but shutdown with the error “[gstreamer] gstCamera::Capture() – an error occurred retrieving the next image buffer  
Traceback”. I have tried to move the code “/dev/video0” out of the .py file and add after the command like “python3 xxx.py /dev/video0”. Amazingly it remains 4 minutes more before the error occured. I can’t figure out the reason and I wonder if there is a way that can automatically reboot the file when the error appears so that I can keep the program remaining.  
Thanks

---

<div class="post-metadata">

**Author:** ![dusty\_nv](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dusty_nv/32/14043_2.png) [@dusty\_nv](https://forums.developer.nvidia.com/u/dusty_nv)\
**Post date:** [September 20, 2023, 12:49pm UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/17 "2023-09-20T12:49:25Z")

</div>

> [@fantastic\_fan](#):
>
> I can’t figure out the reason and I wonder if there is a way that can automatically reboot the file when the error appears so that I can keep the program remaining.

You can catch the exception from videoSource.Capture() that gets thrown when an error occurs. If it’s a normal timeout, `None` will be returned from Capture()

You might want to try temporarily disabling the DNN stuff to see if that’s related, or if it’s a camera issue. Is it always the same camera dropping connection? Does that camera drop out even when it’s the only camera being captured?

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [October 10, 2023, 6:29am UTC](https://forums.developer.nvidia.com/t/object-detection-on-multipole-streams/265107/19 "2023-10-10T06:29:35Z")

</div>

Closed on October 10, 2023, 6:29am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
