树莓派
树莓派opencv安装
当然首选的方法还是通用的:
pip install opencv-python==version(版本可选,但从国外服务器下的很慢)
如果上面的方法尝试失败了,那么果断选择下面的一种方法,屡试不爽
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv(只有opencv3.2.1)
树莓派openVINO安装
https://docs.openvinotoolkit.org/latest/openvino_docs_install_guides_installing_openvino_raspbian.html
树莓派I2c使用
i2c:
https://blog.csdn.net/thystar/article/details/53021992
# coding=utf-8
#-*-coding:UTF-8-*-
import time
import smbus
import serial
import RPi.GPIO as GPIO
Temp_addr = 0x5a
reg = 0x07
bus = smbus.SMBus(1)
data = bus.read_word_data(Temp_addr, reg)
树莓派与Arduino串口通讯
raspi-arduino:
https://blog.csdn.net/a_52hz/article/details/92197048
树莓派端:
# coding=utf-8
#-*-coding:UTF-8-*-
import serial #导入serial库
ser = serial.Serial('/dev/ttyACM0', 9600,timeout=0.05); #打开端口,每0.05秒返回一个消息
def read_arduino(inf):
inf = 'T'
ser.write(inf.encode())#写T字符
response = ser.readall()#用response读取端口的返回值
print(response.decode())
while True:
result = read_arduino('T')
Arduino端:
下面展示一些 内联代码片
。
void setup(){
Serial.begin(9600); // 9600波特率
while (!Serial);
}
void loop() {
// read all the sensor values
// check if a proximity reading is available
if ( Serial.available())//判断串口缓存区有没有数据
{
if('T' == Serial.read())//有数据就用read来读取并判断是不是s
{
Serial.write("HELLO");
}
}
}
树莓派GPIO-Python使用
# coding=utf-8
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
KEY = 18
GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP)
while True:
if(GPIO.input(KEY)==1):
print("key press")
树莓派Opencv 口罩检测+人脸识别
# coding=utf-8
# import the necessary packages
import time
import cv2 as cv
import numpy as np
# 摄像头
cap = cv.VideoCapture(0)
# 创建检测器
face_recognizer = cv.face.createLBPHFaceRecognizer()
# 加载 model
face_recognizer.load('fm_model.xml')
nose_cascade=cv.CascadeClassifier('model/haarcascade_mcs_nose.xml')
face_cascade=cv.CascadeClassifier('model/haarcascade_frontalface_default.xml')
eye_cascade=cv.CascadeClassifier('model/haarcascade_eye.xml')
# 画框
def draw_rectangle(img, rect):
for x,y,w,h in rect:
cv.rectangle(img, (x, y), (x+w, y+h), (128, 128, 0), 2)
# 写文字
def draw_text(img, text, rect):
for x,y,w,h in rect:
cv.putText(img, text, (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
# 用户列表
def user_list():
label = 0
while True:
name = face_recognizer.getLabelInfo(label)
if len(name) != 0:
print('label:', label, 'name:', name)
else:
name = input("inoutID:")
face_recognizer.setLabelInfo(label, name)
return label
label += 1
#return None
def cut_face(image):
results=face_cascade.detectMultiScale(gray,1.1,5)
if results != ():
for x,y,w,h in results:
return results,gray[y:y+h, x:x+w]
else:
return [[0, 0, 0, 0]], None
# 口罩判别
def Mask_detection(image,rect):
nose = nose_cascade.detectMultiScale(gray,1.3,5)
#eye = eye_cascade.detectMultiScale(gray,1.1,5)
#mouth = mouth_cascade.detectMultiScale(gray,1.3,1)
#print(mouth)
#判断是否带口罩
if nose!=() and rect[0][0]+rect[0][2] != 0:
return "NO"
elif rect[0][0]+rect[0][2] != 0:
return "YES"
return '**'
# 此函数识别传递的图像中的人物并在检测到的脸部周围绘制一个矩形及其名称
# 人脸识别 预测
def predict(image):
if image is not None:
#预测人脸
results = face_recognizer.predict(image)
print(results[1])
#置信度阈值
if results[1] < 50:
label_text = face_recognizer.getLabelInfo(results[0])
else:
label_text = 'stranger'
return label_text
else:
#print eye
return 'not whole face'
train_faces = []
train_labels = []
num = -1
train_face_num = 15
while( cap.isOpened() ):
# USB摄像头工作时,读取一帧图像
ret, image = cap.read()
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
rect,cut_img = cut_face(gray)
# 预测 返回人脸标签
label_text = predict(cut_img)
# 检测 返回是否带口罩
label_mask = Mask_detection(gray,rect)
# 输出人脸框 及相关的文字信息
draw_rectangle(image, rect)
draw_text(image, "mask:"+label_mask, [[10,40,10,10]])
draw_text(image, label_text, rect)
key = cv.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if num > -1 and num < train_face_num :
rect,cut_img=cut_face(gray)
if cut_img is not None:
train_faces.append(cut_img)
train_labels.append(train_label)
num=num+1
draw_text(image, "condition:"+str(train_face_num-num), [[10,100,10,10]])
elif num >= train_face_num:
face_recognizer.update(train_faces, np.array(train_labels))
face_recognizer.save('fm_model3.0.xml')
train_faces = []
train_labels = []
num = -1
cv.imshow("Frame", image)
if key == ord("q"):
break
elif key == ord("s"):
if num == -1:
train_label = user_list()
num=0
# 释放资源和关闭窗口
cap.release()
cv.destroyAllWindows()
创建模型
#coding=utf-8
import time
import cv2 as cv
face_recognizer = cv.face.createLBPHFaceRecognizer()
#name = face_recognizer.getLabelInfo(label)
face_recognizer.save('fm_model.xml')
第一次创建空模型后,不能直接调用预测,把源码的预测部分注释掉,再运行源码训练一下更新模型,下一次就可以使用预测