| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 
 | import cv2 as cvfrom mysqlUtil import Connector
 
 import numpy as np
 
 recognizer = cv.face.LBPHFaceRecognizer.create()
 recognizer.read('../face_detect.yml')
 con = Connector()
 
 
 class Face_Recognition:
 
 def face_detect_demo(self,image):
 
 gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
 
 face_detect = cv.CascadeClassifier("D:/app/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml")
 faces = face_detect.detectMultiScale(gray, 1.02, 5)
 img_numpy = np.array(gray, 'uint8')
 for x, y, w, h in faces:
 cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
 id, confidence = recognizer.predict(img_numpy[y:y + h, x:x + w])
 print(id, confidence)
 if confidence < 100:
 print("用户id为:", id)
 print("用户置信度为:", confidence)
 name = con.getUserNameById(id)
 print("用户名:", name)
 cv.putText(image, name, (x + 10, y - 10), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 else:
 print("unknown")
 cv.putText(image, 'unknown', (x + 10, y - 10), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 cv.imshow('image', image)
 
 
 if __name__ == '__main__':
 
 
 
 image = cv.imread('../resource/fanfan_test.jpg')
 
 
 
 image = cv.resize(image, (0, 0), fx=0.25, fy=0.25)
 Face_Recognition().face_detect_demo(image)
 cv.waitKey(0)
 
 
 |