# Capture from local device vs tcpstream

**URL:** <https://forum.opencv.org/t/capture-from-local-device-vs-tcpstream/9358>\
**Category:** Python\
**Tags:** videoio\
**Created:** [June 29, 2022, 3:13pm UTC](https://forum.opencv.org/t/capture-from-local-device-vs-tcpstream/9358 "2022-06-29T15:13:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_Miller](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alan_miller/32/5560_2.png) [@Alan\_Miller](https://forum.opencv.org/u/Alan_Miller)\
**Post date:** [June 29, 2022, 3:13pm UTC](https://forum.opencv.org/t/capture-from-local-device-vs-tcpstream/9358/1 "2022-06-29T15:13:02Z")

</div>

I’m just getting started with opencv.

Can I capture a video stream directly from the camera with:

`cap = cv.VideoCapture(0, cv.CAP_V4L2)`

If I start a stream using libcamera-vid first I can capture the stream.  
Terminal 1

`pi@dev:~/ $ libcamera-vid -t 0 --nopreview --inline --listen -o tcp://0.0.0.0:9994`

Termina2

```auto
pi@dev:~/ $ ./test.py
Capture opened
FFMPEG
1900.0
0.0
Got frame
Got frame
Got frame

```

However, when I use cap = cv.VideoCapture(0, cv.CAP\_V4L2) I get

```auto
pi@dev:~/ $ ./test.py
Capture opened
V4L2
200.0
-1.0
Can't receive frame (stream end?). Exiting ...

```

Here’s my test.py

```auto
#!/usr/bin/python3

import os
import numpy as np
import cv2 as cv

RTSP_URL = 'tcp://172.16.1.3:9994'
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
# start stream first!
cap = cv.VideoCapture(RTSP_URL, cv.CAP_FFMPEG)
#cap = cv.VideoCapture(0, cv.CAP_V4L2)

if not cap.isOpened():
    print("Cannot open camera")
    exit()
else:
    print("Capture opened")
    print(cap.getBackendName())
    print(cap.get(cv.CAP_PROP_BACKEND))
    print(cap.get(cv.CAP_PROP_VIDEO_STREAM))

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    print("Got frame")

cap.release()

```
