当前位置:   article > 正文

python人脸识别基于mtcnn和facenet考勤_mtcnn facenet fastapi

mtcnn facenet fastapi

项目下载:https://download.csdn.net/download/babyai996/87554954icon-default.png?t=N176https://download.csdn.net/download/babyai996/87554954

基于人脸识别的课堂考勤系统

 

课堂考勤是保证学生出勤率的重要手段之一,也是学生课程成绩重要的组成部分,课堂考勤可以很好的监督学生,从而确保了课堂的教学质量。目前主要的考勤手段仍然是教师人工点名或者随机抽查的方式,这种人工点名的方式不仅占用上课时间而且无法解决学生的早退、代签和旷课等现象,无法很好的对学生考勤做到监管.市面上也存在着各种各样的考勤机,但是由于硬件和部署的成本较高,无法很好的解决考勤问题。

为了解决上述的问题,本文采用人脸识别技术,设计实现了一个适合日常课堂考勤的自动化人脸识别考勤系统,主要研究了该系统的设计与功能的实现。阐述了人脸识别技术的基本概念、发展阶段和基本方法,分析了传统考勤方式的不足,同时也介绍了国内外相关技术的发展历史和研究成果。接着详细介绍了活体检测、人脸检测及人脸识别的主要方法。然后根据考勤系统在功能和性能上的各种需求,进行软件架构的设计。最终整体介绍系统的功能以及测试内容,并展示了相应的GUI界面。

利用python实现人脸识别技术是基于mtcnn和facenet考勤

