17_objectRecognition.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import os
  10. ScreenOrientation = False
  11. try:
  12. if os.path.exists("/etc/cameraSize.cfg"):
  13. cameraSize = True
  14. else:
  15. cameraSize = False
  16. except:
  17. cameraSize = False
  18. def getLcdRotation(cameraCapture):
  19. global cameraSize
  20. if cameraSize:
  21. return lcdRotationNew(cameraCapture)
  22. else:
  23. return lcdRotation(cameraCapture)
  24. def lcdRotationNew(inputImg):
  25. global cameraSize,ScreenOrientation
  26. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  27. if ScreenOrientation:
  28. imgRotationAim = image.new(size = (240, 320))
  29. rotationAngle = 90
  30. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  31. else:
  32. imgRotationAim = image.new(size = (320, 240))
  33. GETROTATION = imageRotationBuffer
  34. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  35. return GETROTATION
  36. def lcdRotation(inputImg):
  37. global cameraSize,ScreenOrientation
  38. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  39. if ScreenOrientation:
  40. imgRotationAim = image.new(size = (240, 320))
  41. rotationAngle = 180
  42. else:
  43. imgRotationAim = image.new(size = (320, 240))
  44. rotationAngle = 90
  45. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  46. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  47. return GETROTATION
  48. if cameraSize==True:
  49. camera.camera.config(size=(320,240))
  50. else:
  51. camera.camera.config(size=(240,320))
  52. ScreenOrientation = False
  53. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  54. model = {
  55. "param": "/root/preset/model/yolo2_20class_awnn.param",
  56. "bin": "/root/preset/model/yolo2_20class_awnn.bin"
  57. }
  58. options = {
  59. "model_type": "awnn",
  60. "inputs": {
  61. "input0": (224, 224, 3)
  62. },
  63. "outputs": {
  64. "output0": (7, 7, (1+4+20)*5)
  65. },
  66. "mean": [127.5, 127.5, 127.5],
  67. "norm": [0.0078125, 0.0078125, 0.0078125],
  68. }
  69. labels = ['飞机', '自行车', '鸟', '船', '瓶子', '公共汽车', '汽车', '猫', '椅子', '牛', '餐桌', '狗', '马', '摩托车', '人', '盆栽', '羊', '沙发', '火车', '电视监视器']
  70. #labels = ['plane', 'bicycle', 'bird', 'ship', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'table', 'dog', 'horse', 'motobike', 'human', 'Pot plant', 'sheep', 'sofa', 'train', 'tv']
  71. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  72. m = nn.load(model, opt=options)
  73. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  74. canvasImg = image.new(size = (240, 320))
  75. while True:
  76. canvasImg.clear()
  77. img_objectrecognition = getLcdRotation(camera.capture())
  78. img_objectrecognition = img_objectrecognition.crop(0, 0,224, 224)
  79. out = m.forward(img_objectrecognition.tobytes(), quantize=True, layout="hwc")
  80. boxes, probs = yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]))
  81. if len(boxes):
  82. for boxesi, box in enumerate(boxes):
  83. boxes[boxesi].append(probs[boxesi])
  84. if len(boxes):
  85. for i in (boxes):
  86. 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)
  87. img_objectrecognition.draw_rectangle(i[0],i[1], int(i[0]+i[2]),int(i[1]+i[3]), color=(255,0,0), thickness=1)
  88. else:
  89. img_objectrecognition.draw_string(10,0, "No Result!", scale = 1, color = (255,0,0) , thickness = 1)
  90. if ScreenOrientation:
  91. img_objectrecognitionVER = img_objectrecognition.crop(0,0,240,320)
  92. img_objectrecognitionVER = img_objectrecognitionVER.rotate(-90, adjust=1)
  93. display.show(img_objectrecognitionVER)
  94. else:
  95. canvasImg.draw_image(img_objectrecognition,48,8)
  96. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  97. display.show(canvasImg)