19_handWrittenDigiRecognition.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. ScreenOrientation = False
  11. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  12. def lcdRotation(inputImg,rotationAngle):
  13. from maix import image
  14. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  15. if ScreenOrientation:
  16. imgRotationAim = image.new(size = (240, 320))
  17. else:
  18. imgRotationAim = image.new(size = (320, 240))
  19. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  20. if ScreenOrientation:
  21. CAMERAROTATE = +180
  22. else:
  23. CAMERAROTATE = +90
  24. class Number_recognition:
  25. mdsc_path = "/root/preset/model/Number.mud"
  26. labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  27. anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38]
  28. def __init__(self):
  29. self.model = nn.load(self.mdsc_path)
  30. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  31. def __del__(self):
  32. del self.model
  33. del self.decoder
  34. def cal_fps(self ,start , end):
  35. one_second = 1
  36. one_flash = end - start
  37. fps = one_second / one_flash
  38. return fps
  39. number_recognition = Number_recognition()
  40. canvasImg = image.new(size = (240, 320))
  41. while True:
  42. canvasImg.clear()
  43. img_mnist = camera.capture()
  44. img_mnist = img_mnist.crop(0, 0,224, 224)
  45. out = number_recognition.model.forward(img_mnist, quantize=1, layout = "hwc")
  46. boxes, probs = number_recognition.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  47. if len(boxes):
  48. for boxesi, box in enumerate(boxes):
  49. boxes[boxesi].append(probs[boxesi])
  50. if len(boxes):
  51. for i in (boxes):
  52. 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)
  53. img_mnist.draw_rectangle(i[0],i[1], int(i[0] + i[2]),int(i[1] + i[3]), color=(255,0,0), thickness=1)
  54. if ScreenOrientation:
  55. img_mnistVER = img_mnist.crop(0,0,240,320)
  56. img_mnistVER = img_mnistVER.rotate(-90, adjust=1)
  57. display.show(img_mnistVER)
  58. else:
  59. canvasImg.draw_image(img_mnist,48,8)
  60. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  61. display.show(canvasImg)