homeassistant中usb摄像头的使用(usb转web摄像头)

前言

在homeassistant中使用ffmpeg集成来使用usb摄像头一直是有问题的,只要你开启了stream基本上视频就出不来(默认配置default_config默认就开启了stream,如果非要用ffmpeg,就不用stream流,但是出来的效果也很差)在社区中采用的方法也是将usb转为web摄像头来使用;还有一种插件就是motioneye,这个插件可以解决usb摄像头的问题,但是这个插件的效果非常差,而且占用系统资源很大。因此这里采用的方法是使用MJPG-streamer来将usb摄像头数据转为web摄像头从而和homeassistant来完成联动,并且还可以设置验证身份访问,还是很安全的。

MJPG-streamer的安装

参考文章

#/bin/bash
path=/home/pi/stream
stream_package=(subversion libjpeg-dev imagemagick libv4l-dev cmake git)
apt install ${stream_package[@]} -y
git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer-experimental
make all
make DESTDIR=${path} install

将此脚本以sudo运行,即可安装MJPG-streamer

usb摄像头

v4l2-ctl --list-devices查看usb摄像头信息
在这里插入图片描述

web摄像头图像查看

然后·运行命令
./mjpg_streamer -i "../lib/mjpg-streamer/input_uvc.so -d /dev/video0 --resolution 1920x1080 " -o "../lib/mjpg-streamer/output_http.so -n -w /root/stream/www"

请注意库的位置根据你安装的位置改变
在这里插入图片描述
然后在局域网下打开浏览器输入
一定要是这种格式!!!

http://raspberry-ip-address:8080/?action=stream即可查看到图像

mjpg_stream help

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参数解释

输入

-n表示不初始化Linux-uvc驱动程序的dynctrl
-y表示使用YUV格式,默认:MJPEG(使用更多的cpu功率)
-f表示帧率
-r表示分辨率(可以为QQVGA QCIF CGA QVGA CIF PAL VGA SVGA XGA HD SXGA UXGA FHD 或者 如这种形式: 640x480)
-timestamp用系统时间填充帧时间戳
-rot 设置图像旋转(0-359)
-hf设置水平翻转(ture/false)
-vf设置垂直翻转(ture/false)

输出

-n禁止执行命令
-w包含网页的文件夹平面层次结构(没有子文件夹)
-c要求输入用户名和密码,调用方式如下
./mjpg_streamer -i "../lib/mjpg-streamer/input_uvc.so -d /dev/video1 -n -y -f 25 -r 640x480" -o "../lib/mjpg-streamer/output_http.so -n -c "wp:1152679377" -w /home/wp/www"
注意相应的在homeassistant配置文件中加上
在这里插入图片描述

-p端口设置
-l设置监听的ip

homeassistant使用

MJPEG IP Camera集成
根据此集成,只需要在configuration.yaml中添加即可

# Example configuration.yaml entry
camera:
  - platform: mjpeg
    mjpeg_url: http://192.168.1.92/mjpeg

其他

camera核心集成

ffmpeg使用usb摄像头

注意不要用默认配置

在这里插入图片描述

在这里插入图片描述

homeassistant学习链接

链接
目前写的还很乱,后面会整理

树莓派(docker)nginx-rtmp-raspi

首先安装docker

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

github链接
nginx-rtmp构建webcam超详细链接
然后下载dockerfile脚本

git clone https://github.com/idlerun/nginx-rtmp-raspi

然后将Dockerfile替换掉

ARG baseimg=raspbian/stretch
FROM $baseimg as build
ENV DEBIAN_FRONTEND=noninteractive
ENV SRC_DIR=/src
RUN mkdir -p $SRC_DIR
RUN apt-get update
RUN apt-get install -y \
    wget git libtool autoconf cmake \
    build-essential pkg-config
WORKDIR $SRC_DIR

RUN wget -q http://zlib.net/zlib-1.2.11.tar.gz
RUN tar -zxf zlib-1.2.11.tar.gz