实现了人脸大角度识别

 

  1. import cv2
  2. import os
  3. import numpy as np
  4. from net.mtcnn import mtcnn
  5. import utils.utils as utils
  6. from net.inception import InceptionResNetV1
  7. import time
  8. from dateutil.parser import parse
  9. font = cv2.FONT_HERSHEY_COMPLEX
  10. def seekopp(list_2,i):
  11. for j in range(i-1,-1,-1):
  12. if list_2[j]==0:
  13. continue
  14. elif list_2[j]>0:
  15. return 1
  16. elif list_2[j]<0:
  17. return 0
  18. def local_maximum(list_1):
  19. a=len(list_1)
  20. if a==0:
  21. return 'error'
  22. if a==1:
  23. return list_1
  24. if a==2:
  25. if list_1[0]>list_1[1]:
  26. return list_1[0]
  27. elif list_1[0]<list_1[1]:
  28. return list_1[1]
  29. else:
  30. return list_1
  31. if a>2:
  32. list_2=[]
  33. index_1=[]
  34. for i in range(0,a-1):
  35. list_2.append(list_1[i+1]-list_1[i])
  36. b=len(list_2)
  37. if list_2[0]<0:
  38. index_1.append(0)
  39. for i in range(0,b-1):
  40. if list_2[i+1]<0:
  41. if list_2[i]>0:
  42. index_1.append(i+1)
  43. elif list_2[i]==0:
  44. if seekopp(list_2,i):
  45. index_1.append(i+1)
  46. else:
  47. continue
  48. else:
  49. continue
  50. list_3=[]
  51. for i in index_1:
  52. list_3.append(list_1[i])
  53. return list_3
  54. def transform(t1,t2):
  55. timeArray1 = time.localtime(t1)
  56. timeArray2 = time.localtime(t2)
  57. time_1 = time.strftime("%Y-%m-%d %H:%M:%S", timeArray1)
  58. time_2 = time.strftime("%Y-%m-%d %H:%M:%S", timeArray2)
  59. date1 = parse(time_1)
  60. date2 = parse(time_2)
  61. result = int((date2 - date1).total_seconds())
  62. return result
  63. class face_rec():
  64. time = 0
  65. def __init__(self):
  66. # 创建mtcnn对象
  67. # 检测图片中的人脸
  68. self.mtcnn_model = mtcnn()
  69. # 门限函数
  70. self.threshold = [0.5,0.8,0.9]
  71. # 载入facenet
  72. # 将检测到的人脸转化为128维的向量
  73. self.facenet_model = InceptionResNetV1()
  74. # model.summary()
  75. model_path = './model_data/facenet_keras.h5'
  76. self.facenet_model.load_weights(model_path)
  77. #-----------------------------------------------#
  78. # 对数据库中的人脸进行编码
  79. # known_face_encodings中存储的是编码后的人脸
  80. # known_face_names为人脸的名字
  81. #-----------------------------------------------#
  82. face_list = os.listdir("face_dataset")
  83. self.known_face_encodings=[]
  84. self.known_face_names=[]
  85. self.time_list=[]
  86. self_learn_time = 0
  87. self.total_time = []
  88. for face in face_list:
  89. name = face.split(".")[0]
  90. img = cv2.imread("./face_dataset/"+face)
  91. img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
  92. # 检测人脸
  93. rectangles = self.mtcnn_model.detectFace(img, self.threshold)
  94. # 转化成正方形
  95. rectangles = utils.rect2square(np.array(rectangles))
  96. # facenet要传入一个160x160的图片
  97. rectangle = rectangles[0]
  98. # 记下他们的landmark
  99. landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160
  100. crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
  101. crop_img = cv2.resize(crop_img,(160,160))
  102. new_img,_ = utils.Alignment_1(crop_img,landmark)
  103. new_img = np.expand_dims(new_img,0)
  104. # 将检测到的人脸传入到facenet的模型中,实现128维特征向量的提取
  105. face_encoding = utils.calc_128_vec(self.facenet_model,new_img)
  106. self.known_face_encodings.append(face_encoding)
  107. self.known_face_names.append(name)
  108. def recognize(self,draw):
  109. #-----------------------------------------------#
  110. # 人脸识别
  111. # 先定位,再进行数据库匹配
  112. #-----------------------------------------------#
  113. height,width,_ = np.shape(draw)
  114. draw_rgb = cv2.cvtColor(draw,cv2.COLOR_BGR2RGB)
  115. # 检测人脸
  116. rectangles = self.mtcnn_model.detectFace(draw_rgb, self.threshold)
  117. '''
  118. if len(rectangles)==0:
  119. font = cv2.FONT_HERSHEY_SIMPLEX
  120. if time_flag==0:
  121. print('首次执行!')
  122. first_time = time.time()
  123. t = transform(int(first_time),int(time.time()))
  124. cv2.putText(draw, "Focus on time:"+str(t)+" s", (250, 40), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  125. time_flag==1
  126. return
  127. else:
  128. print('第二次执行!')
  129. t = transform(int(first_time),int(time.time()))
  130. cv2.putText(draw, "Focus on time:"+str(t)+" s", (250, 40), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  131. return
  132. '''
  133. if len(rectangles)==0:
  134. self.time_list.append(time.time())
  135. font = cv2.FONT_HERSHEY_SIMPLEX
  136. t = transform(self.time_list[0],self.time_list[-1])
  137. #print(t,type(t))
  138. cv2.putText(draw, "Noface", (40, 40), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  139. cv2.putText(draw, "Focus on time:"+str(t)+" s", (350, 40), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  140. #print(self.total_time,type(self.total_time))
  141. if len(self.total_time)==0:
  142. self.total_time.append(t)
  143. elif self.total_time[-1]<= t:
  144. self.total_time.pop()
  145. self.total_time.append(t)
  146. elif self.total_time[-1]>= t:
  147. self.total_time.append(t)
  148. else:
  149. pass
  150. return draw,self.total_time
  151. self.time_list.clear()
  152. # 转化成正方形
  153. rectangles = utils.rect2square(np.array(rectangles,dtype=np.int32))
  154. rectangles[:,0] = np.clip(rectangles[:,0],0,width)
  155. rectangles[:,1] = np.clip(rectangles[:,1],0,height)
  156. rectangles[:,2] = np.clip(rectangles[:,2],0,width)
  157. rectangles[:,3] = np.clip(rectangles[:,3],0,height)
  158. #-----------------------------------------------#
  159. # 对检测到的人脸进行编码
  160. #-----------------------------------------------#
  161. face_encodings = []
  162. for rectangle in rectangles:
  163. landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160
  164. crop_img = draw_rgb[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
  165. crop_img = cv2.resize(crop_img,(160,160))
  166. new_img,_ = utils.Alignment_1(crop_img,landmark)
  167. new_img = np.expand_dims(new_img,0)
  168. face_encoding = utils.calc_128_vec(self.facenet_model,new_img)
  169. face_encodings.append(face_encoding)
  170. face_names = []
  171. for face_encoding in face_encodings:
  172. # 取出一张脸并与数据库中所有的人脸进行对比,计算得分
  173. matches = utils.compare_faces(self.known_face_encodings, face_encoding, tolerance = 0.9)
  174. name = "Unknown"
  175. # 找出距离最近的人脸
  176. face_distances = utils.face_distance(self.known_face_encodings, face_encoding)
  177. # 取出这个最近人脸的评分
  178. best_match_index = np.argmin(face_distances)
  179. if matches[best_match_index]:
  180. name = self.known_face_names[best_match_index]
  181. face_names.append(name)
  182. rectangles = rectangles[:,0:4]
  183. #-----------------------------------------------#
  184. # 画框~!~
  185. #-----------------------------------------------#
  186. for (left, top, right, bottom), name in zip(rectangles, face_names):
  187. cv2.rectangle(draw, (left, top), (right, bottom), (0, 0, 255), 2)
  188. font = cv2.FONT_HERSHEY_SIMPLEX
  189. cv2.putText(draw, name, (left , bottom - 15), font, 0.75, (255, 255, 255), 2)
  190. if name!='Unknown':
  191. cv2.putText(draw, 'name:'+name, (10, 20), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  192. cv2.putText(draw, "state:"+"clock on", (10, 60), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  193. cv2.putText(draw, "Please concentrate on your study!", (200, 40), font, 0.6, (0, 0, 255), 1, cv2.LINE_AA)
  194. else:
  195. cv2.putText(draw, 'name:'+name, (10, 20), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  196. cv2.putText(draw, "state:"+"clock out failed", (10, 60), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
  197. cv2.putText(draw, "Please add face!", (300, 40), font, 0.6, (0, 0, 255), 1, cv2.LINE_AA)
  198. return draw,self.total_time
  199. if __name__ == "__main__":
  200. dududu = face_rec()
  201. video_capture = cv2.VideoCapture(0)
  202. while True:
  203. ret, draw = video_capture.read()
  204. dududu.recognize(draw)
  205. cv2.imshow('Video', draw)
  206. if cv2.waitKey(20) & 0xFF == ord('q'):
  207. break
  208. video_capture.release()
  209. cv2.destroyAllWindows()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/325338
推荐阅读
相关标签
  

闽ICP备14008679号