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('/root/dataset') print name_list[picType] else: print " Don't know this person" return index
def 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('F:\myProject\pictures\dataset') print(name_list) print(name_list[picType]) else: print(" Don't know this person") return index
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_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 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()
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()
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
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_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
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(700) 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是所有标签的集合,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
def Batch(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('./res') print("done") print(name_list) print(picType) print(name_list[picType]) 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()
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
class img_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_img(self): #opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 face_cascade = cv2.CascadeClassifier( 'C:\pylearning\ml&dl\\face_detect_v0\config\haarcascade_frontalface_alt.xml' ) #读取dataset数据集下的子文件夹名称 name_list = read_name_list('face') img = cv2.imread('test.jpg') #打开摄像头并开始读取画面 gray = cv2.cvtColor(img, 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.9: #如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' cv2.putText(img, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字 img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) #在人脸区域画一个正方形出来 cv2.imshow("face", img) cv2.waitKey(0)
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 !!!")
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load() self.img_size = 128 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()
gray_face = cv2.resize(gray_face, (emotion_target_size)) face_recog = cv2.resize(face_recog, (face_target_size)) 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)
class Camera_reader(object): #在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() #self.model.load() self.img_size = 128 if os.path.exists("images\\yjd_sc"): shutil.rmtree("images\\yjd_sc") if os.path.exists("images\\yjd_deal"): shutil.rmtree("images\\yjd_deal") if os.path.exists("dataset\\yjd_21_NEU_boy_001231"): shutil.rmtree("dataset\\yjd_21_NEU_boy_001231") os.mkdir("images\\yjd_sc") os.mkdir("images\\yjd_deal") os.mkdir("dataset\\yjd_21_NEU_boy_001231") 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()
class Camera_reader(object): # Build the model when initializing the camera and load the trained model def __init__(self): self.model = Model() self.model.load() self.img_size = 128 def build_camera(self): # The location of the face cascade file in the opencv file, used to help identify faces in images or video streams face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml') # Read the subfolder name under the dataset name_list = read_name_list('./dataset') # Turn on the camera and start reading cameraCapture = cv2.VideoCapture(700) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read() # Grayscale image gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Face detection: scaleFactor is #the magnification ratio (this parameter must be greater than 1); #minNeighbors is the number of repeated identifications (this parameter is used to adjust the accuracy, the larger the more accurate) 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 is the original image, self.img_size is the size of the output image, and interpolation is the interpolation method origin = cv2.resize(origin, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) # Use the model to compare the faces recognized by cv2 label, prob = self.model.predict(origin) # If the model believes that the probability is higher than 70%, it will be displayed as the existing label in the model if prob > 0.7: show_name = name_list[label] else: show_name = 'unknown' # Frame a face frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 1) if prob > 0.7: #storing recognized folder in folder named with date of that day today = datetime.now() recpath = './Recognized/' + today.strftime("%d-%m-%Y") if not os.path.exists(recpath): os.makedirs(recpath) #print(os.getcwd()) cv2.imwrite( recpath + '/' + today.strftime("%H%M%S") + '.jpg', frame[y:y + h, x:x + w]) #if not os.path.exists() # Show name cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) cv2.imshow("Recognizing...", frame) # Release camera cameraCapture.release() # Close all windows cv2.destroyAllWindows()
class Camera_reader(object): # 在初始化camera的时候建立模型,并加载已经训练好的模型 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 build_camera(self): # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 success, frame = self.cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = self.cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 图像灰化 faces = self.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, (92, 112), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict(ROI) # 利用模型对cv2识别出的人脸进行比对 if prob > 0.9: # 如果模型认为概率高于70%则显示为模型中已有的label show_name = self.name_list[label] print("get a face %s " % show_name) data = { "key": "7b97e1bed4d6a452ab5bc68b9fc1e681", "id": 1, "status": "open" } response = requests.get( "http://kuailezhai.cn/update/?key=7b97e1bed4d6a452ab5bc68b9fc1e681&id=1&status=open" ) print(response.text) print("是管理员,允许开门") 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) #self.cameraCapture.release() cv2.destroyAllWindows() def get_one_picture(self): success, frame = self.cameraCapture.read() if success: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 图像灰化 faces = self.face_cascade.detectMultiScale(gray, 1.3, 5) # 识别人脸 # 找不到人脸的情况 cv2.imwrite('./picture' + '.jpg', frame) if len(faces) == 0: print("can not find a face ") return False # 找到人脸 else: for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h] ROI = cv2.resize(ROI, (92, 112), interpolation=cv2.INTER_LINEAR) label, prob = self.model.predict(ROI) # 利用模型对cv2识别出的人脸进行比对 if prob > 0.9: # 如果模型认为概率高于70%则显示为模型中已有的label show_name = self.name_list[label] print("get a face %s " % show_name) return True else: show_name = 'Stranger' print("stranger") return False return False def camera_done(self): self.cameraCapture.release() cv2.destroyAllWindows()
small_gray = cv2.cvtColor(small_image, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 检测人脸,探测图片中的人脸 faces = face_engine.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=15) # 读取dataset数据集下的子文件夹名称 name_list = read_name_list('./dataset') # 框出人脸 for (x, y, w, h) in faces: origin = gray[x:x + w, y:y + h] # origin是原图,self.img_size是输出图像的尺寸,interpolation是插值方法 origin = cv2.resize(origin, (128, 128), interpolation=cv2.INTER_LINEAR) # 利用模型对cv2识别出的人脸进行比对 # 第一个返回值为概率最高的label的index,第二个返回值为对应概率 labelIndex, prob = model.predict(origin) # 如果模型认为概率高于70%则显示为模型中已有的label if prob > 0.7: show_name = name_list[labelIndex] else: show_name = 'unknown' # 1.原始图片 2.人脸坐标原点 3.标记的高度 4,线的颜色 5,线宽 frame = cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 1) # 显示人名 cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) # 显示图片 cv2.imshow("Recognizing...", image) # 暂停窗口