Linux USB摄像头使用

2023-11-19

Linux USB摄像头使用

一、使用V4l2工具调试摄像头

1.v4l2-ctl 常用操作

# 安装V4l2工具包
$ sudo apt install v4l-utils

# 通过v4l2查看摄像头设备
$ sudo v4l2-ctl --list-devices
EP28WD (usb-fe380000.usb-1.3):
        /dev/video0
        /dev/video1
        
# 查看当前摄像头支持的视频压缩格式
$ sudo v4l2-ctl -d /dev/video0 --list-formats
        ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'YUYV'
        Name        : YUYV 4:2:2

        Index       : 1
        Type        : Video Capture
        Pixel Format: 'H264' (compressed)
        Name        : H.264

        Index       : 2
        Type        : Video Capture
        Pixel Format: 'MJPG' (compressed)
        Name        : Motion-JPEG

# 查看摄像头所有参数
$ sudo v4l2-ctl -d  /dev/video0 --all
Driver Info (not using libv4l2):
        Driver name   : uvcvideo
        Card type     : EP28WD
        Bus info      : usb-fe380000.usb-1.3
        Driver version: 4.4.167
        Capabilities  : 0x84A00001
                Video Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
        Width/Height      : 640/480
        Pixel Format      : 'YUYV'
        Field             : None
        Bytes per Line    : 1280
        Size Image        : 614400
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 640, Height 480
        Default     : Left 0, Top 0, Width 640, Height 480
        Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 640, Height 480
Selection: crop_bounds, Left 0, Top 0, Width 640, Height 480
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 25.000 (25/1)
        Read buffers     : 0
                     brightness (int)    : min=-64 max=64 step=1 default=0 value=0
                       contrast (int)    : min=0 max=64 step=1 default=36 value=36
                     saturation (int)    : min=0 max=128 step=1 default=64 value=64
                            hue (int)    : min=-40 max=40 step=1 default=0 value=0
 white_balance_temperature_auto (bool)   : default=1 value=1
                          gamma (int)    : min=72 max=500 step=1 default=100 value=100
                           gain (int)    : min=0 max=100 step=1 default=0 value=0
           power_line_frequency (menu)   : min=0 max=2 default=1 value=1
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=4600 value=4600 flags=inactive
                      sharpness (int)    : min=0 max=6 step=1 default=3 value=3
         backlight_compensation (int)    : min=0 max=2 step=1 default=1 value=1
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute (int)    : min=1 max=5000 step=1 default=157 value=157 flags=inactive
         exposure_auto_priority (bool)   : default=0 value=1
         
    
 # 查看摄像头所支持的分辨率
 sudo v4l2-ctl --list-framesizes=MJPG -d /dev/video0
 ioctl: VIDIOC_ENUM_FRAMESIZES
    Size: Discrete 640x480   # Width/Height
    Size: Discrete 800x600
    Size: Discrete 1280x720
    Size: Discrete 1280x960
    Size: Discrete 1920x1080
    Size: Discrete 1600x1200
    Size: Discrete 2048x1536
    Size: Discrete 2592x1944
    Size: Discrete 3264x2448

2.v4l2-ctl 命令

$ v4l2-ctl help
unknown arguments: help 

General/Common options:
  --all              display all information available
  -C, --get-ctrl=<ctrl>[,<ctrl>...]
                     get the value of the controls [VIDIOC_G_EXT_CTRLS]
  -c, --set-ctrl=<ctrl>=<val>[,<ctrl>=<val>...]
                     set the value of the controls [VIDIOC_S_EXT_CTRLS]
  -D, --info         show driver info [VIDIOC_QUERYCAP]
  -d, --device=<dev> use device <dev> instead of /dev/video0
                     if <dev> starts with a digit, then /dev/video<dev> is used
  -e, --out-device=<dev> use device <dev> for output streams instead of the
                     default device as set with --device
                     if <dev> starts with a digit, then /dev/video<dev> is used
  -h, --help         display this help message
  --help-all         all options
  --help-io          input/output options
  --help-misc        miscellaneous options
  --help-overlay     overlay format options
  --help-sdr         SDR format options
  --help-selection   crop/selection options
  --help-stds        standards and other video timings options
  --help-streaming   streaming options
  --help-tuner       tuner/modulator options
  --help-vbi         VBI format options
  --help-vidcap      video capture format options
  --help-vidout      vidout output format options
  --help-edid        edid handling options
  -k, --concise      be more concise if possible.
  -l, --list-ctrls   display all controls and their values [VIDIOC_QUERYCTRL]
  -L, --list-ctrls-menus
             display all controls and their menus [VIDIOC_QUERYMENU]
  -r, --subset=<ctrl>[,<offset>,<size>]+
                     the subset of the N-dimensional array to get/set for control <ctrl>,
                     for every dimension an (<offset>, <size>) tuple is given.
  -w, --wrapper      use the libv4l2 wrapper library.
  --list-devices     list all v4l devices
  --log-status       log the board status in the kernel log [VIDIOC_LOG_STATUS]
  --get-priority     query the current access priority [VIDIOC_G_PRIORITY]
  --set-priority=<prio>
                     set the new access priority [VIDIOC_S_PRIORITY]
                     <prio> is 1 (background), 2 (interactive) or 3 (record)
  --silent           only set the result code, do not print any messages
  --sleep=<secs>     sleep <secs>, call QUERYCAP and close the file handle
  --verbose          turn on verbose ioctl status reporting

