700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 机器视觉(三)树莓派自制人脸识别考勤机

机器视觉(三)树莓派自制人脸识别考勤机

时间:2020-11-20 01:05:56

相关推荐

机器视觉(三)树莓派自制人脸识别考勤机

一、硬件准备

1,树莓派3B

2,pi camera

3,工具:键盘、鼠标、显示器、SD卡读写器

二、SD卡烧录操作系统

1,U盘格式化软件SDFormatterv4

2,系统烧写工具Win32DiskImager

3,系统镜像文件ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi.img

三、软件系统安装

1,测试相机

打开摄像头,预览几秒后保存一张图片。

#新建终端raspistill -o mytest.jpg

或者终端输入:返回检测到的相机数量。

vcgencmd get_camera

2,python3安装

参考CSDN教程安装,完成后查看版本号:

python3 -V

3,opencv安装

sudo apt-get install libopencv-devsudo apt-get install python-opencv

完成后查看版本号:

pkg-config opencv --modversion

在python中添加cv2,并查看版本号:

pythonimport cv2cv2.__version__

四、源码编写

1,人脸检测

import numpy as npimport cv2# multiple cascades: /Itseez/opencv/tree/master/data/haarcascadesfaceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')cap = cv2.VideoCapture(0)cap.set(3,640) # set Widthcap.set(4,480) # set Heightwhile True:ret, img = cap.read()img = cv2.flip(img, -1)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(20, 20))for (x,y,w,h) in faces:cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)roi_gray = gray[y:y+h, x:x+w]roi_color = img[y:y+h, x:x+w]cv2.imshow('video',img)k = cv2.waitKey(30) & 0xffif k == 27: # press 'ESC' to quitbreakcap.release()cv2.destroyAllWindows()

2,人脸识别

数据库采集:

import cv2import oscam = cv2.VideoCapture(0)cam.set(3, 640) # set video widthcam.set(4, 480) # set video heightface_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')# For each person, enter one numeric face idface_id = input('\n enter user id end press <return> ==> ')print("\n [INFO] Initializing face capture. Look the camera and wait ...")# Initialize individual sampling face countcount = 0while(True):ret, img = cam.read()img = cv2.flip(img, -1) # flip video image verticallygray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = face_detector.detectMultiScale(gray, 1.3, 5)for (x,y,w,h) in faces:cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)count += 1# Save the captured image into the datasets foldercv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])cv2.imshow('image', img)k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting videoif k == 27:breakelif count >= 30: # Take 30 face sample and stop videobreak# Do a bit of cleanupprint("\n [INFO] Exiting Program and cleanup stuff")cam.release()cv2.destroyAllWindows()

训练模型:

pip install opencv-contrib-python

import cv2import numpy as npfrom PIL import Imageimport os# Path for face image databasepath = 'dataset'recognizer = cv2.face.LBPHFaceRecognizer_create()detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");# function to get the images and label datadef getImagesAndLabels(path):imagePaths = [os.path.join(path,f) for f in os.listdir(path)]faceSamples=[]ids = []for imagePath in imagePaths:PIL_img = Image.open(imagePath).convert('L') # convert it to grayscaleimg_numpy = np.array(PIL_img,'uint8')id = int(os.path.split(imagePath)[-1].split(".")[1])faces = detector.detectMultiScale(img_numpy)for (x,y,w,h) in faces:faceSamples.append(img_numpy[y:y+h,x:x+w])ids.append(id)return faceSamples,idsprint ("\n [INFO] Training faces. It will take a few seconds. Wait ...")faces,ids = getImagesAndLabels(path)recognizer.train(faces, np.array(ids))# Save the model into trainer/trainer.ymlrecognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi# Print the numer of faces trained and end programprint("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))

后台开发:使用IDEA软件开发后台,接收树莓派上传的数据存储在mySQL数据库。

五、成果展示

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。