class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() def build_camera(self): # face_cascade = cv2.CascadeClassifier('E:\openCV\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml') name_list = read_name_list('D:\myProject\pictures\dataset') #打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (64, 64), interpolation=cv2.INTER_LINEAR) label,prob = self.model.predict(ROI) #利用模型对cv2识别出的人脸进行比对 if prob >0.95: #如果模型认为概率高于95%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) #在人脸区域画一个正方形出来 cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
class Camera_reader(object): #Load model def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): face_cascade = cv2.CascadeClassifier('D:\proj\opencv-master\opencv-master\data\haarcascades\haarcascade_frontalface_alt.xml') #Read the subfolder in dataset folder name_list = read_name_list('D:\proj\dataset') cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label,prob = self.model.predict(ROI) if prob >0.7: #Only output higher than 70% label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
class Camera_reader(object): def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') name_list = read_name_list("C:\\Users\\jimmychen\\Desktop\\chernger\\chernger_faceRecognition\\dataset") cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label,prob = self.model.predict(ROI) if prob > 0.7: show_name = name_list[label] cv2.putText(frame, show_name + "{}".format(prob), (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) else: show_name = 'Stranger' cv2.putText(frame, show_name + "{}".format(prob), (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) frame = cv2.flip(frame, 1) cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
def test_onePicture(path): model = Model() model.load() color = (0, 255, 0) img1 = cv2.imread(path) img = cv2.resize(img1, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) classfier = cv2.CascadeClassifier( "C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml" ) faceRects = classfier.detectMultiScale(img1, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) if len(faceRects) > 0: # 大于0则检测到人脸 for faceRect in faceRects: # 单独框出每一张人脸 x, y, w, h = faceRect cv2.rectangle(img1, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 3) # 5控制绿色框的粗细 picType, prob = model.predict(img) if picType != -1: name_list = read_name_list('dataset') info = name_list[picType].split('_') infoName = 'Name:' + info[0] infoAge = 'Age:' + info[1] infoUniversity = 'University:' + info[2] infoSex = 'Sex:' + info[3] infoDrivingLN = 'Driving number:' + info[4] cv2.rectangle(img1, (0, 0), (300, 255), color, 3) # 5控制绿色框的粗细 cv2.putText(img1, "Driver Info", (80, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示性别 cv2.putText(img1, infoDrivingLN, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示驾驶证号码 cv2.putText(img1, infoName, (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示名字 cv2.putText(img1, infoAge, (0, 140), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示年龄 cv2.putText(img1, infoUniversity, (0, 180), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示学历 cv2.putText(img1, infoSex, (0, 220), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示性别 cv2.imshow("Camera", img1) # cv2.imshow(name_list[picType],img1) cv2.waitKey(0) print(name_list[picType], prob) else: cv2.imshow(" Don't know this person", img1) cv2.waitKey(0) print(" Don't know this person") else: cv2.imshow("No Face in this picture !!!", img1) cv2.waitKey(0) print("No Face in this picture")
def test_onePicture(path): model = Model() model.load() img = cv2.imread(path) img = cv2.resize(img, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: name_list = read_name_list(r'E:\cut-face') print(name_list[picType], prob) else: print("Don't know this person")
def test_onePicture(path): model = Model() model.load() img = cv2.imread(path) img = cv2.resize(img, (64, 64)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: name_list = read_name_list('D:\myProject\pictures\dataset') print name_list[picType], prob else: print " Don't know this person"
def test_onePicture(path): model= Model() model.load() img = cv2.imread(path) img = cv2.resize(img, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType,prob = model.predict(img) if picType != -1: name_list = read_name_list('/Users/gaoxingyun/Documents/uw/courses/Sp19/EE576_CV/project/realtime_emotion_recognition/dataset') print (name_list[picType],prob) else: print (" Don't know this person")
class Camera_reader(object): # 在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml') # 读取dataset数据集下的子文件夹名称,即标签名 name_list = read_name_list('./dataset') # 打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture('recognize_faces2.mp4') success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() # 图像灰度化 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 检测人脸:scaleFactor是放大比例(此参数必须大于1);minNeighbors是重复识别次数(此参数用来调整精确度,越大则越精确) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=8) for (x, y, w, h) in faces: origin = gray[x:x + w, y:y + h] # origin是原图,self.img_size是输出图像的尺寸,interpolation是插值方法 origin = cv2.resize(origin, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) # 利用模型对cv2识别出的人脸进行比对 label, prob = self.model.predict(origin) # 如果模型认为概率高于70%则显示为模型中已有的label if prob > 0.7: show_name = name_list[label] else: show_name = 'unknown' # 框出人脸 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 1) # 显示人名 cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) cv2.imshow("Recognizing...", frame) # 释放摄像头 cameraCapture.release() # 关闭所有窗口 cv2.destroyAllWindows()
def test_onBatch(path): model = Model() model.load() index = 0 img_list, label_lsit, counter = read_file(path) for img in img_list: picType, prob = model.predict(img) if picType != -1: index += 1 print(labbel[picType], prob) else: print("invaild person") return index
def test_onePicture(path): model = Model() model.load() img = cv2.imread(path) img = cv2.resize(img, (64, 64)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: name_list = read_name_list( 'C:\\Users\\jimmychen\\Desktop\\chernger\\chernger_faceRecognition\\dataset' ) print(name_list[picType], prob) else: print(" Don't know this person")
def test_onBatch(path): model = Model() model.load() index = 0 img_list, label_lsit, counter = read_file(path) for img in img_list: picType, prob = model.predict(img) if picType != -1: index += 1 name_list = read_name_list('./image/trainfaces') print(name_list[picType]) else: print(" Don't know this person") return index
def test_onePicture(path): model = Model() model.load() img = cv2.imread(path) img = cv2.resize(img, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: # name_list = read_name_list('D:\myProject\pictures\dataset') name_list = read_name_list( r'D:/my_laboratory/face_detection20180516/dataset') print(name_list[picType], prob) else: print(" Don't know this person")
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier( '/usr/local/lib/python2.7/site-packages/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_alt.xml' ) #读取dataset数据集下的子文件夹名称 name_list = read_name_list( '/home/hezhiqiang/PycharmProjects/pictures/dataset') #打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) nextCaptureTime = time.time() faces = [] if not cameraCapture.isOpened(): print('Capture failed because of camera') success, frame = cameraCapture.read() while 1: # while success and cv2.waitKey(1) == -1: # print '1' success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 # if nextCaptureTime < time.time(): # nextCaptureTime = time.time() + 0.1 faces = face_cascade.detectMultiScale(gray, 1.3, 5) #识别人脸 for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict(ROI) #利用模型对cv2识别出的人脸进行比对 if prob > 0.7: #如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) #在人脸区域画一个正方形出来 cv2.imshow("MyWindow", frame) if cv2.waitKey(1) & 0xFF == 27: break cameraCapture.release() cv2.destroyAllWindows()
def onePicture(path): model = Model() model.load() img = cv2.imread(path) img = cv2.resize(img, (92, 112)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: name_list = read_name_list('res') print(name_list) print(picType) print(name_list[picType], prob) print("done") else: print(" Don't know this person")
def test_onBatch(path): model= Model() model.load() index = 0 img_list, label_lsit, counter = read_file(path) for img in img_list: picType,prob = model.predict(img) if picType != -1: index += 1 name_list = read_name_list('/Users/gaoxingyun/Documents/uw/courses/Sp19/EE576_CV/project/faceRecognition/dataset') print (name_list[picType]) else: print (" Don't know this person") return index
def test_onBatch(path): model = Model() model.load() index = 0 img_list, label_lsit, counter = read_file(path) for img in img_list: picType, prob = model.predict(img) if picType != -1: index += 1 name_list = read_name_list('D:\myProject\pictures\dataset') print name_list[picType] else: print " Don't know this person" return index
def test_onePicture(path): model = Model() model.load() img = cv2.imread(path) #print(img) img = cv2.resize(img, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: #print(picType,prob) #name_list = read_name_list('../myAIlab/pictures/dataset') #print(name_list[picType],prob) print(labbel[picType], prob) else: print("invaild person")
def test_Pictures(path): model = Model() model.load() for child_dir in os.listdir(path): if endwith(child_dir, 'jpg'): # 找到以jpg结尾的文件路径 str = os.path.join(path, child_dir) # 具体的图片文件的路径 img = cv2.imread(str) resized_img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_CUBIC) img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) picType, prob = model.predict(img) if picType != -1: name_list = read_name_list('pictures/dataset') print(name_list[picType], prob) else: print(" Don't know this person")
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 classname = 0 titlename = "untitled (hzy_order) - Sublime Text (UNREGISTERED)" #获取句柄 self.hwnd = win32gui.FindWindow(classname, titlename) print("得到句柄") def build_camera(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸C:\\Users\\jan\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\ face_cascade = cv2.CascadeClassifier( 'C:\\Users\\jan\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml' ) #读取dataset数据集下的子文件夹名称 name_list = read_name_list('E:\\WWW\\faceRecognition\\dataset') #打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 faces = face_cascade.detectMultiScale(gray, 1.3, 5) #识别人脸 for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict(ROI) #利用模型对cv2识别出的人脸进行比对 if prob > 0.7: #如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] win32gui.SetForegroundWindow(self.hwnd) print("got you %s " % show_name) win32gui.ShowWindow(self.hwnd, win32con.SW_MAXIMIZE) else: show_name = 'Stranger' #cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 #frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) #在人脸区域画一个正方形出来 time.sleep(3) #cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier( 'C:\\Face recognition\\classifier\\haarcascade_frontalface_alt2.xml' ) #读取dataset数据集下的子文件夹名称 name_list = read_name_list('C:\\Face recognition\\data') #打开摄像头并开始读取画面 cap = cv2.VideoCapture(0) ret, frame = cap.read() #读取一帧视频 while ret and cv2.waitKey(10) == -1: ret, frame = cap.read() try: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 faces = face_cascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) #识别人脸,****** for (x, y, w, h) in faces: ROI = gray[y - 10:y + h + 10, x - 10:x + w + 10] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict(ROI) #利用模型对cv2识别出的人脸进行比对 if prob > 0.90: #如果模型认为概率高于95%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) #在人脸区域画一个正方形 cv2.imshow("Camera", frame) except: continue cap.release() cv2.destroyAllWindows() return show_name
class Camera_reader(object): # 在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier( r'C:\Users\01436179\PycharmProjects\pyhton_face\haarcascade_frontalface_alt.xml' ) # 读取dataset数据集下的子文件夹名称 name_list = read_name_list( r'C:\Users\01436179\PycharmProjects\pyhton_face\dataset') # 打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 图像灰化 faces = face_cascade.detectMultiScale(gray, 1.3, 5) # 识别人脸 for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict(ROI) # 利用模型对cv2识别出的人脸进行比对 if prob > 0.7: # 如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示名字 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # 在人脸区域画一个正方形出来 cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
def test_onBatch(path): model = Model() model.load() index = 0 img_list, label_lsit, counter = read_file(path) for img in img_list: picType, prob = model.predict(img) if picType != -1: index += 1 # name_list = read_name_list('D:\myProject\pictures\dataset') name_list = read_name_list( r'D:/my_laboratory/face_detection20180516/dataset') print(name_list[picType]) else: print(" Don't know this person") return index
def test_onePicture(path): # 加载模型 model = Model() model.load() # 读取图片 img = cv2.imread(path) # 重置图片尺寸为:128*128 img = cv2.resize(img, (128, 128)) # 图片灰度化 img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # labelIndex为概率最高的label的索引号, prob为对应概率 labelIndex, prob = model.predict(img) if labelIndex != -1: name_list = read_name_list('./dataset') print(name_list[labelIndex], prob) else: print("Don't know this person.")
def test_onBatch(path): model = Model() model.load() index = 0 img_list, label_lsit, counter = read_file(path) for img in img_list: picType, prob = model.predict(img) if picType != -1: index += 1 name_list = read_name_list( 'C:\\Users\\jimmychen\\Desktop\\chernger\\chernger_faceRecognition\\dataset' ) print(name_list[picType]) else: print("Don't know this person") return index
def test_onePicture(path): model= Model(read_save=True) model.load() img = cv2.imread(path) face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray,1.1,5) for (x, y, w, h) in faces: face = cv2.resize(gray[y:(y + h), x:(x + w)], (128, 128)) picType,prob = model.predict(face) print (picType) name_list = read_name_list('G:\Python/FaceRecognition\datasets') if picType != -1: # if picType>=0 and picType<len(name_list): print (name_list[picType],prob) else: print (" Don't know this person")
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 #face_cascade = cv2.CascadeClassifier('D:/anaconda3/envs/tensorflow-1.13.1/Lib/site-packages/cv2/data/haarcascade_frontalface_alt.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') #读取dataset数据集下的子文件夹名称 name_list = read_name_list('pictures\dataset') #打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() num = 1 while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 faces = face_cascade.detectMultiScale(gray, 1.1, 3) # 识别人脸 for (x, y, w, h) in faces: ROI = gray[y:(y + h), x:(x + w)] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) #cv2.imwrite("pictures/take_photos/p_" + str(num) + ".jpg", ROI) #num += 1............................................................... label, prob = self.model.predict(ROI) # 利用模型对cv2识别出的人脸进行比对 show_name = '' if prob > 0.9: # 如果模型认为概率高于88%则显示为模型中已有的label show_name = name_list[label] if show_name == 'zhouyicheng': #else: #show_name = '' cv2.putText(frame, show_name, (x + 11, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 1) #在人脸区域画一个正方形出来 cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
def test_onBatch(path): # 加载模型 model = Model() model.load() # 计数器 index = 0 # 读取所有图片;img_list是所有图片的集合,label_lsit是所有标签的集合,label_num是标签数量 img_list, label_lsit, label_num = read_file(path) for img in img_list: # labelIndex为概率最高的label的索引号, prob为对应概率 labelIndex, prob = model.predict(img) if labelIndex != -1: index += 1 name_list = read_name_list('./dataset') print(name_list[labelIndex]) else: print("Don't know this person.") return index
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model(read_save=True) self.model.load() self.img_size = 128 #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 self.face_cascade = cv2.CascadeClassifier( "haarcascade_frontalface_default.xml") #读取dataset数据集下的子文件夹名称 self.name_list = read_name_list(dataset_path) def build_camera(self): #打开摄像头并开始读取画面 # cv2.namedWindow("Face_Recognization") cameraCapture = cv2.VideoCapture(0) while cameraCapture.isOpened(): success, frame = cameraCapture.read() if success == True: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 faces = self.face_cascade.detectMultiScale(gray, 1.1, 5) #识别人脸 for (x, y, w, h) in faces: face_img = gray[x:x + w, y:y + h] face_img = cv2.resize(face_img, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict( face_img) #利用模型对cv2识别出的人脸进行比对 if prob > 0.7: #如果模型认为概率高于70%则显示为模型中已有的label show_name = self.name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) #显示名字 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) #在人脸区域画一个正方形出来 cv2.imshow("Face_Recognization", frame) key = cv2.waitKey(10) c = chr(key & 255) if c in ['q', 'Q', chr(27)]: #chr(27)是Esc键 break cameraCapture.release() cv2.destroyAllWindows()
def test_onBatch(path): model = Model() model.load() index = 0 img_list, label_list, counter = read_file(path) # img_list = img_list.reshape(img_list.shape[0], 174, 212, 1) # print(img_list.shape[0:]) # img_list = img_list.astype('float32')/255 # Label_list = np_utils.to_categorical(label_list, num_classes=counter) for img in img_list: picType, prob = model.predict(img) if picType != -1: index += 1 name_list = read_name_list('G:/desktop/myProject/pictures/test') print(name_list) print(name_list[picType]) else: print(" Don't know this person") return index
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 # face_cascade = cv2.CascadeClassifier('E:\openCV\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml') # face_cascade = cv2.CascadeClassifier('D:\Program Files(x86)\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') #读取dataset数据集下的子文件夹名称 # name_list = read_name_list('D:\myProject\pictures\dataset') name_list = read_name_list(r'D:/my_laboratory/face_detection20180516/dataset') #打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 faces = face_cascade.detectMultiScale(gray, 1.3, 5) #识别人脸 for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3) # 在人脸区域画一个正方形出来 label,prob = self.model.predict(ROI) #利用模型对cv2识别出的人脸进行比对 if prob >0.1: #如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 # frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) #在人脸区域画一个正方形出来 # cv2.rectangle(img, (x2, x1), (y2, y1), (255, 0, 0), 3) cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()