2018.2.19版本二,多人单目标判断 """ import cv2 import sys import gc from face_train_use_keras import Model if __name__ == '__main__': if len(sys.argv) != 2: print("Usage:%s camera_id\r\n" % (sys.argv[0])) sys.exit(0) # 加载模型 model = Model() model.load_model( file_path= '/home/yuwangwang/FaceRecognization_Version1/model/me.face.model.h5') # 框住人脸的矩形边框颜色,颜色为绿色 color = (0, 255, 0) # 捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(int(sys.argv[1])) # 人脸识别分类器本地存储路径 cascade_path = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml" # 循环检测识别人脸 while True:
#-*- coding: utf-8 -*- import cv2 import sys import gc from face_train_use_keras import Model if __name__ == '__main__': if len(sys.argv) != 2: print("Usage:%s camera_id\r\n" % (sys.argv[0])) sys.exit(0) #load model model = Model() model.load_model(file_path='me.face.model.h5') color = (0, 255, 0) cap = cv2.VideoCapture(int(sys.argv[1])) cascade_path = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml" while True: _, frame = cap.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #load classfacation cascade = cv2.CascadeClassifier(cascade_path) #where is face
def start(self): global string #加载模型 model = Model() model.load_model(file_path='./model/me.face.model.h5') #框住人脸的矩形边框颜色 color = (0, 255, 0) #捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(0) #人脸识别分类器本地存储路径 cascade_path = "E:/Python/Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml" #循环检测识别人脸 while True: _, frame = cap.read() #读取一帧视频 #图像灰化,降低计算复杂度 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #使用人脸识别分类器,读入分类器 cascade = cv2.CascadeClassifier(cascade_path) #利用分类器识别出哪个区域为人脸 faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) id = '' if len(faceRects) > 0: for faceRect in faceRects: x, y, w, h = faceRect #截取脸部图像提交给模型识别这是谁 image = frame[y - 10:y + h + 10, x - 10:x + w + 10] faceID = model.face_predict(image) #如果是“我” if faceID == 0: cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2) #文字提示是谁 cv2.putText( frame, 'KK', (x + 30, y + 30), #坐标 cv2.FONT_HERSHEY_SIMPLEX, #字体 1, #字号 (255, 0, 255), #颜色 2) #字的线宽 id = "KK" else: cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2) #文字提示是谁 cv2.putText( frame, 'WWL', (x + 30, y + 30), #坐标 cv2.FONT_HERSHEY_SIMPLEX, #字体 1, #字号 (255, 0, 255), #颜色 2) #字的线宽 id = "wwl" cv2.imshow("faceRecognition", frame) #等待10毫秒看是否有按键输入 k = cv2.waitKey(10) #如果输入q则退出循环 if k & 0xFF == ord('='): localtime = time.asctime(time.localtime(time.time())) print("wwl的打卡时间为时间为 :", localtime) self.onclock(id, localtime) file = './res/wwl.txt' with open(file, 'a+') as f: f.write(localtime + '\n') #加\n换行显示 break #释放摄像头并销毁所有窗口 cap.release() cv2.destroyAllWindows()
import gc from face_train_use_keras import Model import os import path import pymysql import time import pandas as pd ''' ''' path = "E:\\python\\face_5.17\\data" l = [] l = os.listdir(path) model = Model() model.load_model(file_path='./model/face_detect607.h5') #框住人脸的矩形边框颜色 color = (0, 255, 0) color2 = (255, 0, 0) color3 = (0, 0, 255) #捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(0) #"rtsp://*****:*****@192.168.1.108/cam/realmonitor?channel=1&subtype=0" #人脸识别分类器本地存储路径 cascade_path = "E:/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml" #conn = pymysql.connect(host='192.168.1.102', port=3306, user='******', passwd='123456',db="dect",charset='utf8') #cursor = conn.cursor()
#-*-coding:utf-8 -*- __auther__ = 'Alex' # -*- coding: utf-8 -*- import cv2 import dlib import time from face_train_use_keras import Model if __name__ == '__main__': # 加载模型 model = Model() model.load_model(file_path='./model/jiaYouBG.model.h5') # 框住人脸的矩形边框颜色 color = (0, 255, 0) # 捕获指定摄像头的实时视频流 cap = cv2.VideoCapture("./video/2.mp4") inWidth = 300 inHeight = 300 inScaleFactor = 1.0 net = cv2.dnn.readNetFromCaffe('openCVmodel/deploy.prototxt','openCVmodel/res10_300x300_ssd_iter_140000.caffemodel') predicterPath = "model/shape_predictor_68_face_landmarks.dat" sp = dlib.shape_predictor(predicterPath) # 人脸识别分类器本地存储路径 cascade_path = "/home/alex/桌面/face_recgnition/openCVmodel/haarcascades/haarcascade_frontalface_alt2.xml"
# -*- coding: utf-8 -*- import cv2 import sys import gc from face_train_use_keras import Model MODEL_PATH = './me.face.model.h5' if __name__ == '__main__': if len(sys.argv) != 2: print("Usage:%s camera_id\r\n" % (sys.argv[0])) sys.exit(0) # 加载模型 model = Model() model.load_model(MODEL_PATH) faceID = model.face_predict(image) print(faceID)
def start_face_recog(self, msg): self.stop_dialog() model = Model() model.load_model(file_path='./face_recog/model/face.model.h5') color = (0, 255, 0) cascade_path = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml" num = 0 pos_person = {'jiashi': 0, 'yushizhuo': 0} while num <= 2: frame = self.get_img() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cascade = cv2.CascadeClassifier(cascade_path) faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) if len(faceRects) > 0: if num == 0: self.show_image("recognising111.gif") sentence = "Please do not move. I am trying to find out who you \\pau=30\\ are" self.say(sentence) num += 1 for faceRect in faceRects: x, y, w, h = faceRect image = frame[y - 10:y + h + 10, x - 10:x + w + 10] faceID = model.face_predict(image) if faceID == 0: cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2) # 文字提示是谁 cv2.putText( frame, 'jiashi', (x + 30, y + 30), # 坐标 cv2.FONT_HERSHEY_SIMPLEX, # 字体 1, # 字号 (255, 0, 255), # 颜色 2) # 字的线宽 pos_person['jiashi'] += 1 elif faceID == 1: cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2) # 文字提示是谁 cv2.putText( frame, 'yushizhuo', (x + 30, y + 30), # 坐标 cv2.FONT_HERSHEY_SIMPLEX, # 字体 1, # 字号 (255, 0, 255), # 颜色 2) # 字的线宽 pos_person['yushizhuo'] += 1 else: pass cv2.namedWindow("face_pre", cv2.WINDOW_NORMAL) cv2.imshow("face_pre", frame) k = cv2.waitKey(1) if k & 0xFF == ord('q'): break cv2.destroyAllWindows() # 经过3次取值,获取的最有可能的人 print pos_person self.current_person = max(pos_person, key=pos_person.get) this_per = self.get_history_item(self.current_person) print "========================" item_list = list(this_per[self.current_person]) print item_list image_name = self.current_person + '.png' # 展示人的身份卡 self.show_image(image_name) sentence = "I see, you must be " + self.current_person + "\\pau=100\\, do you like the " \ + item_list[len(item_list) - 1] + " you bought last time ?" self.say(sentence) self.start_dialog() print('\033[0;32m [Kamerider I] Your name is ' + self.current_person + "\033[0m")
# -*- coding: utf-8 -*- import cv2 from face_train_use_keras import Model if __name__ == '__main__': model = Model() # 加载模型 model.load_model(file_path='my_transfer.h5') # 框住人脸的矩形边框颜色 color = (0, 255, 0) # 捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(0) # 人脸识别分类器本地存储路径 cascade_path = "haarcascade_frontalface_default.xml" # 循环检测识别人脸 while True: _, frame = cap.read() # 读取一帧视频 # 图像灰化,降低计算复杂度 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 使用人脸识别分类器,读入分类器 cascade = cv2.CascadeClassifier(cascade_path) # 利用分类器识别出哪个区域为人脸 faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2,
def show(): #加载模型 model = Model() model.load_model(file_path='./model/me.face.model.h5') #框住人脸的矩形边框颜色 color = (0, 255, 0) #捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(0) #人脸识别分类器本地存储路径 cascade_path = "/home/binge/anaconda3/pkgs/opencv3-3.1.0-py36_0/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml" #循环检测识别人脸 while True: _, frame = cap.read() #读取一帧视频 #图像灰化,降低计算复杂度 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #使用人脸识别分类器,读入分类器 cascade = cv2.CascadeClassifier(cascade_path) #利用分类器识别出哪个区域为人脸 faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) if len(faceRects) > 0: for faceRect in faceRects: x, y, w, h = faceRect #截取脸部图像提交给模型识别这是谁 image = frame[y - 10:y + h + 10, x - 10:x + w + 10] faceID = model.face_predict(image) #如果是“我” if faceID == 0: cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2) #文字提示是谁 cv2.putText( frame, 'me', (x + 30, y + 30), #坐标 cv2.FONT_HERSHEY_SIMPLEX, #字体 1, #字号 (255, 0, 255), #颜色 2) #字的线宽 else: pass cv2.imshow("识别", frame) #等待10毫秒看是否有按键输入 k = cv2.waitKey(10) #如果输入q则退出循环 if k & 0xFF == ord('q'): break #释放摄像头并销毁所有窗口 cap.release() cv2.destroyAllWindows()
#-*- coding: utf-8 -*- import cv2 import sys import gc from face_train_use_keras import Model from keras import backend as k if __name__ == '__main__': #加载模型 model = Model() model.load_model(file_path='model_weight.h5') #框住人脸的矩形边框颜色 color = (0, 255, 0) #捕获指定摄像头的实时视频流 #cap = cv2.VideoCapture('D:/tmp/vedio/123.mp4') cap = cv2.VideoCapture('D:/tmp/vedio/mengmeng.mp4') #人脸识别分类器本地存储路径 cascade_path = "C:/projects/opencv-python/opencv/modules/objdetect/src/cascadedetect/haarcascade_frontalface_alt2.xml" #循环检测识别人脸 while True: ret, frame = cap.read() #读取一帧视频 #图像灰化,降低计算复杂度 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #使用人脸识别分类器,读入分类器 cascade = cv2.CascadeClassifier(cascade_path) #利用分类器识别出哪个区域为人脸
gray2 = gray.astype(np.float32) dst = gray2 - blockImage2 dst = dst.astype(np.uint8) dst = cv2.GaussianBlur(dst, (3, 3), 0) dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR) return dst ############################################################################################ ############################################################################################ if __name__ == '__main__': # 加载模型 model = Model() model.load_model(file_path='./model/test.face.model.h5') # 框住人脸的矩形边框颜色 color = (0, 255, 0) # 捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(0) # 人脸识别分类器本地存储路径 cascade_path = "C:\\Users\\Administrator\\Desktop\\Graduation design\\code\\csdn\\haarcascade_frontalface_alt2.xml" # 循环检测识别人脸 while True: _, frame = cap.read() # 读取一帧视频 # frame=cv2.imread("dupei.jpg")#########################################################################################################测试使用
def read_capture(detector): # 加载模型 model = Model() model.load_model(file_path='./model/face.model.h5') num = 100 video = cv2.VideoCapture(0) if video.isOpened(): success, frame = video.read() while success: im = frame.copy() # dlib的人脸检测器只能检测80x80和更大的人脸,如果需要检测比它小的人脸,需要对图像上采样,一次上采样图像尺寸放大一倍 # rects = detector(img,1) #1次上采样 rects = detector(im, 0) print rects for rect in rects: # rect.left(),rect.top(),rect.right(),rect.bottom() img2recog = im[rect.top() - 10:rect.bottom() + 10, rect.left() - 10:rect.right() + 10] faceID = model.face_predict(img2recog) if faceID == 0: print "======================================================", faceID cv2.rectangle(im, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 0, 255), 2, 8) # 文字提示是谁 cv2.putText( im, 'jiashi', (rect.left() + 30, rect.top() - 30), # 坐标 cv2.FONT_HERSHEY_SIMPLEX, # 字体 1, # 字号 (255, 0, 255), # 颜色 2) # 字的线宽 elif faceID == 1: print "======================================================", faceID cv2.rectangle(im, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 0, 255), 2, 8) # 文字提示是谁 cv2.putText( im, 'renzhongxing', (rect.left() + 30, rect.top() - 30), # 坐标 cv2.FONT_HERSHEY_SIMPLEX, # 字体 1, # 字号 (255, 0, 255), # 颜色 2) # 字的线宽 elif faceID == 2: print "======================================================", faceID cv2.rectangle(im, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 0, 255), 2, 8) # 文字提示是谁 cv2.putText( im, 'jingzhibo', (rect.left() + 30, rect.top() - 30), # 坐标 cv2.FONT_HERSHEY_SIMPLEX, # 字体 1, # 字号 (255, 0, 255), # 颜色 2) # 字的线宽 elif faceID == 3: print "======================================================", faceID cv2.rectangle(im, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 0, 255), 2, 8) # 文字提示是谁 cv2.putText( im, 'zhanghaiyang', (rect.left() + 30, rect.top() - 30), # 坐标 cv2.FONT_HERSHEY_SIMPLEX, # 字体 1, # 字号 (255, 0, 255), # 颜色 2) # 字的线宽 cv2.imshow('capture face detection', im) if cv2.waitKey(1) >= 0: break success, frame = video.read() cv2.destroyAllWindows() video.release()
def __init__(self): self.model = Model() self.model.load_model(file_path='/home/yuwangwang/FaceRecognization_Version2/model/me.face.model.h5')
class Camera_reader(object): # 在初始化camera的时候建立模型,并加载已经训练好的模型 def __init__(self): self.model = Model() self.model.load_model(file_path='/home/yuwangwang/FaceRecognization_Version2/model/me.face.model.h5') def build_camera(self, camera_id, path_name): # opencv文件中人脸级联文件的位置,用于帮助识别图像或者视频流中的人脸 cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml') # 读取dataset数据集下的子文件夹名称 name_list = read_name_list(path_name) # 框住人脸的矩形边框颜色,颜色为绿色 color = (0, 255, 0) # 打开摄像头并开始读取画面 # 捕获指定摄像头的实时视频流 cap = cv2.VideoCapture(camera_id) success, frame = cap.read() while True: success, frame = cap.read() # 图像灰化 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 利用分类器识别出哪个区域为人脸 # 参数1:image--待检测图片,一般为灰度图像加快检测速度; # 参数2:objects--被检测物体的矩形框向量组; # 参数3:scaleFactor--表示在前后两次相继的扫描中,搜索窗口的比例系数。默认为1.1即每次搜索窗口依次扩大10%; # 参数4:minNeighbors--表示构成检测目标的相邻矩形的最小个数(默认为3个)。 # 如果组成检测目标的小矩形的个数和小于 min_neighbors - 1 都会被排除。 # 如果min_neighbors 为 0, 则函数不做任何操作就返回所有的被检候选矩形框, # 这种设定值一般用在用户自定义对检测结果的组合程序上; # 参数5:flags--要么使用默认值,要么使用CV_HAAR_DO_CANNY_PRUNING,如果设置为 # CV_HAAR_DO_CANNY_PRUNING,那么函数将会使用Canny边缘检测来排除边缘过多或过少的区域, # 因此这些区域通常不会是人脸所在区域; # 参数6、7:minSize和maxSize用来限制得到的目标区域的范围。 faceRects = cascade.detectMultiScale(frame_gray , 1.3, 5) # 识别人脸 #faceRects = cascade.detectMultiScale(frame_gray, scaleFactor = 1.2, minNeighbors = 3, minSize = (32, 32)) for (x, y, w, h) in faceRects: image = frame[y - 10: y + h + 10, x - 10: x + w + 10] label,prob = self.model.face_predict(image) # 利用模型对cv2识别出的人脸进行比对 if prob >0.8: # 如果模型认为概率高于70%则显示为模型中已有的label show_name = name_list[label] else: show_name = 'Stranger' # 显示名字的坐标,字体,字号为1,颜色为粉色,字的线宽为2 cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,255), 2) # 在人脸区域画一个正方形出来 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), color, thickness = 2) cv2.imshow("Camera", frame) # 等待10毫秒看是否有按键输入 k = cv2.waitKey(10) # 如果输入q则退出循环 if k & 0xFF == ord('q'): break # 释放摄像头并销毁所有窗口 cap.release() cv2.destroyAllWindows() return camera_id, path_name
# -*- coding: utf-8 -*- """ Created on Mon Mar 26 21:34:23 2018 @author: 杨玉林 """ import cv2 from face_train_use_keras import Model if __name__ == '__main__': model = Model() model.load_model(file_path='D:\\face detection\\data\\me.face.model.h5') color = (0, 255, 0) cap = cv2.VideoCapture(0) cascade_path = 'D:\\jiance\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt2.xml' while True: _, frame = cap.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cascade = cv2.CascadeClassifier(cascade_path) faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3,