16_faceComparison.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. '''
  2. Face recognize demo, download modle from maixhub first:
  3. https://maixhub.com/
  4. '''
  5. from maix import nn, display, camera, image
  6. from maix.nn.app import face
  7. from maix.nn.app.face import FaceRecognize
  8. import time
  9. import sys
  10. sys.path.append('/root/')
  11. from CocoPi import BUTTON
  12. key_A = BUTTON(14)
  13. key_B = BUTTON(8)
  14. key_C = BUTTON(13)
  15. key_D = BUTTON(7)
  16. ScreenOrientation = False
  17. class Face_Recognizer:
  18. def __init__(self, threshold = 0.5, nms = 0.3, max_face_num = 1):
  19. model = "/root/preset/model/retinaface.mud"
  20. model_fe = "/root/preset/model/fe_resnet.mud"
  21. self.input_size = (224, 224, 3)
  22. input_size_fe = (128, 128, 3)
  23. self.feature_len = 256
  24. self.features = []
  25. print("-- load model:", model)
  26. m = nn.load(model)
  27. print("-- load ok")
  28. print("-- load model:", model_fe)
  29. m_fe = nn.load(model_fe)
  30. print("-- load ok")
  31. self.recognizer = FaceRecognize(m, m_fe, self.feature_len, self.input_size, threshold, nms, max_face_num)
  32. print("-- init end")
  33. def get_faces(self, img, std_img = False):
  34. faces = self.recognizer.get_faces(img, std_img)
  35. return faces
  36. def __len__(self):
  37. return len(self.features)
  38. def add_user(self, name, feature):
  39. self.features.append([name, feature])
  40. return True
  41. def remove_user(self, name_del):
  42. rm = None
  43. for name, feature in self.features:
  44. if name_del == name:
  45. rm = [name, feature]
  46. if rm:
  47. self.features.remove(rm)
  48. return True
  49. return False
  50. def recognize(self, feature):
  51. max_score = 0
  52. uid = -1
  53. for i, user in enumerate(self.features):
  54. score = self.recognizer.compare(user[1], feature)
  55. if score > max_score:
  56. max_score = score
  57. uid = i
  58. if uid >= 0:
  59. return self.features[uid][0], max_score
  60. return None, 0
  61. def get_input_size(self):
  62. '''
  63. @return input_size (w, h, c)
  64. '''
  65. return self.input_size
  66. def get_feature_len(self):
  67. return self.feature_len
  68. def darw_info(self, img, box, points, disp_str, bg_color=(255, 0, 0, 255), font_color=(255, 255, 255, 255), font_size=32):
  69. font_wh = image.get_string_size(disp_str)
  70. for p in points:
  71. img.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=bg_color)
  72. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3], color=bg_color, thickness=2)
  73. if disp_str:
  74. img.draw_rectangle(box[0], box[1] - font_wh[1], box[0] + font_wh[0], box[1], color=bg_color, thickness = -1)
  75. img.draw_string(box[0], box[1] - font_wh[1], disp_str, color=font_color)
  76. def darw_title(self, img, dis_size ,key_l = None, key_r =None):
  77. if key_C:
  78. key_l = "| "+ key_l
  79. img.draw_string( 1, 2 ,key_l , scale = 1, color = (255, 255, 255), thickness = 2)
  80. if key_D:
  81. key_r = key_r+" |"
  82. w = int(dis_size[0] - 4 - image.get_string_size(key_r)[0] * 1)
  83. img.draw_string( w, 2 ,key_r , scale = 1, color = (255, 255, 255), thickness = 2)
  84. max_face_num = 4
  85. detect_threshold = 0.5
  86. detect_nms = 0.3
  87. score_threshold = 70
  88. names = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
  89. face_recognizer = Face_Recognizer(detect_threshold, detect_nms, max_face_num = max_face_num)
  90. camera.config(size=face_recognizer.get_input_size()[:2])
  91. def lcdRotation(inputImg,rotationAngle):
  92. from maix import image
  93. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  94. if ScreenOrientation:
  95. imgRotationAim = image.new(size = (240, 320))
  96. else:
  97. imgRotationAim = image.new(size = (320, 240))
  98. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  99. if ScreenOrientation:
  100. CAMERAROTATE = +180
  101. else:
  102. CAMERAROTATE = +90
  103. canvasImg = image.new(size = (240, 320))
  104. while 1:
  105. canvasImg.clear()
  106. img = camera.capture().crop(0, 0,224, 224)
  107. if not img:
  108. time.sleep(0.02)
  109. continue
  110. faces = face_recognizer.get_faces(img)
  111. face_recognizer.darw_title(img , face_recognizer.get_input_size()[:2] , "rm" ,"add")
  112. if faces:
  113. # for prob, box, landmarks, feature, std_img in faces:
  114. for prob, box, landmarks, feature in faces:
  115. # [ prob, [x,y,w,h], [[x,y], [x,y], [x,y], [x,y], [x,y]], feature ]
  116. if key_C.is_pressed():
  117. if len(face_recognizer) < len(names):
  118. while not (key_C.is_pressed() == False):
  119. time.sleep(0.1)
  120. idx = len(face_recognizer)
  121. print("add user: {}, {}".format(idx, names[idx]))
  122. face_recognizer.add_user(names[idx], feature)
  123. else:
  124. print("user full")
  125. name, score = face_recognizer.recognize(feature)
  126. if name:
  127. if score > score_threshold:
  128. face_recognizer.darw_info(img, box, landmarks, "{}:{:.2f}".format(name, score), font_color=(0, 0, 255, 255), bg_color=(0, 255, 0, 255))
  129. print("user: {}, score: {:.2f}".format(name, score))
  130. else:
  131. face_recognizer.darw_info(img, box, landmarks, "{}:{:.2f}".format(name, score), font_color=(255, 255, 255, 255), bg_color=(255, 0, 0, 255))
  132. print("maybe user: {}, score: {:.2f}".format(name, score))
  133. else:
  134. face_recognizer.darw_info(img, box, landmarks, "", font_color=(255, 255, 255, 255), bg_color=(255, 255, 255, 255))
  135. if key_D.is_pressed():
  136. if len(face_recognizer) > 0:
  137. while not (key_D.is_pressed() == False):
  138. time.sleep(0.1)
  139. idx = len(face_recognizer) - 1
  140. print("remove user:", names[idx])
  141. face_recognizer.remove_user(names[idx])
  142. else:
  143. print("user empty")
  144. canvasImg.draw_image(img,48,8)
  145. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  146. display.show(canvasImg)