14_humanDetection.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from time import time
  5. from maix import image
  6. import os
  7. from maix import display, camera
  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 Person:
  47. mud_path = "/root/preset/model/person_int8.mud"
  48. labels = ["person"]
  49. anchors = [4.72, 6.26, 1.39, 3.53, 0.78, 1.9, 0.35, 0.95, 2.49, 4.87]
  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. canvasImg = image.new(size = (320, 240))
  78. if cameraSize==True:
  79. camera.camera.config(size=(320,240))
  80. else:
  81. camera.camera.config(size=(240,320))
  82. def main():
  83. app = Person()
  84. #camera.config((224,224))
  85. while True:
  86. canvasImg.clear()
  87. img = getLcdRotation(camera.capture()).crop(0, 0,224, 224)
  88. app.process(img)
  89. canvasImg.draw_image(img,48,8)
  90. canvasImg.draw_image(image.open("/root/preset/img/exit_ff0000_24x24.png"),288,216,alpha=1)
  91. display.show(canvasImg)
  92. main()