jetson NanoCamera(USB摄像头连接)上篇文章简单的分析了,使用USB摄像头捕获视频流的内部过程。今天这篇文章算是最后的一篇使用文,会从现在拥有的功能,安装,使用等方面描述一下.
- OpenCV已经准备好了。可以使用OpenCV imshow直接调用图像文件
- 获得图像文件是一个NumPy RGB数组。
- 支持不同的相机翻转模式(逆时针,旋转180度,顺时针-90度,水平翻转,垂直翻转)
- 可以与多台摄像机一起使用。
- 支持帧速率执行。*仅适用于USB,RTSP和IP / MJPEG相机。
- 帧速率强制使用GStreamer视频速率插件确保摄像机以给定的帧速率工作
- 它基于加速的GStreamer插件
- 应与其他Jetson板卡(如Jetson TX1,TX2等)配合使用(未测试)
- 同时支持硬件和CPU加速。
- 轻松读取图像作为
numpy
数组image = camera.read()
- 支持线程读取-适用于所有类型的相机。要启用快速线程读取,您将启用implement_fps:
enforce_fps = True
- 使用
isReady()
功能初始化后,请检查摄像机的状态。True
如果准备就绪,False
否则返回。 - 提供调试支持。添加了错误代码和可选的异常处理。如果出现问题,可以重新启动摄像头;如果摄像头出现故障,则可以发送使用者通知。
- 使用
device_id
参数支持多个CSI摄像机。
以上内容为支持的功能,而且一部分功能需要opencv的帮助:
代码语言:javascript复制pip3 install opencv-python
安装一下
代码语言:javascript复制pip3 install nanocamera
pip安装
代码语言:javascript复制git clone https://github.com/thehapyone/NanoCamera
cd NanoCamera
sudo python3 setup.py install
源码安装。
接下来讲解使用过程:
CSI摄像头的使用
代码语言:javascript复制import nanocamera as nano
# Create the Camera instance for 640 by 480
camera = nano.Camera()
需要设置为camera_type = 0.默认是FPS 30,大小为640x480,而且不旋转 flip = 0.
自定义高度,宽度
代码语言:javascript复制import nanocamera as nano
# Create the Camera instance for No rotation (flip=0) with size of 1280 by 800
camera = nano.Camera(flip=0, width=1280, height=800, fps=30)
多个CSI摄像头的使用:
代码语言:javascript复制import nanocamera as nano
# Create the Camera instance for No rotation (flip=0) with size of 1280 by 800
# Connect to CSI camera with ID 0 (Default)
camera_1 = nano.Camera(device_id=0, flip=0, width=1280, height=800, fps=30)
# Connect to another CSI camera on the board with ID 1
camera_2 = nano.Camera(device_id=1, flip=0, width=1280, height=800, fps=30)
用camera_id来指定。
使用USB摄像头:
代码语言:javascript复制import nanocamera as nano
# Create the Camera instance for No rotation (flip=0) with size of 640 by 480
camera = nano.Camera(camera_type=1, device_id=1, width=640, height=480, fps=30)
相机种类为1,设备的id也得填入。
代码语言:javascript复制ls /dev/video*
在linux系统下执行这个命令看自己的摄像头名字。
RTSP的摄像头使用下面的代码来设置:
代码语言:javascript复制# a location for the rtsp stream. Stream location without "rtsp://"
rtsp_location = "192.168.1.26:8554/stream"
# Create the Camera instance
camera = nano.Camera(camera_type=2, source=rtsp_location, width=640, height=480, fps=30)
种类为2,而且要设置source
代码语言:javascript复制rtsp_location = "192.168.1.26:8554/stream"
还有一种摄像头是IP、MJPMG的摄像头或者照片:
代码语言:javascript复制# a location for the camera stream. Stream location without "http://"
camera_stream = "192.168.1.26:80/stream"
# Create the Camera instance
camera = nano.Camera(camera_type=3, source=camera_stream, width=640, height=480, fps=30)
记得把相机的种类设置为3,流媒体的source也要打开
代码语言:javascript复制camera_stream = "192.168.1.26:80/stream"
特别要需要知道自己的相机的位置所在。
这个代码可以让你的相机强制一个速率来获得帧
代码语言:javascript复制import nanocamera as nano
# enforce the capture frame rate with the enforce_fps=True
camera = nano.Camera(camera_type=1, device_id=1, width=640, height=480, fps=30, enforce_fps=True)
读取视频流的代码:
代码语言:javascript复制frame = camera.read()
获得的格式是numpy.ndarray(),格式为BGR,这个转换写过很多了,自己感兴趣的可以看看
在这么多传输的流程里面,一定要一直确保相机的正常使用,所以有保证相机运行正常的代码
代码语言:javascript复制status = camera.isReady()
接下来会给出SCI相机的连接代码:
代码语言:javascript复制import cv2
#from nanocamera.NanoCam import Camera
import nanocamera as nano
if __name__ == '__main__':
# Create the Camera instance
camera = nano.Camera(flip=0, width=640, height=480, fps=30)
print('CSI Camera ready? - ', camera.isReady())
while camera.isReady():
try:
# read the camera image
frame = camera.read()
# display the frame
cv2.imshow("Video Frame", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
except KeyboardInterrupt:
break
# close the camera instance
camera.release()
# remove camera object
del camera
IP/MPJEG的代码:
代码语言:javascript复制import cv2
# from nanocamera.NanoCam import Camera
import nanocamera as nano
if __name__ == '__main__':
# requires the Camera streaming url. Something like this: http://localhost:80/stream
# For IP/MJPEG camera, the camera_type=3.
# This works with only camera steaming MJPEG format and not H.264 codec for now
# a location for the camera stream
camera_stream = "192.168.1.26:80"
# Create the Camera instance
camera = nano.Camera(camera_type=3, source=camera_stream, width=640, height=480, fps=30)
print('MJPEG/IP Camera is now ready')
while camera.isReady():
try:
# read the camera image
frame = camera.read()
# display the frame
cv2.imshow("Video Frame", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
except KeyboardInterrupt:
break
# close the camera instance
camera.release()
# remove camera object
del camera
其实这些代码都没哟什么好说的,都是建立一个对象,按规则初始化,然后就是CV2接管来进行渲染输出等。
这个库最好的地方在于它的可调试性,不是说它的调试功能多强大,而是夸它的实现。
如果你在代码中使用了:
代码语言:javascript复制 camere.hasError()
这个代码,会在所有的有错误的地方打印错误的代码的列表和布尔值
代码语言:javascript复制# status holds a list.
status = camera.hasError()
print (status)
>> ([0, 3], True)
print ("错误代码列表 : ", status[0])
>> Error codes list : [0, 3]
print ("Error State : ", status[1])
>> Error State: True
错误的代码是:
代码语言:javascript复制'''
-1 = Unknown error
0 = No error
1 = Error: Could not initialize camera.
2 = Thread Error: Could not read image from camera
3 = Error: Could not read image from camera
4 = Error: Could not release camera
'''
在写一个例子:
代码语言:javascript复制error_status = camera.hasError()
if error_status[1] == False: # means no error detected so far
# read the camera image
frame = camera.read()
# print the current error codes
print (error_status[0])
# display the frame
cv2.imshow("Video Frame", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
# an error has occured.
print ("An error with the camera. Error code : ", error_status[0])
debug开关先使能,一般来说其实是调试多线程的应用。
接下来的例子是一个开发时的样板例子:
代码语言:javascript复制if __name__ == '__main__':
# with debug=True. An Exception will be raised if something goes wrong.
# Create the Camera instance
try:
# Create the Camera instance
print("camera stream")
camera = nano.Camera(camera_type=1, device_id=0, fps=30, debug=True)
except Exception as e:
# handle the exception from opening camera session
else:
print('USB Camera ready? - ', camera.isReady())
while True:
try:
# read the camera image
frame = camera.read()
# do something with frame like: send_to_cloud(frame)
except KeyboardInterrupt:
break
except Exception as e:
# handle the exception from reading
break
print("done here")
try:
# close the camera instance
camera.release()
except Exception as e:
# handle the exception from releasing the camera
自定义了错误的等级:
代码语言:javascript复制The except cause might catch the following exceptions:
>> Exception Type - Error: Could not initialize USB Camera
>> Exception Type - An error as occurred. Error Value: [0, 3]
>> Exception Type - Unknown Error has occurred
>> Exception Type - Error: Could not release camera
这个就是抛出的东西,可以看到到底是哪里出了毛病。
大概就是这么多了,接下来的话,就是去机器上面运行了~