3.usb摄像头与video匹配

linux系统接上usb摄像头会出现/dev/video*设备,如果接多个摄像头,通过/dev/video*无法与真实摄像头一一对应。由于有的时候一个usb摄像头会产生两个或多个video(只有一个能拍照),所以通过udev的rule规则也无法固定。这时可以通过/dev/v4l/by-id/路径的软链接固定。

$ ll /dev/v4l/by-id/
lrwxrwxrwx 1 root root  12 Sep 10 14:32 usb-Generic_HDR_CAMERA_200901010001-video-index0 -> ../../video0
lrwxrwxrwx 1 root root  12 Sep 10 14:32 usb-Generic_HDR_CAMERA_200901010001-video-index1 -> ../../video1
lrwxrwxrwx 1 root root  12 Sep 10 14:24 usb-USB3.0_Camera_8M_USB3.0_Camera_8M_SITN00000000001-video-index0 -> ../../video2
lrwxrwxrwx 1 root root  12 Sep 10 14:24 usb-USB3.0_Camera_8M_USB3.0_Camera_8M_SITN00000000001-video-index1 -> ../../video3

二、安装应用程序显示摄像头捕捉到的视频

1.使用应用程序camorama

sudo apt-get install camorama
camorama

#如果使用Xshell进行ssh远程访问,会提醒安装Xmanager软件显示视频流
  • Xmanager是一个运行于MS Windows平台上的高性能的X window服务器。可以在本地PC上同时运行Unix/Linux和Windows图形应用程序。

  • Xmanager可以将PC变成X Windows工作站,它可以无缝拼接到UNIX 应用程序中。在UNIX/Linux和Windows网络环境中,Xmanager 是最好的连通解决方案。

2.使用应用程序茄子(cheese)

cheese是Ubuntu自带摄像软件,中文名称茄子。

cheese + 设备号

sudo apt-get install cheese
cheese
# 或
cheese /dev/video0

#不支持ssh远程显示

三、ffmpeg获取摄像头图像

sudo apt-get install ffmpeg

sudo ffmpeg -i /dev/video0 -vframes 1 -s 640x480 -q:v 2 -f image2 /tmp/image6-vdieo0-test1.jpg

FFmpeg常用命令

  • 对海康IP摄像头进行拍照、推流和保存视频
sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4 \ # base
    -vcodec copy -an -f flv rtmp://172.17.0.1/live/camera0 \    # stream
    -flags +global_header -f stream_segment -segment_time 60 -segment_format_options movflags=+faststart -reset_timestamps 1 -vcodec copy -q:v 4 -an -r 24 -strftime 1 /data/tmpfs/camera/video/%s.flv \    # video
    -r 2/1 -strftime 1 -y /data/tmpfs/camera/capture.jpg  # capture

# 从ip摄像头推流
sudo ffmpeg -rtsp_transport tcp -i rtsp://admin:jiangxing123@10.56.0.17:554/h264/ch1/sub/av_stream -vcodec copy -an -f flv rtmp://10.55.2.185:1935/live/camera0

# 从视频文件推流
sudo ffmpeg -re  -i record.flv -vcodec copy -an -f flv  rtmp://10.55.2.185:1935/live/camera0

# 从usb摄像头推流
sudo ffmpeg -f video4linux2 -qscale 10 -r 12 -s 640x480 -i /dev/video0  -f flv rtmp://10.55.2.185:1935/live/camera0

# ==============test==============
sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4  -f flv -c:v libx264 -an rtmp://10.55.2.185:1935/live/camera0
sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4  -vcodec copy -an  -f flv  rtmp://10.55.2.185:1935/live/camera0

sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4 -vcodec libx264  -crf 5 -acodec aac -f flv rtmp://10.55.2.185:1935/live/camera0 

sudo ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.19:554/mpeg4   -vcodec libx264 -acodec aac -f flv rtmp://10.55.2.185:1935/live/camera0

四、gstreamer获取摄像头图像

gstreamer v4l2src 插件

# install gstreamer
apt-get -y install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio

