20_carLicensePlateRecognition.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : en
  4. from time import time
  5. ScreenOrientation = False
  6. class LPR:
  7. loc_model_path = '/root/preset/model/loc.mud'
  8. reg_model_path = '/root/preset/model/reg.mud'
  9. chars =[ "皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  10. "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘" , "粤",
  11. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青" , "宁",
  12. "新", "警", "学", "A" , "B" , "C" , "D" , "E" , "F" , "G",
  13. "H" , "J" , "K" , "L" , "M" , "N" , "P" , "Q" , "R" , "S",
  14. "T" , "U" , "V" , "W", "X" , "Y" , "Z" , "0" , "1", "2", "3",
  15. "4", "5", "6", "7", "8", "9", "-"]
  16. variances = [0.1, 0.2]
  17. steps = [8, 16, 32]
  18. min_sizes = [12, 24, 48, 96, 192, 320]
  19. def __init__(self) -> None:
  20. from maix import nn
  21. self.loc_model = nn.load(self.loc_model_path , opt = None)
  22. self.reg_model = nn.load(self.reg_model_path , opt = None)
  23. from maix.nn import decoder
  24. self.loc_decoder = decoder.license_plate_location([224,224] , self.steps , self.min_sizes, self.variances)
  25. self.reg_decoder = decoder.CTC((1,68,18))
  26. def __del__(self):
  27. del self.loc_model
  28. del self.loc_decoder
  29. def cal_fps(self ,start , end):
  30. one_second = 1
  31. one_flash = end - start
  32. fps = one_second / one_flash
  33. return fps
  34. def draw_fps(self,img , fps):
  35. img.draw_string(0, 0 ,'FPS :'+str(fps), scale=1,color=(255, 0, 255), thickness=1)
  36. def draw_string(self , img , x , y , string , color):
  37. img.draw_string( x , y , string ,color = color)
  38. def draw_paste(self , src ,dst):
  39. src.paste(dst , 0 , 0)
  40. def draw_rectangle(self,img, box):
  41. img.draw_rectangle(box[0], box[1], box[2], box[3],color=(230 ,230, 250), thickness=2)
  42. def draw_point(self,img,landmark):
  43. for i in range(4):
  44. x = landmark[2 * i ]
  45. y = landmark[2 * i + 1]
  46. img.draw_rectangle(x-2,y-2, x+2,y+2,color= (193 ,255 ,193), thickness =-1)
  47. def process(self,input):
  48. loc_out = self.loc_model.forward(input, quantize=1, layout = "chw") # retinaface decoder only support chw layout
  49. 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]])
  50. for i,box in enumerate(boxes):
  51. landmark = landmarks[i][:6]
  52. reg_in = input.crop_affine(landmark , 94 , 24)
  53. reg_out = self.reg_model.forward(reg_in , quantize=1, layout = "chw")
  54. LP_number = self.reg_decoder.run(reg_out)
  55. string_LP = ''
  56. for id in LP_number:
  57. string_LP += self.chars[id]
  58. self.draw_string(input , box[0], box[1] , string_LP ,color=(225,0,0))
  59. self.draw_paste(input , reg_in)
  60. self.draw_rectangle(input,box)
  61. self.draw_point(input , landmarks[i])
  62. def lcdRotation(inputImg,rotationAngle):
  63. from maix import image
  64. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  65. if ScreenOrientation:
  66. imgRotationAim = image.new(size = (240, 320))
  67. else:
  68. imgRotationAim = image.new(size = (320, 240))
  69. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  70. if ScreenOrientation:
  71. CAMERAROTATE = +180
  72. else:
  73. CAMERAROTATE = +90
  74. def main():
  75. from maix import display, camera , image
  76. image.load_freetype("/home/res/sans.ttf")
  77. app = LPR()
  78. canvasImg = image.new(size = (240, 320))
  79. while True:
  80. canvasImg.clear()
  81. img = camera.capture().crop(0, 0,224, 224)
  82. app.process(img)
  83. canvasImg.draw_image(img,48,8)
  84. canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  85. display.show(canvasImg)
  86. # break
  87. main()