15_faceDetection.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  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=(320,240))
  10. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  11. model = {
  12. "param": "/root/preset/model/yolo2_face_awnn.param",
  13. "bin": "/root/preset/model/yolo2_face_awnn.bin"
  14. }
  15. labels = ["person"]
  16. options = {
  17. "model_type": "awnn",
  18. "inputs": {
  19. "input0": (224, 224, 3)
  20. },
  21. "outputs": {
  22. "output0": (7, 7, (1+4+len(labels))*5)
  23. },
  24. "mean": [127.5, 127.5, 127.5],
  25. "norm": [0.0078125, 0.0078125, 0.0078125],
  26. }
  27. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  28. m = nn.load(model, opt=options)
  29. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  30. canvasImg = image.new(size = (240, 320))
  31. while True:
  32. canvasImg.clear()
  33. img_facedetection = camera.capture()
  34. img_facedetection = img_facedetection.crop(0, 0,224, 224)
  35. out = m.forward(img_facedetection.tobytes(), quantize=True, layout="hwc")
  36. boxes, probs = yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]))
  37. if len(boxes):
  38. for i in (boxes):
  39. img_facedetection.draw_rectangle(i[0],i[1], int(i[0]+i[2]),int(i[1]+i[3]), color=(255,0,0), thickness=1)
  40. canvasImg.draw_image(img_facedetection,48,8)
  41. canvasImg.draw_image(image.open("/root/preset/img/exit_ff0000_24x24.png"),288,216,alpha=1)
  42. display.show(canvasImg)