19_handWrittenDigiRecognition.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  53. class Number_recognition:
  54. mdsc_path = "/root/preset/model/Number.mud"
  55. labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  56. anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38]
  57. def __init__(self):
  58. self.model = nn.load(self.mdsc_path)
  59. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  60. def __del__(self):
  61. del self.model
  62. del self.decoder
  63. def cal_fps(self ,start , end):
  64. one_second = 1
  65. one_flash = end - start
  66. fps = one_second / one_flash
  67. return fps
  68. number_recognition = Number_recognition()
  69. canvasImg = image.new(size = (240, 320))
  70. while True:
  71. canvasImg.clear()
  72. img_mnist = getLcdRotation(camera.capture())
  73. img_mnist = img_mnist.crop(0, 0,224, 224)
  74. out = number_recognition.model.forward(img_mnist, quantize=1, layout = "hwc")
  75. boxes, probs = number_recognition.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  76. if len(boxes):
  77. for boxesi, box in enumerate(boxes):
  78. boxes[boxesi].append(probs[boxesi])
  79. if len(boxes):
  80. for i in (boxes):
  81. img_mnist.draw_string(i[0],i[1], (str(number_recognition.labels[i[4][0]]) + str(str(":") + str(round(i[4][1][i[4][0]]*100, 2)))), scale = 1, color = (255,0,0) , thickness = 1)
  82. img_mnist.draw_rectangle(i[0],i[1], int(i[0] + i[2]),int(i[1] + i[3]), color=(255,0,0), thickness=1)
  83. if ScreenOrientation:
  84. img_mnistVER = img_mnist.crop(0,0,240,320)
  85. img_mnistVER = img_mnistVER.rotate(-90, adjust=1)
  86. display.show(img_mnistVER)
  87. else:
  88. canvasImg.draw_image(img_mnist,48,8)
  89. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  90. display.show(canvasImg)