22_gestureRecognition.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : en
  4. from time import time
  5. from maix import image
  6. import os
  7. ScreenOrientation = False
  8. try:
  9. if os.path.exists("/etc/cameraSize.cfg"):
  10. cameraSize = True
  11. else:
  12. cameraSize = False
  13. except:
  14. cameraSize = False
  15. def getLcdRotation(cameraCapture):
  16. global cameraSize
  17. if cameraSize:
  18. return lcdRotationNew(cameraCapture)
  19. else:
  20. return lcdRotation(cameraCapture)
  21. def lcdRotationNew(inputImg):
  22. global cameraSize,ScreenOrientation
  23. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  24. if ScreenOrientation:
  25. imgRotationAim = image.new(size = (240, 320))
  26. rotationAngle = 90
  27. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  28. else:
  29. imgRotationAim = image.new(size = (320, 240))
  30. GETROTATION = imageRotationBuffer
  31. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  32. return GETROTATION
  33. def lcdRotation(inputImg):
  34. global cameraSize,ScreenOrientation
  35. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  36. if ScreenOrientation:
  37. imgRotationAim = image.new(size = (240, 320))
  38. rotationAngle = 180
  39. else:
  40. imgRotationAim = image.new(size = (320, 240))
  41. rotationAngle = 90
  42. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  43. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  44. return GETROTATION
  45. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  46. class Hand:
  47. mud_path = "/root/preset/model/hand_int8.mud"
  48. labels = ["0","1","2","3","4","5"]
  49. anchors = [3.78, 5.81, 3.97, 3.98, 4.05, 4.98, 4.81, 5.41, 2.91, 4.53]
  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]+ box[3] ,disp_str, scale=1,color=(0, 0, 255), 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.6, 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. global Hand
  80. app = Hand()
  81. canvasImg = image.new(size = (320, 240))
  82. while True:
  83. img = getLcdRotation(camera.capture()).crop(0, 0,224, 224)
  84. app.process(img)
  85. canvasImg.draw_image(img,48,8)
  86. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  87. display.show(canvasImg)
  88. main()