# 连续拍照
gst-launch-1.0 --gst-debug-level=3 v4l2src device=/dev/video0 ! \
        image/jpeg,width=1920,height=1080,framerate=30/1 ! \
        multifilesink location=/data/tmpfs/capture1.jpg max-files=1 post-messages=true
        
# 单次拍照
gst-launch-1.0 v4l2src device=/dev/video0 num-buffers=1 ! image/jpeg,width=1920,height=1080 ! filesink location=/data/tmpfs/capture2.jpg
  • 对海康IP摄像头进行拍照、推流和保存视频
gst-launch-1.0 -e --gst-debug-level=3 rtspsrc location=rtsp://192.168.1.19:554/mpeg4 ! rtph264depay ! h264parse ! tee name=t \
    t. ! queue ! avdec_h264 ! queue flush-on-eos=true ! videorate ! video/x-raw,framerate=5/1 ! jpegenc ! multifilesink post-messages=true location=/data/tmpfs/camera/capture.jpg max-files=1 \    # capture
    t. ! queue ! flvmux streamable=true ! rtmpsink sync=false location=rtmp://172.17.0.1/live/camera0 \ # stream
    t. ! queue ! splitmuxsink max-size-time=600000000000 location=/data/tmpfs/camera/video/%06d.mp4 # video

五、图片加上文字

https://blog.gtwang.org/linux/imagemagick-image-watermarking-tutorial/

html颜色代码

Ubuntu 添加中文字体

sudo apt-get install imagemagick

# 左下角
convert /data/tmpfs/capture.jpg -gravity SouthWest -pointsize 40 \
   -stroke none   -fill white    -annotate +5+5  "`date "+%Y-%m-%d %H:%M:%S"`"  \
   -stroke none    -fill white    -annotate +5+70  "$text"   /data/tmpfs/output.jpg
   
# 左上角
convert /data/tmpfs/capture.jpg -gravity NorthWest -pointsize 40 \
   -stroke none   -fill white    -annotate +5+5  "`date "+%Y-%m-%d %H:%M:%S"`"  \
   -stroke '#40DCD7'    -fill white    -annotate +5+70  "$text"   /data/tmpfs/output.jpg
   
 # 有阴影的文字
convert /data/tmpfs/capture.jpg \
  \( -background 'rgba(0, 0, 0, .5)' -fill white \
     -font Helvetica -pointsize 60                \
      label:"`date "+%Y-%m-%d %H:%M:%S"` " -splice 20x20               \
  \) -gravity NorthWest -geometry +40+40 \
  -composite /data/tmpfs/output2.jpg
 
   
# 使用中文字体
text="Hello"
text2="D10.08"
# font对应中文字体路径
convert /data/tmpfs/capture.jpg -font /usr/share/fonts/truetype/wqy/wqy-microhei.ttc  -gravity NorthWest -pointsize 60 \
   -stroke none   -fill white  -annotate +40+40  "`date "+%Y-%m-%d %H:%M:%S"`"  \
   -gravity NorthEast -pointsize 60 \
   -stroke none   -fill white  -annotate +40+40  "$text2" \
   -gravity SouthEast -pointsize 60 \
   -stroke none   -fill white  -annotate +40+40  "$text"  /data/tmpfs/output.jpg
  • 图片方位

    • NorthWestNorthNorthEastWestCenterEastSouthWestSouthSouthEast
  • 安装中文字体

    sudo apt install -y --force-yes --no-install-recommends fonts-wqy-microhei
    sudo apt install -y --force-yes --no-install-recommends ttf-wqy-zenhei
    # 查看中文字体路径
    $ fc-list :lang=zh-cn
    /usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑,文泉驿微米黑:style=Regular
    /usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑,文泉
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linux USB摄像头使用 的相关文章

