23_fingerGuessing.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from time import time
  5. from maix import image
  6. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  7. import os
  8. ScreenOrientation = False
  9. try:
  10. if os.path.exists("/etc/cameraSize.cfg"):
  11. cameraSize = True
  12. else:
  13. cameraSize = False
  14. except:
  15. cameraSize = False
  16. def getLcdRotation(cameraCapture):
  17. global cameraSize
  18. if cameraSize:
  19. return lcdRotationNew(cameraCapture)
  20. else:
  21. return lcdRotation(cameraCapture)
  22. def lcdRotationNew(inputImg):
  23. global cameraSize,ScreenOrientation
  24. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  25. if ScreenOrientation:
  26. imgRotationAim = image.new(size = (240, 320))
  27. rotationAngle = 90
  28. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  29. else:
  30. imgRotationAim = image.new(size = (320, 240))
  31. GETROTATION = imageRotationBuffer
  32. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  33. return GETROTATION
  34. def lcdRotation(inputImg):
  35. global cameraSize,ScreenOrientation
  36. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  37. if ScreenOrientation:
  38. imgRotationAim = image.new(size = (240, 320))
  39. rotationAngle = 180
  40. else:
  41. imgRotationAim = image.new(size = (320, 240))
  42. rotationAngle = 90
  43. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  44. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  45. return GETROTATION
  46. class Mora:
  47. mud_path = "/root/preset/model/mora_int8.mud"
  48. labels = ["Scissors", "Stone" ,"Paper"]
  49. anchors = [3.23, 3.25, 1.47, 1.55, 5.09, 5.33, 4.03, 4.28, 2.12, 2.56]
  50. def __init__(self) -> None:
  51. from maix import nn
  52. self.model = nn.load(self.mud_path)
  53. from maix.nn import decoder
  54. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  55. def __del__(self):
  56. del self.model
  57. del self.decoder
  58. def cal_fps(self ,start , end):
  59. one_second = 1
  60. one_flash = end - start
  61. fps = one_second / one_flash
  62. return fps
  63. def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
  64. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  65. img.draw_string(box[0], box[1] ,disp_str, scale=1,color=(222, 0, 3), thickness=2)
  66. #img.draw_string(0, 0 ,'FPS :'+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
  67. def process(self,input):
  68. t = time()
  69. out = self.model.forward(input, quantize=1, layout = "hwc")
  70. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.5, img_size=(224,224))
  71. for i, box in enumerate(boxes):
  72. class_id = probs[i][0]
  73. prob = probs[i][1][class_id]
  74. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  75. fps = self.cal_fps(t, time())
  76. self.draw_rectangle_with_title(input, box, disp_str, fps)
  77. def main():
  78. from maix import display, camera,image
  79. app = Mora()
  80. canvasImg = image.new(size = (320, 240))
  81. while True:
  82. img = getLcdRotation(camera.capture()).crop(0, 0,224, 224)
  83. app.process(img)
  84. canvasImg.draw_image(img,48,8)
  85. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  86. display.show(canvasImg)
  87. main()