15_faceDetection.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : ch
  4. from maix import camera
  5. from maix import display
  6. from maix import image
  7. from maix import nn
  8. from maix.nn import decoder
  9. camera.camera.config(size=(240,320))
  10. ScreenOrientation = False
  11. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  12. model = {
  13. "param": "/root/preset/model/yolo2_face_awnn.param",
  14. "bin": "/root/preset/model/yolo2_face_awnn.bin"
  15. }
  16. labels = ["person"]
  17. options = {
  18. "model_type": "awnn",
  19. "inputs": {
  20. "input0": (224, 224, 3)
  21. },
  22. "outputs": {
  23. "output0": (7, 7, (1+4+len(labels))*5)
  24. },
  25. "mean": [127.5, 127.5, 127.5],
  26. "norm": [0.0078125, 0.0078125, 0.0078125],
  27. }
  28. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  29. m = nn.load(model, opt=options)
  30. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  31. def lcdRotation(inputImg,rotationAngle):
  32. from maix import image
  33. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  34. if ScreenOrientation:
  35. imgRotationAim = image.new(size = (240, 320))
  36. else:
  37. imgRotationAim = image.new(size = (320, 240))
  38. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  39. if ScreenOrientation:
  40. CAMERAROTATE = +180
  41. else:
  42. CAMERAROTATE = +90
  43. canvasImg = image.new(size = (240, 320))
  44. while True:
  45. img_facedetection = lcdRotation(camera.capture(),CAMERAROTATE)
  46. img_facedetection = img_facedetection.crop(0, 0,224, 224)
  47. out = m.forward(img_facedetection.tobytes(), quantize=True, layout="hwc")
  48. boxes, probs = yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]))
  49. if len(boxes):
  50. for i in (boxes):
  51. img_facedetection.draw_rectangle(i[0],i[1], int(i[0]+i[2]),int(i[1]+i[3]), color=(255,0,0), thickness=1)
  52. if ScreenOrientation:
  53. img_facedetectionVER = img_facedetection.crop(0,0,240,320)
  54. img_facedetectionVER = img_facedetectionVER.rotate(-90, adjust=1)
  55. display.show(img_facedetectionVER)
  56. else:
  57. canvasImg.draw_image(img_facedetection,48,8)
  58. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  59. display.show(canvasImg)