17_objectRecognition.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : en
  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. ScreenOrientation = False
  11. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  12. model = {
  13. "param": "/root/preset/model/yolo2_20class_awnn.param",
  14. "bin": "/root/preset/model/yolo2_20class_awnn.bin"
  15. }
  16. options = {
  17. "model_type": "awnn",
  18. "inputs": {
  19. "input0": (224, 224, 3)
  20. },
  21. "outputs": {
  22. "output0": (7, 7, (1+4+20)*5)
  23. },
  24. "mean": [127.5, 127.5, 127.5],
  25. "norm": [0.0078125, 0.0078125, 0.0078125],
  26. }
  27. #labels = ['飞机', '自行车', '鸟', '船', '瓶子', '公共汽车', '汽车', '猫', '椅子', '牛', '餐桌', '狗', '马', '摩托车', '人', '盆栽', '羊', '沙发', '火车', '电视监视器']
  28. labels = ['plane', 'bicycle', 'bird', 'ship', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'table', 'dog', 'horse', 'motobike', 'human', 'Pot plant', 'sheep', 'sofa', 'train', 'tv']
  29. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  30. m = nn.load(model, opt=options)
  31. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  32. def lcdRotation(inputImg,rotationAngle):
  33. from maix import image
  34. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  35. if ScreenOrientation:
  36. imgRotationAim = image.new(size = (240, 320))
  37. else:
  38. imgRotationAim = image.new(size = (320, 240))
  39. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  40. if ScreenOrientation:
  41. CAMERAROTATE = +180
  42. else:
  43. CAMERAROTATE = +90
  44. canvasImg = image.new(size = (240, 320))
  45. while True:
  46. canvasImg.clear()
  47. img_objectrecognition = camera.capture()
  48. img_objectrecognition = img_objectrecognition.crop(0, 0,224, 224)
  49. out = m.forward(img_objectrecognition.tobytes(), quantize=True, layout="hwc")
  50. boxes, probs = yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]))
  51. if len(boxes):
  52. for boxesi, box in enumerate(boxes):
  53. boxes[boxesi].append(probs[boxesi])
  54. if len(boxes):
  55. for i in (boxes):
  56. img_objectrecognition.draw_string(10,0, (str(str(labels[i[4][0]])) + str(round(i[4][1][i[4][0]]*100, 2))), scale = 1, color = (255,0,0) , thickness = 1)
  57. img_objectrecognition.draw_rectangle(i[0],i[1], int(i[0]+i[2]),int(i[1]+i[3]), color=(255,0,0), thickness=1)
  58. else:
  59. img_objectrecognition.draw_string(10,0, "Not recognize any object", scale = 1, color = (255,0,0) , thickness = 1)
  60. if ScreenOrientation:
  61. img_objectrecognitionVER = img_objectrecognition.crop(0,0,240,320)
  62. img_objectrecognitionVER = img_objectrecognitionVER.rotate(-90, adjust=1)
  63. display.show(img_objectrecognitionVER)
  64. else:
  65. canvasImg.draw_image(img_objectrecognition,48,8)
  66. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  67. display.show(canvasImg)