14_humanDetection.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : en
  4. from time import time
  5. from maix import image
  6. class Person:
  7. mud_path = "/root/preset/model/person_int8.mud"
  8. labels = ["person"]
  9. anchors = [4.72, 6.26, 1.39, 3.53, 0.78, 1.9, 0.35, 0.95, 2.49, 4.87]
  10. def __init__(self) -> None:
  11. from maix import nn
  12. self.model = nn.load(self.mud_path)
  13. from maix.nn import decoder
  14. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  15. def __del__(self):
  16. del self.model
  17. del self.decoder
  18. def cal_fps(self ,start , end):
  19. one_second = 1
  20. one_flash = end - start
  21. fps = one_second / one_flash
  22. return fps
  23. def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
  24. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  25. img.draw_string(box[0], box[1]+ box[3] ,disp_str, scale=1,color=(0, 0, 255), thickness=2)
  26. #img.draw_string(0, 0 ,'FPS :'+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
  27. def process(self,input):
  28. t = time()
  29. out = self.model.forward(input, quantize=1, layout = "hwc")
  30. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.6, img_size=(224,224))
  31. for i, box in enumerate(boxes):
  32. class_id = probs[i][0]
  33. prob = probs[i][1][class_id]
  34. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  35. fps = self.cal_fps(t, time())
  36. self.draw_rectangle_with_title(input, box, disp_str, fps)
  37. canvasImg = image.new(size = (320, 240))
  38. def main():
  39. from maix import display, camera
  40. app = Person()
  41. #camera.config((224,224))
  42. while True:
  43. canvasImg.clear()
  44. img = camera.capture().crop(0, 0,224, 224)
  45. app.process(img)
  46. canvasImg.draw_image(img,48,8)
  47. canvasImg.draw_image(image.open("/root/preset/img/exit_ff0000_24x24.png"),288,216,alpha=1)
  48. display.show(canvasImg)
  49. main()