face_test-compare.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. for name in dir():
  2. if not name.startswith('_'):
  3. del globals()[name]
  4. import gc
  5. import KPU as kpu
  6. from Maix import utils
  7. utils.gc_heap_size(0xAF000)
  8. import sensor
  9. import image
  10. import lcd
  11. import time
  12. from Maix import FPIOA, GPIO
  13. import gc
  14. from fpioa_manager import fm
  15. from board import board_info
  16. try:from cocorobo import display_cjk_string
  17. except:pass
  18. def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
  19. try:
  20. display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
  21. return canvas
  22. except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)
  23. import lcd
  24. import image
  25. try:from cocorobo import display_cjk_string
  26. except:pass
  27. def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
  28. try:
  29. display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
  30. return canvas
  31. except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)
  32. _canvas_x, _canvas_y = 0, 0
  33. def set_key_state(*_):
  34. global start_processing
  35. start_processing = True
  36. gc.enable()
  37. gc.collect()
  38. # kpu.memtest()
  39. task_fd = kpu.load("/sd/preset/models/face_reocgnition/FD.emodel")
  40. task_ld = kpu.load("/sd/preset/models/face_reocgnition/KP_chwise.emodel")
  41. task_fe = kpu.load("/sd/preset/models/face_reocgnition/face_recognition_v4_2020_09_07.emodel")
  42. #kpu.set_outputs(task, 0, 1,1,1) # 配置输出形状
  43. kpu.set_outputs(task_fe, 0, 1,1,512)
  44. # kpu.memtest()
  45. kpu.set_outputs(task_fe, 0,1,1, 512)
  46. fm.register(10, fm.fpioa.GPIOHS0)
  47. key_gpio = GPIO(GPIO.GPIOHS0, GPIO.IN)
  48. start_processing = False
  49. key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT)
  50. lcd.init(type=2)
  51. lcd.rotation(1)
  52. sensor.reset()
  53. sensor.set_pixformat(sensor.RGB565)
  54. sensor.set_framesize(sensor.QVGA)
  55. sensor.set_vflip(0)
  56. sensor.set_hmirror(0)
  57. sensor.run(1)
  58. anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437,6.92275, 6.718375, 9.01025) # anchor for face detect
  59. dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),(81, 105)] # standard face key point position
  60. a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
  61. img_lcd = image.Image()
  62. img_face = image.Image(size=(128, 128))
  63. index = -1
  64. a = img_face.pix_to_ai()
  65. record_ftr = []
  66. record_ftrs = []
  67. gc.collect()
  68. names = ["李晓明", "王晓丽"]
  69. lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))
  70. lcd.rotation(1)
  71. lcd.clear(lcd.BLACK)
  72. while True:
  73. canvas = sensor.snapshot()
  74. code = kpu.run_yolo2(task_fd, canvas)
  75. index = -1
  76. total_faces = 0
  77. if code:
  78. for i in code:
  79. # print(code)
  80. canvas.draw_rectangle(i.rect())
  81. # print(code[0])
  82. face_cut = canvas.cut(i.x(), i.y(), i.w(), i.h())
  83. face_cut_128 = face_cut.resize(128, 128)
  84. a = face_cut_128.pix_to_ai()
  85. fmap = kpu.forward(task_ld, face_cut_128)
  86. plist = fmap[:]
  87. le = (i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
  88. re = (i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
  89. nose = (i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
  90. lm = (i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
  91. rm = (i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h()))
  92. a = canvas.draw_circle(le[0], le[1], 4)
  93. a = canvas.draw_circle(re[0], re[1], 4)
  94. a = canvas.draw_circle(nose[0], nose[1], 4)
  95. a = canvas.draw_circle(lm[0], lm[1], 4)
  96. a = canvas.draw_circle(rm[0], rm[1], 4)
  97. src_point = [le, re, nose, lm, rm]
  98. T = image.get_affine_transform(src_point, dst_point)
  99. a = image.warp_affine_ai(canvas, img_face, T)
  100. a = img_face.ai_to_pix()
  101. del(face_cut_128)
  102. fmap = kpu.forward(task_fe, img_face)
  103. feature = kpu.face_encode(fmap[:])
  104. reg_flag = False
  105. scores = []
  106. for j in range(len(record_ftrs)):
  107. score = kpu.face_compare(record_ftrs[j], feature)
  108. scores.append(score)
  109. max_score = 0
  110. index = -1
  111. for k in range(len(scores)):
  112. if max_score < scores[k]:
  113. max_score = scores[k]
  114. index = k
  115. print(len(names), index)
  116. if(len(names) > index):
  117. if max_score > 85:
  118. a = lcd_draw_string(canvas, i.x(), i.y(), ("%s :%2.1f" % (names[index], max_score)), color=(0, 255, 0), scale=2, mono_space=False)
  119. else:
  120. a = lcd_draw_string(canvas, i.x(), i.y(), ("X :%2.1f" % (max_score)), color=(255, 0, 0), scale=2, mono_space=False)
  121. if start_processing:
  122. record_ftr = feature
  123. record_ftrs.append(record_ftr)
  124. start_processing = False
  125. break
  126. canvas = canvas.cut(40,0,240,240)
  127. lcd.display(canvas, oft=(0,0))
  128. gc.collect()
  129. # kpu.memtest()
  130. if "李晓明" == (names[index] if (index != -1 and len(names) > index) else ""):
  131. lcd_draw_string(canvas, 0,0, "李晓明", color=(255,0,0), scale=5, mono_space=False)
  132. elif "王晓丽" == (names[index] if (index != -1 and len(names) > index) else ""):
  133. lcd_draw_string(canvas, 0,0, "王晓丽", color=(255,0,0), scale=5, mono_space=False)
  134. lcd.display(canvas, oft=(_canvas_x,_canvas_y))