RUN wget  https://www.openssl.org/source/openssl-1.1.1.tar.gz --no-check-certificate
RUN tar xf openssl-1.1.1.tar.gz

RUN wget -q https://nginx.org/download/nginx-1.15.6.tar.gz
RUN tar xf nginx-1.15.6.tar.gz

RUN wget -q https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
RUN tar xf v1.2.1.tar.gz

RUN wget https://sourceforge.net/projects/pcre/files/pcre/8.42/pcre-8.42.tar.gz/download --no-check-certificate -O pcre-8.42.tar.gz
RUN tar -zxf pcre-8.42.tar.gz

WORKDIR /src/nginx-1.15.6
RUN mkdir -p /build /conf
RUN ./configure --prefix=/build \
    --with-zlib=/src/zlib-1.2.11 \
    --with-openssl=/src/openssl-1.1.1 \
    --with-pcre=/src/pcre-8.42 \
    --sbin-path=/usr/local/bin/nginx \
    --conf-path=/conf/nginx.conf \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx-error.log \
    --http-log-path=/var/log/nginx-access.log \
    --without-mail_pop3_module \
    --without-mail_imap_module \
    --without-mail_smtp_module \
    --without-http_fastcgi_module \
    --without-http_uwsgi_module \
    --without-http_scgi_module \
    --without-http_memcached_module \
    --with-http_ssl_module \
    --with-http_gzip_static_module \
    --with-threads \
    --add-module=../nginx-rtmp-module-1.2.1
RUN make -j4
RUN make install


FROM $baseimg
COPY --from=build /usr/local/bin/nginx /usr/local/bin/
COPY --from=build /build /build
COPY nginx.conf /conf/
RUN mkdir -p /web/www /web/hls
COPY index.html /web/www/
ENTRYPOINT nginx

替换后在文件夹nginx-rtmp-raspi

sudo bash build.sh

其中使用方法在stream.conf中有

ffmpeg -re -f video4linux2 -i /dev/video0 -framerate 25 -vcodec h264_omx -map 0:v -b:v 800k -vprofile baseline -vf "scale=640:360" -f flv rtmp://localhost/show/stream

在浏览器中使用ip:8080访问

问题

解决wget报错ERROR: The certificate of ‘xxxxx’ is not trusted

在wget指令后添加–no-check-certificate

要在树莓派4B上驱动USB摄像头,你可以按照以下步骤进行操作: 1. 首先,使用命令`lsusb`查看已连接的USB设备,确保摄像头被正确识别。 2. 安装Motion软件,可以使用以下命令进行安装:`sudo apt-get install motion`。安装完成后,需要修改Motion的配置文件。 3. 修改Motion的配置文件motion.conf,可以使用以下命令进行编辑:`sudo nano /etc/motion/motion.conf`。在文件找到并修改以下参数: - 修改分辨率:将capture_width和capture_height设置为所需的分辨率,例如640 x 480。 - 修改访问地址和端口号:将stream_localhost设置为off,将stream_port设置为8081。 - 可选:如果你遇到摄像头卡顿的问题,可以尝试调整framerate参数来提高帧率。 4. 保存并关闭文件,然后重新启动Motion服务:`sudo systemctl restart motion`。 5. 接下来,你可以通过访问`http://[树莓派地址]:8081`来查看实时监控视频。默认的用户名是admin,密码为空。 6. 如果你希望将摄像头接入到Home Assistant,你需要修改Home Assistant的配置文件configuration.yaml,添加以下内容: ``` camera: - platform: generic name: 摄像头 still_image_url: http://{树莓派地址}:8081/picture/1/current/ username: admin framerate: 30 binary_sensor: - platform: ffmpeg_motion input: http://{树莓派地址}:8081/ name: 移动侦测 ``` 保存并关闭文件,然后重新启动Home Assistant。 这样,你就可以通过树莓派4B驱动USB摄像头并进行监控了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一WILLPOWER一

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值