-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__testing__.py
66 lines (53 loc) · 2.08 KB
/
__testing__.py
1
2
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# @Time : 2018/10/19
# @Author : Haldate
# 此文件用于读取分类器,并进行预测评估测试
import os
import cv2 as cv
import numpy as np
#import matplotlib.pyplot as plt
# 设置分类器文件路径
recognizer_Path = "C:/Users/lxdn/PycharmProjects/untitled/"
cascPath = "F:/Python37-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml"
faceCascade = cv.CascadeClassifier(cascPath)
personNames = np.load("C:/Users/lxdn/PycharmProjects/untitled/nameList.npy")
def getPersonName(id, conf):
return personNames[id]
recognizer = "MyFacePCAModel.xml"
recognizer1="MyFaceLDAModel.xml"
classifier = cv.face.EigenFaceRecognizer_create()
classifier.read(recognizer_Path + recognizer)
classifier1=cv.face.FisherFaceRecognizer_create()
classifier1.read(recognizer_Path + recognizer1)
# 调用摄像头
camera = cv.VideoCapture(0)
# 设置文字
# font = cv.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
font = cv.FONT_HERSHEY_SIMPLEX
while True:
# 从中读取图像
rect, image = camera.read()
image = cv.flip(image,1)
# 将读取出的图像转化为灰度图
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,1.3,5)
for (x, y, w, h) in faces:
cv.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
videoGray = cv.resize(gray[y:y + h,x:x + w], (200, 200))
imageId, conf = classifier.predict(videoGray)
imageId1,conf1=classifier1.predict(videoGray)
personName = getPersonName(imageId, conf)
personName1=getPersonName(imageId1,conf1)
print("Eigenfaces")
print(imageId,conf)
print(personName)
print("FisherFace")
print(imageId1,conf1)
print(personName1)
cv.putText(image, personName, (x,y-20),font, 1, (0, 255, 0), 2)
cv.putText(image, personName1, (x, y-80), font, 1, (255, 0, 0), 2)
cv.imshow('camera', image)
if cv.waitKey(10) & 0xff == ord('q'):
break
camera.release()
cv.destroyAllWindows()
print("Test Done")