分享一个简易的Python OpenCV人脸识别程序
# 人脸采集与训练#### 文件结构
![文件结构图](https://picdl.sunbangyan.cn/2023/11/15/f61f5cad81d3bf379f22605c3d004e92.png)
### face_train.py 人脸识别和训练
```Python
import os
import cv2
import numpy as np
import pickle
current_id = 0
label_ids = {}
x_train = []
y_labels = []
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")
cap = cv2.VideoCapture(0)
recognizer = cv2.face_LBPHFaceRecognizer.create()
classifier = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
count = 0
while True:
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = classifier.detectMultiScale(gray, 1.3, 5)
# 画一个脸部矩形
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), 2)
count += 1
cv2.imwrite("./images/XXX/face" + str(count) + ".jpg", gray)
cv2.imshow('frame', frame)
cv2.imshow('freame_with_gray', gray)
if cv2.waitKey(1) & 0xFF == ord('q') or count == 100:
break
cap.release()
cv2.destroyAllWindows()
# 开始训练
for root, dirs, files in os.walk(image_dir):
for file in files:
path = os.path.join(root, file)
# print(path)
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_arr = np.array(gray, "uint8")
label = os.path.basename(root)
if label not in label_ids:
label_ids = current_id
current_id += 1
id_ = label_ids
faces = classifier.detectMultiScale(image_arr,1.5,5)
for x,y,w,h in faces:
rol = image_arr
x_train.append(rol)
y_labels.append(id_)
with open("label.pickle", "wb") as file:
pickle.dump(label_ids, file)
print(label_ids)
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.xml")
```
### face_recognition.py 人脸识别程序
```Python
import cv2
import numpy as np
import pickle
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
recognizer = cv2.face_LBPHFaceRecognizer.create()
recognizer.read("trainner.xml")
labels = {}
with open('label.pickle', 'rb') as file:
origin_labels = pickle.load(file)
labels = {v: k for k, v in origin_labels.items()}
while True:
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# 画一个脸部矩形
for (x, y, w, h) in faces:
gray_roi = gray
id_,conf = recognizer.predict(gray_roi)
if conf>=50:
print(labels)
cv2.putText(frame, labels, (x, y), cv2.FONT_HERSHEY_PLAIN,1.0, (0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), 2)
cv2.namedWindow('result', cv2.WINDOW_FULLSCREEN)
cv2.imshow('result', frame)
# cv2.imshow('freame_with_gray', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
``` recognizer.train(x_train, np.array(y_labels))
cv2.error: OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp:362: error: (-210:Unsupported format or combination of formats) Empty training data was given. You'll need more than one sample to learn a model. in function 'cv::face::LBPH::train'
报错,请问您用的Python及opencv版本是 EXE????????? python菜鸟看懵逼了 这个是一张一张添加代码? {:1_921:}打包成GUI运行就更好了 有没有带UI的版本? netpeng 发表于 2023-11-15 23:51
有没有带UI的版本?
暂时还没有:lol wsxb 发表于 2023-11-15 22:06
EXE?????????
不是哦,暂时还没有打包成gui aigc 发表于 2023-11-15 23:10
打包成GUI运行就更好了
感谢支持,我继续努力🙏 感谢分享 学习下,测试下看看。感谢分享。