# supervise **Repository Path**: xuebashuoge/supervise ## Basic Information - **Project Name**: supervise - **Description**: capture image if there is somebody in the dorm - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-12-11 - **Last Updated**: 2021-12-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 配置树莓派 - 安装最新版的 - 换源,看清楚系统版本,最新的是bullseye - pip换源 - 安装配置zsh - 安装配置nvim - 安装配置tmux # 安装OpenCV ## 增加swap - `sudo nano /etc/dphys-swapfile` - `CONF_SWAPSIZE=2048` - `sudo /etc/init.d/dphys-swapfile stop` - `sudo /etc/init.d/dphys-swapfile start` ## 安装系统依赖 - `sudo apt-get install build-essential cmake git pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libatlas-base-dev gfortran python2.7-dev python3-dev libgstreamermm-1.0-dev llibgtk-3-dev libopenexr-dev webp libwebp-dev libdc1394-dev` - 确认ok可以加 `-y` - 有missing就加 `--fix-missing` ## 下载源码 - 用已有的opencv和opencv_contrib ## 配置 - `sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=/home/pi/Git/opencv_contrib/modules \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_CXX_EXAMPLES=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D BUILD_EXAMPLES=ON ..` - `cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D BUILD_opencv_xfeatures2d=OFF \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D ENABLE_NEON=ON \ -D WITH_TENGINE=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=/usr/lib/python3/dist-packages \ -D OPENCV_EXTRA_MODULES_PATH=/home/pi/Git/opencv_contrib/modules \ -D PYTHON_EXECUTABLE=/usr/bin/python3.9 \ -D BUILD_EXAMPLES=ON ..` - "'numpy/ndarrayobject.h' file not found" - `sudo apt install python3-numpy` - 不行的话就强行连接include - `sudo ln -s /usr/lib/python3/site-packages/numpy/core/include/numpy /usr/include/numpy` ## 不搞乱七八糟的 - 直接使用旧版buster的RaspbianOS - 可以直接`sudo apt install python3-opencv` - 新版bullseye兼容性太差了 # Openvino ## 下载可能要用的模型 - `python3 ~/openvino/open_model_zoo/tools/model_tools/downloader.py --name face-detection-0205,face-detection-0204,face-detection-0206,face-detection-retail-0004,face-detection-retail-0005,face-detection-adas-0001,person-detection-0200,person-detection-0201,person-detection-0202,person-detection-0203,pedestrian-detection-adas-0002` - 如下模型的输出 $\in\R^7$ - pedestrian-detection-adas-0002 - person-detection-0200 - person-detection-0201 - person-detection-0202 - face-detection-0200 - face-detection-0202 - face-detection-0204 - face-detection-adas-0001 - face-detection-retail-0004 - face-detection-retail-0005 - [image_id, label, conf, x_min, y_min, x_max, y_max] - image_id - ID of the image in the batch - label - predicted class ID (0 - person) - conf - confidence for the predicted class - (x_min, y_min) - coordinates of the top left bounding box corner, range [0,1] - (x_max, y_max) - coordinates of the bottom right bounding box corner, range [0,1]. - 如下模型的输出 $\in\R^5$ - person-detection-0203 - face-detection-0205 - face-detection-0206 - [x_min, y_min, x_max, y_max, conf] - conf - confidence for the predicted class - (x_min, y_min) - coordinates of the top left bounding box corner - (x_max, y_max) - coordinates of the bottom right bounding box corner. - 这些模型中`net.outputs["labels"].precision=I32`,加载非常慢(可能是加载不进去),不适合使用 - 使用face-detection还是person-detection - 各有优劣 - person - 识别不出被部分遮挡的人 - 比如被我的自行车遮住了 - 能够识别背影 - face - 可以身体部分被遮住,只要脸还在就行 - 下床后背对镜头识别不出来 # Python ## Log - logging库 - `logging.handlers` - `TimedRotatingFileHandler()` - 参数 - filename: the first file name - when: measure of interval, "D" for day - interval: number of interval to create a new log file - backupCount: number of kept log files - 将log信息写入文件中,按照设定的间隔写入新的文件 - 若需要使用`backupCount`功能,保存固定数量的文件,则最好不要加`suffix`使文件名都一致 - `logging.StreamHandler()` - 将log信息输入到terminal ## Vcgencmd - 树莓派出现各种自动重启 - 经查询后发现是电压不足 - 监测方法 1. 红灯闪烁甚至不亮 2. 输入`vcgencmd get_throttled`查询是否返回非`0x0` - 至少5V3A电源,加上了外设(音响充电、摄像头、风扇),可能要求更高 - vcgencmd库 - `Vcgencmd().get_throttled()` - 返回`{'raw_data': '0x50000', 'binary': '01010000000000000000', 'breakdown': {'0': False, '1': False, '2': False, '3': False, '16': True, '17': False, '18': True, '19': False}}` - 参数对应 - ```python flags = { "UNDERVOLTED" : '0', "CAPPED" : '1', "THROTTLED" : '2', "SOFT_TEMPLIMIT" : '3', "HAS_UNDERVOLTED" : '16', "HAS_CAPPED" : '17', "HAS_THROTTLED" : '18', "HAS_SOFT_TEMPLIMIT" : '19', } ``` - 判断throtted输出,从而进行warning - warning超过一定次数直接结束程序 - 防止欠电压或高温对树莓派硬件造成不可逆损失 ## 防止数据量爆炸 - 被检测者长期存在会造成发送数据过多,存储照片过多 ### 拍照间隔 - 摄像头原始拍摄帧率30FPS(0.33s一张输入) - 实际使用推理速度较短,可忽略 - 因此需要每次拍照后加`time.sleep()`进行时延 - 默认设置1s一张 ### 识别间隔 - 若被识别者一直存在则一直识别 - 设定一定间隔内不多次识别 - 利用`time.time()`计时,若该次识别时间与上次识别时间少于5min,则跳过 ## Pi Camera - picamera库 - 安全打开摄像头 - ```python with PiCamera() as cap: with PiRGBArray(cap) as raw_cap: ``` - 通常情况利用 - `cap.capture(raw_cap, 'bgr')` - `image = raw_cap.array` - 连续读取 - ```python for frame in cap.capture_continuous(raw_cap, format="bgr"): image = frame.array ... raw_cap.truncate(0) ``` - 每次capture之后需要清理array - 为避免warning刷屏可以注释掉/usr/lib/python3/dist-packages/picamera/array.py line 172-178