如何从 Jetson TX1 嵌入式摄像头接收图像?
How to receive images from Jetson TX1 embedded camera?
我用最新的 Jetpack 刷新了我的 Jetson TX1(Linux 对于 Tegra R23.2),以下命令完美运行:
gst-launch-1.0 nvcamerasrc fpsRange="30.0 30.0" ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! nvtee ! nvvidconv flip-method=2 ! 'video/x-raw(memory:NVMM), format=(string)I420' ! nvoverlaysink -e
我尝试使用以下 python 程序从网络摄像头接收图像:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
我收到以下错误:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /hdd/buildbot/slave_jetson_tx_2/35-O4T-L4T-Jetson-L/opencv/modules/imgproc/src/color.cpp, line 3739
Traceback (most recent call last):
File "webcam.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /hdd/buildbot/slave_jetson_tx_2/35-O4T-L4T-Jetson-L/opencv/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor
我知道问题是它无法从网络摄像头接收图像。我还更改了代码以仅显示从网络摄像头接收到的图像,但它给了我错误,这意味着它没有从相机获得图像。
我还尝试使用 C++ 编写以下代码:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation.
if(!cap.open(0))
return 0;
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
imshow("this is you, smile! :)", frame);
if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
并且使用
编译没有任何错误
g++ webcam.cpp -o webcam `pkg-config --cflags --libs opencv`
但是,当我 运行 程序时,我再次收到此错误:
$ ./webcam
Unable to stop the stream.: Device or resource busy
Unable to stop the stream.: Bad file descriptor
VIDIOC_STREAMON: Bad file descriptor
Unable to stop the stream.: Bad file descriptor
我错过了什么?在 运行 这个程序之前,我应该 运行 激活网络摄像头吗?
根据 nvidia 论坛,您需要正确设置 gstreamer 管道。目前 opencv 无法自动检测 nvcamera 的流。
我让它工作的唯一方法是使用 Opencv3 和这行代码来抓取视频:
cap = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
感谢这个帖子提供的所有信息,无论如何,我刚刚 python 从 TX1 相机模块获取一帧。
重要的是我们需要安装OpenCV 3.1.0,您可以按照官方的构建方法将python cv2.so lib替换为3.1.0版本。原来的 l4t OpenCV 是 2.4.
另外重要的是使用正确的nvcamerasrc;尝试
c
ap = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1 ! nvtee ! nvvidconv flip-method=2 ! video/x-raw(memory:NVMM), format=(string)I420 ! nvoverlaysink -e ! appsink")
在我的TX1上也能用,特分享一下。
我用最新的 Jetpack 刷新了我的 Jetson TX1(Linux 对于 Tegra R23.2),以下命令完美运行:
gst-launch-1.0 nvcamerasrc fpsRange="30.0 30.0" ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! nvtee ! nvvidconv flip-method=2 ! 'video/x-raw(memory:NVMM), format=(string)I420' ! nvoverlaysink -e
我尝试使用以下 python 程序从网络摄像头接收图像:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
我收到以下错误:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /hdd/buildbot/slave_jetson_tx_2/35-O4T-L4T-Jetson-L/opencv/modules/imgproc/src/color.cpp, line 3739
Traceback (most recent call last):
File "webcam.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /hdd/buildbot/slave_jetson_tx_2/35-O4T-L4T-Jetson-L/opencv/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor
我知道问题是它无法从网络摄像头接收图像。我还更改了代码以仅显示从网络摄像头接收到的图像,但它给了我错误,这意味着它没有从相机获得图像。
我还尝试使用 C++ 编写以下代码:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation.
if(!cap.open(0))
return 0;
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
imshow("this is you, smile! :)", frame);
if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
并且使用
编译没有任何错误g++ webcam.cpp -o webcam `pkg-config --cflags --libs opencv`
但是,当我 运行 程序时,我再次收到此错误:
$ ./webcam
Unable to stop the stream.: Device or resource busy
Unable to stop the stream.: Bad file descriptor
VIDIOC_STREAMON: Bad file descriptor
Unable to stop the stream.: Bad file descriptor
我错过了什么?在 运行 这个程序之前,我应该 运行 激活网络摄像头吗?
根据 nvidia 论坛,您需要正确设置 gstreamer 管道。目前 opencv 无法自动检测 nvcamera 的流。
我让它工作的唯一方法是使用 Opencv3 和这行代码来抓取视频:
cap = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
感谢这个帖子提供的所有信息,无论如何,我刚刚 python 从 TX1 相机模块获取一帧。
重要的是我们需要安装OpenCV 3.1.0,您可以按照官方的构建方法将python cv2.so lib替换为3.1.0版本。原来的 l4t OpenCV 是 2.4.
另外重要的是使用正确的nvcamerasrc;尝试 c
ap = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1 ! nvtee ! nvvidconv flip-method=2 ! video/x-raw(memory:NVMM), format=(string)I420 ! nvoverlaysink -e ! appsink")
在我的TX1上也能用,特分享一下。