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 build_camera(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier( 'E:\openCV\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml' ) #读取dataset数据集下的子文件夹名称 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, (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 __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 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")
def __init__(self): self.model = Model() self.model.load() self.img_size = 128 print("init") # 摄像头初始化 加快使用速度 self.face_cascade = cv2.CascadeClassifier( './haarcascade_frontalface_alt.xml') # 读取dataset数据集下的子文件夹名称 self.name_list = read_name_list('res') # 打开摄像头并开始读取画面 self.cameraCapture = cv2.VideoCapture(0)
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, (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_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")
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_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")
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")
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_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 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 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
def test_onBatch(path): model= Model(read_save=True) model.load() index = 0 img_list,img_name_list = readAllImg(path,'.jpg') face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") for img in img_list: 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) name_list = read_name_list('G:\Python/FaceRecognition\datasets') if picType != -1: # if picType>=0 and picType<len(name_list): print (img_name_list[index],":",name_list[picType],",",prob) index += 1 else: print (" Don't know this person") return index
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 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
def build_camera(self): hight = 0 color = (0, 255, 0) #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier( 'C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml' ) #打开摄像头并开始读取画面 cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() name_list = read_name_list('dataset') count = 0 while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() count += 1 if count < 100: cv2.imwrite("images/yjd_sc/" + str(count) + ".jpg", frame) elif count == 200: #将摄像头采集的钱200张图像当做原始图片进行实时建模和模型的训练 cv2.imwrite("images/yjd_sc/" + str(count) + ".jpg", frame) dealImg('images\\yjd_sc', 'images\\yjd_deal') readPicSaveFace('images\\yjd_deal', 'dataset\\yjd_21_NEU_boy_001231', '.jpg', '.JPG', 'png', 'PNG') dataset = DataSet("dataset") self.model.read_trainData(dataset) self.model.build_model() self.model.train_model() self.model.evaluate_model() self.model.save() else: #将其实时训练好的模型应用于之后检测的图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #图像灰化 faces = face_cascade.detectMultiScale(gray, 1.3, 5) #识别人脸 cv2.putText(frame, "Driver Info", (80, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示信息标题 if len(faces) == 0: cv2.rectangle(frame, (0, 0), (300, 100), color, 3) # 5控制绿色框的粗细 cv2.putText(frame, "No people !!!", (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 else: 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' # 当识别出来的不是陌生人时,需要显示出司机信息 if show_name != "Stranger": info = show_name.split('_') infoName = 'Name:' + info[0] infoAge = 'Age:' + info[1] infoUniversity = 'University:' + info[2] infoSex = 'Sex:' + info[3] infoDrivingLN = 'DriverNumber:' + info[4] cv2.rectangle(frame, (0, 0), (280, len(faces) * 220), color, 3) # 5控制绿色框的粗细 cv2.putText(frame, info[0], (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示姓名信息 cv2.putText(frame, infoDrivingLN, (0, hight + 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示驾驶证号码 cv2.putText(frame, infoName, (0, hight + 95), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示名字 cv2.putText(frame, infoAge, (0, hight + 130), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示年龄 cv2.putText(frame, infoUniversity, (0, hight + 165), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示学历 cv2.putText(frame, infoSex, (0, hight + 200), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示性别 else: cv2.rectangle(frame, (0, 0), (300, 100), color, 3) # 5控制绿色框的粗细 cv2.putText(frame, "Stranger", (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 cv2.putText(frame, "It is a Stranger", (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) # 在人脸区域画一个正方形出来 cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
except: continue gray_face = preprocess_input(gray_face, False) gray_face = np.expand_dims(gray_face, 0) gray_face = np.expand_dims(gray_face, -1) emotion_prediction = emotion_classifier.predict(gray_face) # emotion_probability = np.max(emotion_prediction) emotion_label_arg = np.argmax(emotion_prediction,axis=1) emotion_text = class_names[int(emotion_label_arg)] picType,prob = face_model.predict(face_recog) 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) face_text = name_list[picType] else: print (" Don't know this person") face_text = 'unknown' color = (0,255,0) draw_bounding_box(face_coordinates, rgb_image, color) draw_text(face_coordinates, rgb_image, emotion_text, color, 0, 45, 1, 1) draw_text(face_coordinates, rgb_image, face_text, color, 0, -45, 1, 1) bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) cv2.imshow('window_frame', bgr_image)
while success and cv2.waitKey(1) == -1: success, frame = cam.read() det = cv2.resize(frame, (img_size, img_size), interpolation=cv2.INTER_LINEAR) # det = image.array_to_img(det) label, prob = predict(model, det) if prob > 0.9: show_name = name_list[label] else: show_name = 'Unknown' cv2.putText(frame, show_name, (20, img_size - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # frame = cv2.rectangle(frame, (y, x), (h, w), (255, 0, 0), 2) cv2.imshow("Camera", frame) cam.release() cv2.destroyAllWindows() if __name__ == '__main__': img_size = 224 name_list = read_name_list('A:\\python_work\\data') # num_classes = len(name_list) model = load_model('model\\model.h5') main()
def test_onePicture2(): global image_file, im, image_label path = ent.get() if path == "": path = "TwoF.jpg" t.delete(0.0, END) t.insert('insert', " Driver info(left - > right)\n\n") model = Model() model.load() color = (0, 255, 0) name_list = read_name_list('dataset') img1 = cv2.imread("images\\" + path) gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) # 图像灰化 face_cascade = cv2.CascadeClassifier( 'C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml' ) faceRects = face_cascade.detectMultiScale(gray, 1.2, 3, minSize=(32, 32)) # 识别人脸 hight = 0 if len(faceRects) > 0: # 大于0则检测到人脸 for (x, y, w, h) in faceRects: # 单独框出每一张人脸 hight = hight + 1 ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (128, 128), interpolation=cv2.INTER_LINEAR) label, prob = model.predict(ROI) # 利用模型对cv2识别出的人脸进行比对 if prob > 0.4: # 如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' img1 = cv2.rectangle(img1, (x, y), (x + w, y + h), color, 2) # 在人脸区域画一个正方形出来 if show_name != "Stranger": info = show_name.split('_') cv2.putText(img1, info[0], (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 infoName = 'Name: ' + info[0] infoAge = 'Age: ' + info[1] infoUniversity = 'University: ' + info[2] infoSex = 'Sex: ' + info[3] infoDrivingLN = 'DriverNumber: ' + info[4] t.insert( 'insert', " driver" + str(hight) + " " + infoDrivingLN + "\n") t.insert('insert', " " + infoName + "\n") t.insert('insert', " " + infoAge + "\n") t.insert( 'insert', " " + infoUniversity + "\n") t.insert('insert', " " + infoSex + "\n\n") else: cv2.putText(img1, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 t.insert('insert', "It is a Stranger!!!") cv2.imwrite("images/result.jpg", img1) image_file = Image.open("images/result.jpg") im = ImageTk.PhotoImage(image_file) image_label = Label(image_frame, image=im) image_label.grid(row=4, column=0, pady=20, padx=30) else: image_file = Image.open("images/" + path) im = ImageTk.PhotoImage(image_file) image_label = Label(image_frame, image=im) image_label.grid(row=4, column=0, pady=20, padx=30) t.insert('insert', "No driver in this picture !!!")
def build_camera(self): hight = 0 color = (0, 255, 0) #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier('C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml') #读取dataset数据集下的子文件夹名称 name_list = read_name_list('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) #识别人脸 cv2.putText(frame, "Driver Info", (80, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2, 2) # 显示信息标题 if len(faces) == 0: cv2.rectangle(frame, (0, 0), (300, 100), color, 3) # 5控制绿色框的粗细 cv2.putText(frame, "No people !!!", (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 else: 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' # 当识别出来的不是陌生人时,需要显示出司机信息 if show_name != "Stranger": info = show_name.split('_') infoName = 'Name:' + info[0] infoAge = 'Age:' + info[1] infoUniversity = 'University:' + info[2] infoSex = 'Sex:' + info[3] infoDrivingLN = 'DriverNumber:' + info[4] cv2.rectangle(frame, (0, 0), (280, len(faces) * 220), color, 3) # 5控制绿色框的粗细 cv2.putText(frame, info[0], (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示姓名信息 cv2.putText(frame, infoDrivingLN, (0, hight + 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示驾驶证号码 cv2.putText(frame, infoName, (0, hight + 95), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示名字 cv2.putText(frame, infoAge, (0, hight + 130), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示年龄 cv2.putText(frame, infoUniversity, (0, hight + 165), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示学历 cv2.putText(frame, infoSex, (0, hight + 200), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, 3) # 显示性别 else: cv2.rectangle(frame, (0, 0), (300, 100), color, 3) # 5控制绿色框的粗细 cv2.putText(frame, "Stranger", (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 cv2.putText(frame, "It is a Stranger", (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示陌生人信息 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) # 在人脸区域画一个正方形出来 cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows()
# -*- coding: utf-8 -*- import face_recognition import cv2 from read_data import read_name_list from read_data import read_file video_capture = cv2.VideoCapture(0) #读取摄像头,并识别摄像头中的人脸,进行匹配。 all_encoding, lable_list, counter = read_file('./dataset') name_list = read_name_list('./dataset') face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) if process_this_frame: face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings( small_frame, face_locations) face_names = [] #匹配,并赋值 for face_encoding in face_encodings: i = 0 j = 0 for t in all_encoding: