14_humanDetection.py 2.4 KB

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