树莓派摄像头测试

一、树莓派摄像头安装和使用

使用usb摄像头进行实时监控步骤:

**1.安装motion程序
$ sudo apt-get install motion
2.配置motion程序
sudo vim /etc/default/motion
把no改成yes,开启motion的daemon一直检测设备。
$ sudo vim /etc/motion/motion.conf
把daemon off改成daemon on
确认视频流的接口是8081
把stream_localhost on改成stream_localhost off,关闭localhost本地的限制。
把sdl_threadnr注释掉。
保存文件,退出。
3.启动motion程序的daemon
$ sudo motion
只要这个motion一直开着,就支持热插拔。
关闭重启motion:
$ sudo killall -TERM motion
$ sudo motion
4.打开浏览器查看树莓派的摄像头影像
地址是:http://树莓派IP地址:8081

二、python人脸定位

A 目标检测步骤:
1)安装软件
pip3 install opencv-python
sudo apt-get install libatlas-base-dev libjasper-dev
sudo apt-get install libgstreamer1.0-0
sudo apt-get install libgstreamer-plugins-base1.0-0
sudo apt-get install libqtgui4 libqt4-test
2)行人检测
行人检测HOG+SVM
1、提取样本hog特征。
2、投入svm分类器训练,得到model。
3、由model生成检测子。
4、利用检测子检测负样本,得到hardexample。
5、提取hardexample的hog特征并结合第一步中的特征一起投入训练,得到最终检测子。
代码:

import cv2
def is_inside(o, i):
    ox, oy, ow, oh = o
    ix, iy, iw, ih = i
    # 如果符合条件,返回True,否则返回False
    return ox > ix and oy > iy and ox + ow < ix + iw and oy + oh < iy + ih

#根据坐标画出人物所在的位置

def draw_person(img, person):
  x, y, w, h = person
  cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)

#定义HOG特征+SVM分类器

img = cv2.imread("people3.jpg")
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
found, w = hog.detectMultiScale(img, winStride=(8, 8), scale=1.05)

#判断坐标位置是否有重叠

found_filtered = []
for ri, r in enumerate(found):
    for qi, q in enumerate(found):
        a = is_inside(r, q)
        if ri != qi and a:
            break
    else:
        found_filtered.append(r)

#勾画筛选后的坐标位置

for person in found_filtered:
    draw_person(img, person)

#显示图像

cv2.imshow("people detection", img)
while 1:
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

> B 轮廓检测 把整幅图像分成了非黑即白的二值图像。 函数为cv2.threshold()  这个函数有四个参数, 	第一个原图像,
> 	第二个进行分类的阈值, 	第三个是高于(低于)阈值时赋予的新值, 	第四个是一个方法选择参数,常用的有:  		•
> cv2.THRESH_BINARY(黑白二值)  		• cv2.THRESH_BINARY_INV(黑白二值反转)  		•
> cv2.THRESH_TRUNC (得到的图像为多像素值)  		• cv2.THRESH_TOZERO  		•
> cv2.THRESH_TOZERO_INV  该函数有两个返回值  	第一个retVal(得到的阈值值)  	第二个就是阈值化后的图像。 
> ret,thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)	
> 对于BINARY方法,当图像中的灰度值大于127的重置像素值为255.


import numpy as np
import cv2

img = cv2.imread(‘test1.jpg’)
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

 *

> 人脸定位

*
 安装软件
pip3 install opencv-python      
打开python编辑器,运行下面的代码:
import cv2 as cv
如果不报错,就表示安装成功了。
opencv分类器文件
网址  https://github.com/opencv/opencv/tree/master/data/haarcascades     
人脸定位脚本

import cv2 as cv
import numpy as np
#检测人脸                      
def face_detect_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)   #  转为灰度图像。
    face_detector = cv.CascadeClassifier("d:/shares/haarcascade_frontalface_alt.xml")  # 加载分类器。
    faces = face_detector.detectMultiScale(gray, 1.1, 2)
    for x, y, w, h in faces:
        cv.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 2)   #    人脸位置加红框。   
    cv.imshow("result", image)         # 显示已经定位人脸的图片       
 
print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("d:/shares/faces002.jpg")       # 读取图片 文件。    
face_detect_demo(src)                # 调用函数              
 
cv.waitKey(0)
 
cv.destroyAllWindows()         # 关闭所有窗口 

三、树莓派3b shell简单编程

1.用vnc客户端连接树莓派,打开树莓派图形界面,打开终端
2.先cd+你想要进入的目录下,用mkdir+目录名创建一个你想要存放shell脚本的文件下
3.进入文件夹之后,输入:sudo nano shell脚本文件名(.sh文件),进入文件编程
chmod +x ./文件名.sh #使脚本具有执行权限
./文件名.sh #执行脚本
4.输入以下程序:

#!/bin/bash
#monitor available disk space
#提取本服务器的IP地址信息  
SPACE=` df -hP | awk '{print int($5)}'`
usage1=` echo $SPACE | awk '{print int($2)}' `
echo $usage1
if [ $usage1 -ge 30 ]
then
  echo " 服务器 磁盘空间 使用率已经超过90%,请及时处理。" > ~/warning.log
fi

四、语音识别

一键三连呀!
原文地址:https://www.cnblogs.com/jee-cai/p/14095371.html