20_carLicensePlateRecognition.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 LPR:
  46. loc_model_path = '/root/preset/model/loc.mud'
  47. reg_model_path = '/root/preset/model/reg.mud'
  48. chars =[ "皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  49. "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘" , "粤",
  50. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青" , "宁",
  51. "新", "警", "学", "A" , "B" , "C" , "D" , "E" , "F" , "G",
  52. "H" , "J" , "K" , "L" , "M" , "N" , "P" , "Q" , "R" , "S",
  53. "T" , "U" , "V" , "W", "X" , "Y" , "Z" , "0" , "1", "2", "3",
  54. "4", "5", "6", "7", "8", "9", "-"]
  55. variances = [0.1, 0.2]
  56. steps = [8, 16, 32]
  57. min_sizes = [12, 24, 48, 96, 192, 320]
  58. def __init__(self) -> None:
  59. from maix import nn
  60. self.loc_model = nn.load(self.loc_model_path , opt = None)
  61. self.reg_model = nn.load(self.reg_model_path , opt = None)
  62. from maix.nn import decoder
  63. self.loc_decoder = decoder.license_plate_location([224,224] , self.steps , self.min_sizes, self.variances)
  64. self.reg_decoder = decoder.CTC((1,68,18))
  65. def __del__(self):
  66. del self.loc_model
  67. del self.loc_decoder
  68. def cal_fps(self ,start , end):
  69. one_second = 1
  70. one_flash = end - start
  71. fps = one_second / one_flash
  72. return fps
  73. def draw_fps(self,img , fps):
  74. img.draw_string(0, 0 ,'FPS :'+str(fps), scale=1,color=(255, 0, 255), thickness=1)
  75. def draw_string(self , img , x , y , string , color):
  76. img.draw_string( x , y , string ,color = color)
  77. def draw_paste(self , src ,dst):
  78. src.paste(dst , 0 , 0)
  79. def draw_rectangle(self,img, box):
  80. img.draw_rectangle(box[0], box[1], box[2], box[3],color=(230 ,230, 250), thickness=2)
  81. def draw_point(self,img,landmark):
  82. for i in range(4):
  83. x = landmark[2 * i ]
  84. y = landmark[2 * i + 1]
  85. img.draw_rectangle(x-2,y-2, x+2,y+2,color= (193 ,255 ,193), thickness =-1)
  86. def process(self,input):
  87. loc_out = self.loc_model.forward(input, quantize=1, layout = "chw") # retinaface decoder only support chw layout
  88. boxes , landmarks = self.loc_decoder.run(loc_out, nms = 0.2 ,score_thresh = 0.7 , outputs_shape =[[1,4,2058],[1,2,2058],[1,8,2058]])
  89. for i,box in enumerate(boxes):
  90. landmark = landmarks[i][:6]
  91. reg_in = input.crop_affine(landmark , 94 , 24)
  92. reg_out = self.reg_model.forward(reg_in , quantize=1, layout = "chw")
  93. LP_number = self.reg_decoder.run(reg_out)
  94. string_LP = ''
  95. for id in LP_number:
  96. string_LP += self.chars[id]
  97. self.draw_string(input , box[0], box[1] , string_LP ,color=(225,0,0))
  98. self.draw_paste(input , reg_in)
  99. self.draw_rectangle(input,box)
  100. self.draw_point(input , landmarks[i])
  101. def main():
  102. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  103. app = LPR()
  104. canvasImg = image.new(size = (240, 320))
  105. while True:
  106. canvasImg.clear()
  107. img = getLcdRotation(camera.capture()).crop(0, 0,224, 224)
  108. app.process(img)
  109. canvasImg.draw_image(img,48,8)
  110. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  111. display.show(canvasImg)
  112. # break
  113. main()