WK520077778 发表于 2023-12-13 14:44

Raspberry Pi树莓派上安装usb网络摄像头

本帖最后由 WK520077778 于 2023-12-13 22:18 编辑

Raspberry Pi树莓派上安装usb网络摄像头

      Raspberry Pi树莓派是为学习计算机编程教育设计的微型电脑,具有体积小,功耗低,兼容性好,接口丰富等优点,具有电脑的所有基本功能,比如:上网,处理文档,听音乐,打游戏,看视频等。大量用户为树莓派提供新的应用场景,在编程教育,科研领域和开发者中有广泛使用。

       Raspberry Pi树莓派的系统是基于debian开发的Raspberry Pios(raspbian),用户可以安装windows 10iot,可以运行Linux操作系统。连接鼠标,键盘和显示器。

       Raspberry Pi树莓派的硬件,基于ARM的微型电脑主板,以SD/MicroSD卡为内存硬盘,卡片主板周围有1/2/4个USB接口和一个10/100 以太网接口(A型没有网口),可连接键盘、鼠标和网线,同时拥有视频模拟信号的电视输出接口和HDMI高清视频输出接口,以上部件全部整合在一张仅比信用*稍大的主板上,具备所有PC的基本功能只需接通电视机和键盘,连接键盘,鼠标,网线,电视机,外界USB设备。

       树莓派 Raspberry Pi 5 是Raspberry Pi系列计算机中的最新产品,与 Raspberry Pi 4 产品相比,它的 CPU 性能提高了 2-3 倍,GPU 性能大幅提升,摄像头、显示屏和 USB 接口也得到了改进。同时为了帮助追踪产品销售供退货情况,在电路板上侧打标记,同时印制电路板和纸箱提供唯一提供追踪的序列号。
      Raspberry Pi上安装usb网络摄像头,树莓派一块,32G以上内存卡一张,读卡器(或带sd卡接口的电脑)一个;网络摄像头,公母杜邦线若干,面包板一个,micro USB线一根,电源一个;HDMI接口显示器一台(或HDMI转其它接口)。
      集成开发环境: Visual Studio 2017(建议使用社区版);开发语言:C#;开发平台:UWP;通信协议:MQTT;树莓派操作系统:WIndows 10 IoT Core;数据库:Sqlite.ssh远端控制Raspberry Pi.   配置Raspberry Pi:按照说明书安装Raspberry Pi,在你的操作系统中依次选择Raspberry Pi OS (Other) > Raspberry Pi OS (Legacy) Lite安装。进入你选择的hostname pi-webcam,通过授权认证键入username and password,让Raspberry Pi上网需要配置wireless LAN 自动连接 Wi-Fi,在wifi网络中找到自己网络配置ssid和password,并且打开ssh你能不用键盘和鼠标连接。打开终端连接你的电脑,通过ssh连接Raspberry Pi ,按照这个命令用你选择的成像器取代 <username>。
    打开电脑的会话终端,用ssh进入Raspberry Pi <username>处,输入你所选择的用户名指令。

   如果是首次运行,系统连接需要弹出提示框,要求你在Raspberry Pi Imager:中配置账户密码。

   现在你连接Raspberry Pi, 运行两个指令确保你的安装包升级到最新版本。一但更新指令完成,重启你的Raspberry Pi使得安装配置生效。连接用Raspberry Pi OTG (On-The-Go) USB设备连接电脑,我们用以下命令添加到/boot/firmware/config.txt file.

下一步我们需要安装几个预装程序:

meson是一个开源构架系统,我们用它把各种摄像头软件汇总, Libcamera 和 Libjpeg 可以让Raspberry Pi使用摄像头,键入y确认安装这些程序。

然后我们下载UVC gadget 软件,这可以Raspberry Pi的usb播放视频流。回到下载界面:

可以开始构建和安装软件,通过如下code命令实现:
回到下载界面:
接下来,创建一个脚本,该脚本将在每次Raspberry Pi通电时运行,以便设置所有内容。使用nano文本编辑器创建bash脚本:

在文本编译器中键入如下代码:
#!/bin/bash
# Variables we need to make things easier later on.