随机推荐

  • 【华为云计算产品系列】云上容灾架构实战部署详解

    华为云计算产品系列 云上容灾架构实战部署详解 1 前言 2 容灾方案介绍 2 1 本地高可用 2 2 同城双活 2 3 主备容灾 同步远程复制 异步远程复制 2 3 1 同步远程复制 2 3 2 异步远程复制 2 4 两地三中心 3 基于主
  • AI 编程助手 Amazon CodeWhisperer 使用(持续更新中)

    文章作者 天博 近期 chatGPT 非常火爆 试用一些公众号功能 及自己接入的 chatGPT 写小作文能力一流 但是准确率很低 一直在搜寻可以在 idea 中使用 且非常丝滑的 AI 编程工具 前不久看到 infoq 推荐的 Amazo
  • Java求出水仙花数

    题目要求 水仙花数必须满足如下2个要求 1 水仙花数是一个三位数 2 水仙花数的个位 十位 百位的数字立方和等于原数 实现思路 1 写出一个100 999的循环 2 将个位 百位和十位求出并计算其平方和 3 判断平方和是否与原数相同 实现代
  • flutter 文字拼接

    Container margin EdgeInsets only right 10 child Row mainAxisSize MainAxisSize min children ConstrainedBox constraints Bo
  • matlab直接在图上加图注,MATLAB insertShape图像上直接添加图像

    官方说明文档 http cn mathworks com help vision ref insertshape html jsessionid cf20e78d9c69962c2a47d7672c89 用figure 和plot的方式无法
  • 行为型模式-状态模式

    package per mjn pattern state after 环境角色类 public class Context 定义对应状态对象的常量 public final static OpeningState OPENING STAT
  • querystring模块

    querystring decode querystring encode querystring escape str querystring parse str sep eq options querystring stringify
  • 作为程序员,你是如何在工作以后找到女朋友的?

    作为程序员 你必然经历这样的一个场景 一个温馨的夜晚 一个可爱懵懂的妹子找你求助 想让你到她家修电脑 如果没有 那么请照镜子检查BUG 一般情况下 场景内会发生三种可能 1 你是一个计算机大牛 从进屋检查到修完电脑再到离开 全程五分钟说拜拜
  • 计算机网络:IP数据报文分析

    IP 数据报前 40 字节 编号 1 45 00 05 dc 4a cb 20 00 40 01 4B 52 c0 a8 00 27 7c 7f cf 0f 08 00 45 2e 00 01 00 17 61 62 63 64 65 66
  • Java 密码学相关知识

    密码的发展 密码的起源 密码的起源非常早 早期的符号密码像宗教符号 岩壁画 至今无人破解的 费托斯圆盘 和无人读懂的 伏尼契手稿 古代隐写术 国内的 封蜡技术 隐形墨水 吞服信条 国外的 卡登格子隐藏法 意大利数学家卡登 两张带孔的卡片 文
  • 在mybatis-plus项目中自带要编写sql语句,@select注解的使用

    提示 在mybatisplus中编写sql语句有两种方法 我重点使用 select注释这种方法 还有一种是增加mapper xml文件配置 这里不做讲解 在mybatis plus项目中自带要编写sql语句 select注解的使用 前言 一
  • Servlet3.0基础

    一 要求 1 MyEclipse10 0或以上版本 2 发布到Tomcat7 0或以上版本 二 步骤 1 创建javaEE6 0应用 三 概述 注解代替web xml配置文件 异步处理 对上传的支持 四 注解代替配置文件 1 删除web x
  • 【java篇】线程安全问题(大总结)

    哎嘿 CSDN的大佬您来啦 这来都来了 浅浅的给个赞呗 系列文章目录 线程的创建与主要方法分析和其他基础知识点 可以参考以下文章 线程知识点总结 南斋孤鹤的博客 CSDN博客 线程知识 超全 线程知识点 及线程方面的一些理解性问题https
  • Haproxy+keepalived(高可用集群部署)

    Haproxy keepalived Haproxy定义 Haproxy应用 Haproxy支持的调度算法 Haproxy配置文件详解 haproxy keepalived优点 项目环境 主服务器配置 1 haproxy配置 2 keepa
  • js制作简单的轮播图

    实现原理 首先定义一个div 设置width和height 然后在这个div里面再定义一个div2 该div的宽度为父div的宽度的n倍 其中n表示图片的张数 在这个div2里面放置需要进行轮播的所有图片 设置每张图片的宽度为一个最外层父类
  • eclipse javaweb 项目报错 The type javax.servlet.http.HttpServletRequest cannot be resolved.

    问题 把一个项目 copy 到自己电脑上后 图片 javaweb 项目 各种红 报错 jsp 页面也报错 Description Resource Path Location Type The type javax servlet http
  • 多益网络校招 —— 二面hr面

    11月27号晚上技术面试 12月6号才收到hr面试通知 12月8号hr面 整轮hr面试只有15分钟 一开始是做自我介绍 然后问了以下问题 1 我父母对我的职业有什么看法 2 之前有没有拿到满意的offer 3 为什么学前端 4 未来职业规划
  • docker 安装wiki.js 和wekan

    wiki js https blog csdn net vegas lee article details 122356646 wekan http t zoukankan com caihemm p 14446937 html
  • 用python实现英文字母和相应序数转换

    用python实现英文字母和相应序数转换 第一步 字母转数字 英文字母转对应数字相对简单 可以在命令行输入一行需要转换的英文字母 然后对每一个字母在整个字母表中匹配 并返回相应的位数 然后累加这些位数即可 过程中 为了使结果更有可读性 输出
  • Linux USB摄像头使用

    Linux USB摄像头使用 一 使用V4l2工具调试摄像头 1 v4l2 ctl 常用操作 安装V4l2工具包 sudo apt install v4l utils 通过v4l2查看摄像头设备 sudo v4l2 ctl list dev