21_maskDetection.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from time import time
  5. from maix import display, camera,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. class Mask:
  46. mud_path = "/root/preset/model/mask_int8.mud"
  47. labels = ["no wear","wear"]
  48. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  49. def __init__(self) -> None:
  50. from maix import nn
  51. self.model = nn.load(self.mud_path)
  52. from maix.nn import decoder
  53. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  54. def __del__(self):
  55. del self.model
  56. del self.decoder
  57. def cal_fps(self ,start , end):
  58. one_second = 1
  59. one_flash = end - start
  60. fps = one_second / one_flash
  61. return fps
  62. def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
  63. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  64. img.draw_string(box[0], box[1]+ box[3] ,disp_str, scale=1,color=(0, 0, 255), thickness=1)
  65. img.draw_string(0, 0 ,'FPS :'+str(fps), scale=2 ,color=(0, 0, 255), thickness=1)
  66. def process(self,input):
  67. t = time()
  68. out = self.model.forward(input, quantize=1, layout = "hwc")
  69. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.6, img_size=(224,224))
  70. for i, box in enumerate(boxes):
  71. class_id = probs[i][0]
  72. prob = probs[i][1][class_id]
  73. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  74. fps = self.cal_fps(t, time())
  75. self.draw_rectangle_with_title(input, box, disp_str, fps)
  76. app = Mask()
  77. canvasImg = image.new(size = (320, 240))
  78. while True:
  79. img = getLcdRotation(camera.capture()).crop(0, 0,224, 224)
  80. app.process(img)
  81. canvasImg.draw_image(img,48,8)
  82. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  83. display.show(canvasImg)