CONFIGFS="/sys/kernel/config"
GADGET="$CONFIGFS/usb_gadget"
VID="0x0525"
PID="0xa4a2"
SERIAL="0123456789"
MANUF=$(hostname)
PRODUCT="UVC Gadget"
BOARD=$(strings /proc/device-tree/model)
UDC=`ls /sys/class/udc` # will identify the 'first' UDC
# Later on, this function is used to tell the usb subsystem that we want
# to support a particular format, framesize and frameintervals
create_frame() {
      # Example usage:
      # create_frame <function name> <width> <height> <format> <name> <intervals>
      FUNCTION=$1
      WIDTH=$2
      HEIGHT=$3
      FORMAT=$4
      NAME=$5
      wdir=functions/$FUNCTION/streaming/$FORMAT/$NAME/${HEIGHT}p
      mkdir -p $wdir
      echo $WIDTH > $wdir/wWidth
      echo $HEIGHT > $wdir/wHeight
      echo $(( $WIDTH * $HEIGHT * 2 )) > $wdir/dwMaxVideoFrameBufferSize
      cat <<EOF > $wdir/dwFrameInterval
$6
EOF
}
# This function sets up the UVC gadget function in configfs and binds us
# to the UVC gadget driver.
create_uvc() {
      CONFIG=$1
      FUNCTION=$2
      echo "      Creating UVC gadget functionality : $FUNCTION"
      mkdir functions/$FUNCTION
      create_frame $FUNCTION 640 480 uncompressed u "333333
416667
500000
666666
1000000
1333333
2000000
"
      create_frame $FUNCTION 1280 720 uncompressed u "1000000
1333333
2000000
"
      create_frame $FUNCTION 1920 1080 uncompressed u "2000000"
      create_frame $FUNCTION 640 480 mjpeg m "333333
416667
500000
666666
1000000
1333333
2000000
"
      create_frame $FUNCTION 1280 720 mjpeg m "333333
416667
500000
666666
1000000
1333333
2000000
"
      create_frame $FUNCTION 1920 1080 mjpeg m "333333
416667
500000
666666
1000000
1333333
2000000
"
      mkdir functions/$FUNCTION/streaming/header/h
      cd functions/$FUNCTION/streaming/header/h
      ln -s ../../uncompressed/u
      ln -s ../../mjpeg/m
      cd ../../class/fs
      ln -s ../../header/h
      cd ../../class/hs
      ln -s ../../header/h
      cd ../../class/ss
      ln -s ../../header/h
      cd ../../../control
      mkdir header/h
      ln -s header/h class/fs
      ln -s header/h class/ss
      cd ../../../
      # This configures the USB endpoint to allow 3x 1024 byte packets per
      # microframe, which gives us the maximum speed for USB 2.0. Other
      # valid values are 1024 and 2048, but these will result in a lower
      # supportable framerate.
      echo 2048 > functions/$FUNCTION/streaming_maxpacket
      ln -s functions/$FUNCTION configs/c.1
}
# This loads the module responsible for allowing USB Gadgets to be
# configured through configfs, without which we can't connect to the
# UVC gadget kernel driver
echo "Loading composite module"
modprobe libcomposite
# This section configures the gadget through configfs. We need to
# create a bunch of files and directories that describe the USB
# device we want to pretend to be.
if
[ ! -d $GADGET/g1 ]; then
      echo "Detecting platform:"
      echo "board : $BOARD"
      echo "udc   : $UDC"
      echo "Creating the USB gadget"
      echo "Creating gadget directory g1"
      mkdir -p $GADGET/g1
      cd $GADGET/g1
      if
[ $? -ne 0 ]; then
                echo "Error creating usb gadget in configfs"
                exit 1;
      else
                echo "OK"
      fi
      echo "Setting Vendor and Product ID's"
      echo $VID > idVendor
      echo $PID > idProduct
      echo "OK"
      echo "Setting English strings"
      mkdir -p strings/0x409
      echo $SERIAL > strings/0x409/serialnumber
      echo $MANUF > strings/0x409/manufacturer
      echo $PRODUCT > strings/0x409/product
      echo "OK"
      echo "Creating Config"
      mkdir configs/c.1
      mkdir configs/c.1/strings/0x409
      echo "Creating functions..."
      create_uvc configs/c.1 uvc.0
      echo "OK"
      echo "Binding USB Device Controller"
      echo $UDC > UDC
      echo "OK"
fi
# Run uvc-gadget. The -c flag sets libcamera as a source, arg 0 selects
# the first available camera on the system. All cameras will be listed,
# you can re-run with -c n to select camera n or -c ID to select via
# the camera ID.
保存好代码后,通过以下指令执行脚本:$ sudo chmod +x ~/.rpi-uvc-gadget.sh为了让设备在每次启动自动引导,添加/etc/rc.local file.系统将在启动后自动引导可执行文件$ sudo nano /etc/rc.local输入以下代码在0上方执行脚本,将<username> 占位符中指定你的用户名。$ /home/<username>/.rpi-uvc-gadget.sh &最后关闭Raspberry Pi:$ sudo shutdown -h now用电线连接摄像头到 Raspberry Pi。





小灵通2018 发表于 2024-1-27 21:28

高大上的**

班杰明 发表于 2024-1-29 11:56

树莓派价格越来越贵了

drawingchips 发表于 2024-1-29 17:36

现在树莓派的开发板,越来越贵了。

SophiaOP 发表于 2024-2-21 16:57

树莓派的生态是好啊,就是价格比较肉疼

AloneKaven 发表于 2024-2-22 18:58

用国产平替也是不错的

OliviaSH 发表于 2024-2-25 21:07

AloneKaven 发表于 2024-2-22 18:58
用国产平替也是不错的

国产的生态做的不行
页: [1]
查看完整版本: Raspberry Pi树莓派上安装usb网络摄像头