22_gestureRecognition.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : en
  4. from time import time
  5. from maix import image
  6. ScreenOrientation = False
  7. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  8. class Hand:
  9. mud_path = "/root/preset/model/hand_int8.mud"
  10. labels = ["0","1","2","3","4","5"]
  11. anchors = [3.78, 5.81, 3.97, 3.98, 4.05, 4.98, 4.81, 5.41, 2.91, 4.53]
  12. def __init__(self) -> None:
  13. from maix import nn
  14. self.model = nn.load(self.mud_path)
  15. from maix.nn import decoder
  16. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  17. def __del__(self):
  18. del self.model
  19. del self.decoder
  20. def cal_fps(self ,start , end):
  21. one_second = 1
  22. one_flash = end - start
  23. fps = one_second / one_flash
  24. return fps
  25. def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
  26. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  27. img.draw_string(box[0], box[1]+ box[3] ,disp_str, scale=1,color=(0, 0, 255), thickness=2)
  28. img.draw_string(0, 0 ,'FPS :'+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
  29. def process(self,input):
  30. t = time()
  31. out = self.model.forward(input, quantize=1, layout = "hwc")
  32. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.6, img_size=(224,224))
  33. for i, box in enumerate(boxes):
  34. class_id = probs[i][0]
  35. prob = probs[i][1][class_id]
  36. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  37. fps = self.cal_fps(t, time())
  38. self.draw_rectangle_with_title(input, box, disp_str, fps)
  39. def lcdRotation(inputImg,rotationAngle):
  40. from maix import image
  41. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  42. if ScreenOrientation:
  43. imgRotationAim = image.new(size = (240, 320))
  44. else:
  45. imgRotationAim = image.new(size = (320, 240))
  46. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  47. if ScreenOrientation:
  48. CAMERAROTATE = +180
  49. else:
  50. CAMERAROTATE = +90
  51. def main():
  52. from maix import display, camera,image
  53. global Hand
  54. app = Hand()
  55. canvasImg = image.new(size = (320, 240))
  56. while True:
  57. img = camera.capture().crop(0, 0,224, 224)
  58. app.process(img)
  59. canvasImg.draw_image(img,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)
  62. main()