【BIGTREETECH PI开发板】+05.UVC(USB摄像头)应用:边沿检测和视频推流(zmj)
1. 边沿检测
**BIGTREETECH PI开发板的USB摄像头(UVC)可以十分轻松的实现基于OPENCV的边沿检测功能。
//------UVC / USB-Camera
UVC通俗的讲就是USB摄像头,全称为USB Video Class,即:USB视频类,是一种为USB视频捕获设备定义的协议标准。
USB摄像头的视频流主要通过python调用OpenCV库完成对每一帧图像的边沿检测,参数调节通过滑动条实时调节以获取更好结果,按下ESC键退出测试程序(不得不说python是真的强大):
//------基于OPENCV的摄像头视频边沿检测
//---指令(python2或者python3均可以):
python3 ./edge.py 4
或者
python ./edge.py 4
//---指令中的参数”4“说明:
使用USB-Camera,它位于/dev/video4
//------依赖(根据错误提示安装对应的依赖库及以来组件):
sudo apt install opencv*
sudo apt install libopencv*
sudo apt install gststream*
sudo apt install x11proto-video* python3-mecavideo multimedia-video libvideo* librust-gstreamer-video* gnome-video*
1.1 代码解析
代码功能说明:
//------代码功能说明:
a.导入必要的库:cv2、numpy、video 和 sys。
b.创建两个滑动条:cv.namedWindow('edge') 用来显示边缘检测的结果窗口,cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing) 和 cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing) 用来设置边缘检测的参数。
c.创建一个视频捕获对象:cap = video.create_capture(fn),其中 fn 是指定的视频文件名。
d.在一个无限循环中,从视频捕获对象中读取帧数据:while True:。
e.将读取的帧数据转换为灰度图像:gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)。
f.对灰度图像进行边缘检测:edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)。
g.将原始图像和边缘检测结果合并:vis = img.copy(),然后将结果图像中非零像素的部分设为绿色:vis = np.uint8(vis/2.),最后将结果图像显示出来:cv.imshow('edge', vis)。
h.监听窗口的关闭事件:ch = cv.waitKey(5),如果按下了ESC键,则退出循环:if ch == 27:。
i.最后打印程序的文档字符串:print(__doc__),并关闭所有窗口:cv.destroyAllWindows()。
//------完整代码:
#!/usr/bin/env python
from __future__ import print_function
import cv2 as cv
import numpy as np
# relative module
import video
# built-in module
import sys
def main():
try:
fn = sys.argv[1]
except:
fn = 0
def nothing(*arg):
pass
cv.namedWindow('edge')
cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing)
cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing)
cap = video.create_capture(fn)
while True:
_flag, img = cap.read()
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
thrs1 = cv.getTrackbarPos('thrs1', 'edge')
thrs2 = cv.getTrackbarPos('thrs2', 'edge')
edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)
vis = img.copy()
vis = np.uint8(vis/2.)
vis[edge != 0] = (0, 255, 0)
cv.imshow('edge', vis)
ch = cv.waitKey(5)
if ch == 27:
break
print('Done')
if __name__ == '__main__':
print(__doc__)
main()
cv.destroyAllWindows()
1.2 测试效果
//------原图:
//------检测效果(参数合适):
//------检测效果(低参):丢失了很多检测细节
//------检测效果(高参):此时引入了很多图像噪点
//------检测效果
uvc_video
2. 视频推流测试
BIGTREETECH PI开发板的USB摄像头(UVC)借助mjpg-streamer实现网页监控推流十分便捷。
2.1 下载编译
下载编译流程如下:
//------mjpg-streamer
//---下载
git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer
cd mjpg-streamer-experimental
gcc -v
//---依赖项/依赖库的安装
sudo apt install cmake
sudo apt-get install -y cmake libjpeg62-turbo-dev
//---编译安装mjpg-streamer
sudo apt install cmake
make all
make install
//---测试指令
sudo /usr/local/bin/mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 1280x720 -y" -o "output_http.so"
部分测试配置(自动)如下所示:
设备:/dev/video0
分辨率:1280*720(720P)
帧率:1帧/秒(此种情况下节省网络带宽,降低CPU占用率)
格式:YUYV
端口:8080
//---访问方式(PC端和手机端均OK):
http://192.168.7.228:8080/?action=stream
//------编译安装:
//------温度变化:
2.2 测试结果
最终呈现效果是网页端实时刷新(1帧/秒)摄像头结果,统一局域网内在PC端和手机端均能正常查看推流效果。
//---测试指令
sudo /usr/local/bin/mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 1280x720 -y" -o "output_http.so"
部分测试配置(自动)如下所示:
设备:/dev/video0
分辨率:1280*720(720P)
帧率:1帧/秒(此种情况下节省网络带宽,降低CPU占用率)
格式:YUYV
端口:8080
//---访问方式(PC端和手机端均OK):
http://192.168.7.228:8080/?action=stream
//------测试Log
biqu@BTT-CB1:~/zmj_ws/gst/mjpg-streamer/mjpg-streamer-experimental$ \
> ./mjpg_streamer -h
-----------------------------------------------------------------------
Usage: ./mjpg_streamer
-i | --input "<input-plugin.so> [parameters]"
-o | --output "<output-plugin.so> [parameters]"
[-h | --help ]........: display this help
[-v | --version ].....: display version information
[-b | --background]...: fork to the background, daemon mode
-----------------------------------------------------------------------
Example #1:
To open an UVC webcam "/dev/video1" and stream it via HTTP:
./mjpg_streamer -i "input_uvc.so -d /dev/video1" -o "output_http.so"
-----------------------------------------------------------------------
Example #2:
To open an UVC webcam and stream via HTTP port 8090:
./mjpg_streamer -i "input_uvc.so" -o "output_http.so -p 8090"
-----------------------------------------------------------------------
Example #3:
To get help for a certain input plugin:
./mjpg_streamer -i "input_uvc.so --help"
-----------------------------------------------------------------------
In case the modules (=plugins) can not be found:
* Set the default search path for the modules with:
export LD_LIBRARY_PATH=/path/to/plugins,
* or put the plugins into the "/lib/" or "/usr/lib" folder,
* or instead of just providing the plugin file name, use a complete
path and filename:
./mjpg_streamer -i "/path/to/modules/input_uvc.so"
-----------------------------------------------------------------------
biqu@BTT-CB1:~/zmj_ws/gst/mjpg-streamer/mjpg-streamer-experimental$ \
> sudo /usr/local/bin/mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 1280x720 -y" -o "output_http.so"
MJPG Streamer Version: git rev: 310b29f4a94c46652b20c4b7b6e5cf24e532af39
i: Using V4L2 device.: /dev/video0
i: Desired Resolution: 1280 x 720
i: Frames Per Second.: -1
i: Format............: YUYV
i: JPEG Quality......: 80
i: TV-Norm...........: DEFAULT
UVCIOC_CTRL_ADD - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Mode: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Frequency: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Disable video processing: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Raw bits per pixel: Inappropriate ioctl for device (25)
o: www-folder-path......: disabled
o: HTTP TCP port........: 8080
o: HTTP Listen Address..: (null)
o: username:password....: disabled
o: commands.............: enabled
^[^Csetting signal to stop //手动Ctrl+C退出//
i: cleaning up resources allocated by input thread
o: cleaning up resources allocated by server thread #00
force cancellation of threads and cleanup resources
done
biqu@BTT-CB1:~/zmj_ws/gst/mjpg-streamer/mjpg-streamer-experimental$
//------测试场景:PC端和手机端
//------执行测试:
//------测试结果:
<- 测试结果视频 ->
UVC-mjpg_streamer
//------end