一、基础信息
二、可用的 GPIO 管脚
三、折腾摄像头端
首先 mipi 摄像头会干扰 uvc 的设备节点,因此先将 mipi 摄像头硬件卸载下来。
3.1、将 usb 摄像头硬件摸清楚
- 将 usb 摄像头插入板子,查看枚举信息:
lsusb
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 003: ID 0bda:c820 Realtek Semiconductor Corp. USB 2.0 Hub [MTT]
Bus 002 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 002: ID 1bcf:2cd1 Sunplus Innovation Technology Inc. DECXIN CAMERA
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
r 如上图,Bus 001 Device 002: ID 1bcf:2cd1 Sunplus Innovation Technology Inc. DECXIN CAMERA
已经枚举出来了
- 使用 v4l2-ctl 工具查看必要信息,通过 **
**ls /dev/video***
** 不难发现其实 /dev/video40 才是所需要的节点
v4l2-ctl
是一个 Linux 命令行工具,用于控制视频 4 Linux 设备,包括摄像头。通过这个工具,您可以查看和修改摄像头的各种设置,例如亮度、对比度、曝光等。
以下是一些常用的 v4l2-ctl
命令示例:
列出设备信息:
v4l2-ctl --list-devices
这个命令会列出系统中可用的视频设备,包括摄像头,以及每个摄像头的设备名称和相关信息。
查看当前设备的设置:
v4l2-ctl -d /dev/video0 --list-ctrls
这个命令会列出 /dev/video0
设备的可调节控制参数,比如亮度、对比度、饱和度等。
获取特定控制参数的当前值:
v4l2-ctl -d /dev/video0 --get-ctrl=exposure_auto
这个命令会显示摄像头曝光模式的当前设置。
设置特定控制参数的值:
v4l2-ctl -d /dev/video0 --get-ctrl=exposure_auto
这个命令会将摄像头的曝光模式设置为自动。
要查看摄像头支持的分辨率,
v4l2-ctl --list-formats-ext
firefly@firefly:~/Desktop$ v4l2-ctl -d /dev/video40 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1920x1200
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x960
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1024x768
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 960x720
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 1920x1200
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x960
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1024x768
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 960x720
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.011s (90.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
- 快速使用 python 来捕获视频保存本地并 load 到 windows 端查看
必须安装必要工具 : sudo apt-get install python3-opencv
使用脚本:注意是 MJPG 格式(比较常用)
import cv2
# 使用摄像头设备索引号初始化摄像头
# 通常,第一个摄像头设备的索引号为0,第二个为1,以此类推,我们用的是40
cap = cv2.VideoCapture(40)
# 检查摄像头是否正确打开
if not cap.isOpened():
print("无法打开摄像头")
exit()
# 获取视频帧的宽度和高度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 定义视频编解码器并创建 VideoWriter 对象
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (frame_width, frame_height))
# 不断循环,读取视频帧并写入输出视频文件
while True:
# 从摄像头捕获一帧图像
ret, frame = cap.read()
# 如果帧读取失败,则退出循环
if not ret:
print("无法读取帧")
break
# 将帧写入输出视频文件
out.write(frame)
# 检测键盘输入,按下ESC键退出循环
if cv2.waitKey(1) == 27:
break
# 释放摄像头资源、关闭输出视频文件并关闭所有窗口
cap.release()
out.release()
cv2.destroyAllWindows()
运行后得到:output.avi (注意强制停止,否则视频文件很大)
至此,使用 python 验证了 uvc 是可行的,并且使用 v4l2-ctl 工具验证了部分相机参数是可以被设置的,因此进入写 c++ 代码实现环节
3.2、代码实现
-
准备开发的软件环境
-
安装库,sudo apt-get install libopencv-dev
-
编写代码和 makefile
- makefile 中一般使用 pkg-config 来管理 opencv 的一些编译相关内容
- 但后面发现根本不是从 pkg 来安装 opencv 的,无法找到 opencv.pc
locate opencv.pc
命令是空的
-
改 makefile,不需要 pkg 来配置
-
源代码:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// 打开摄像头
VideoCapture cap(40);
// 检查摄像头是否正确打开
if (!cap.isOpened()) {
cout << "无法打开摄像头" << endl;
return -1;
}
// 获取摄像头的默认分辨率
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
// 定义视频编解码器并创建 VideoWriter 对象
VideoWriter video("output.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), 20, Size(frame_width, frame_height));
// 检查视频文件是否成功打开
if (!video.isOpened()) {
cout << "无法创建视频文件" << endl;
return -1;
}
// 不断循环,读取视频帧并写入输出视频文件
while (true) {
Mat frame;
// 从摄像头捕获一帧图像
cap >> frame;
// 如果帧读取失败,则退出循环
if (frame.empty()) {
cout << "无法读取帧" << endl;
break;
}
// 将帧写入输出视频文件
video.write(frame);
// 检测键盘输入,按下ESC键退出循环
if (waitKey(1) == 27) {
break;
}
}
// 释放摄像头资源、关闭输出视频文件
cap.release();
video.release();
return 0;
}
Makefile 文件:
CC=g++
CFLAGS=-std=c++11
LIBS=-lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgproc
INCLUDE=-I/usr/include/opencv4
LIBRARY=-L/usr/lib//aarch64-linux-gnu
all: capture_video
capture_video: capture_video.cpp
$(CC) $(CFLAGS) -o capture_video capture_video.cpp $(INCLUDE) $(LIBRARY) $(LIBS)
clean:
rm -f capture_video
运行截图:
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于