You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pipline.py 7.0 kB

7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #coding=utf-8
  2. import detect
  3. import finemapping as fm
  4. import segmentation
  5. import cv2
  6. import time
  7. import numpy as np
  8. from PIL import ImageFont
  9. from PIL import Image
  10. from PIL import ImageDraw
  11. import json
  12. import sys
  13. import typeDistinguish as td
  14. reload(sys)
  15. sys.setdefaultencoding("utf-8")
  16. fontC = ImageFont.truetype("./Font/platech.ttf", 14, 0);
  17. #寻找车牌左右边界
  18. def find_edge(image):
  19. sum_i = image.sum(axis=0)
  20. sum_i = sum_i.astype(np.float)
  21. sum_i/=image.shape[0]*255
  22. # print sum_i
  23. start= 0 ;
  24. end = image.shape[1]-1
  25. for i,one in enumerate(sum_i):
  26. if one>0.4:
  27. start = i;
  28. if start-3<0:
  29. start = 0
  30. else:
  31. start -=3
  32. break;
  33. for i,one in enumerate(sum_i[::-1]):
  34. if one>0.4:
  35. end = end - i;
  36. if end+4>image.shape[1]-1:
  37. end = image.shape[1]-1
  38. else:
  39. end+=4
  40. break
  41. return start,end
  42. #垂直边缘检测
  43. def verticalEdgeDetection(image):
  44. image_sobel = cv2.Sobel(image.copy(),cv2.CV_8U,1,0)
  45. # image = auto_canny(image_sobel)
  46. # img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT
  47. # canny_image = auto_canny(image)
  48. flag,thres = cv2.threshold(image_sobel,0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY)
  49. print flag
  50. flag,thres = cv2.threshold(image_sobel,int(flag*0.7),255,cv2.THRESH_BINARY)
  51. # thres = simpleThres(image_sobel)
  52. kernal = np.ones(shape=(3,15))
  53. thres = cv2.morphologyEx(thres,cv2.MORPH_CLOSE,kernal)
  54. return thres
  55. #确定粗略的左右边界
  56. def horizontalSegmentation(image):
  57. thres = verticalEdgeDetection(image)
  58. # thres = thres*image
  59. head,tail = find_edge(thres)
  60. # print head,tail
  61. # cv2.imshow("edge",thres)
  62. tail = tail+5
  63. if tail>135:
  64. tail = 135
  65. image = image[0:35,head:tail]
  66. image = cv2.resize(image, (int(136), int(36)))
  67. return image
  68. #打上boundingbox和标签
  69. def drawRectBox(image,rect,addText):
  70. cv2.rectangle(image, (int(rect[0]), int(rect[1])), (int(rect[0] + rect[2]), int(rect[1] + rect[3])), (0,0, 255), 2,cv2.LINE_AA)
  71. cv2.rectangle(image, (int(rect[0]-1), int(rect[1])-16), (int(rect[0] + 80), int(rect[1])), (0, 0, 255), -1,
  72. cv2.LINE_AA)
  73. img = Image.fromarray(image)
  74. draw = ImageDraw.Draw(img)
  75. draw.text((int(rect[0]+1), int(rect[1]-16)), addText.decode("utf-8"), (255, 255, 255), font=fontC)
  76. imagex = np.array(img)
  77. return imagex
  78. import cache
  79. import finemapping_vertical as fv
  80. def RecognizePlateJson(image):
  81. images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
  82. jsons = []
  83. for j,plate in enumerate(images):
  84. plate,rect,origin_plate =plate
  85. cv2.imwrite("./"+str(j)+"_rough.jpg",plate)
  86. # print "车牌类型:",ptype
  87. # plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
  88. plate =cv2.resize(plate,(136,int(36*2.5)))
  89. t1 = time.time()
  90. ptype = td.SimplePredict(plate)
  91. if ptype>0 and ptype<5:
  92. plate = cv2.bitwise_not(plate)
  93. # demo = verticalEdgeDetection(plate)
  94. image_rgb = fm.findContoursAndDrawBoundingBox(plate)
  95. image_rgb = fv.finemappingVertical(image_rgb)
  96. cache.verticalMappingToFolder(image_rgb)
  97. # print time.time() - t1,"校正"
  98. image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
  99. cv2.imwrite("./"+str(j)+".jpg",image_gray)
  100. # image_gray = horizontalSegmentation(image_gray)
  101. t2 = time.time()
  102. val = segmentation.slidingWindowsEval(image_gray)
  103. if len(val)==3:
  104. blocks, res, confidence = val
  105. if confidence/7>0.0:
  106. image = drawRectBox(image,rect,res)
  107. for i,block in enumerate(blocks):
  108. block_ = cv2.resize(block,(25,25))
  109. block_ = cv2.cvtColor(block_,cv2.COLOR_GRAY2BGR)
  110. image[j * 25:(j * 25) + 25, i * 25:(i * 25) + 25] = block_
  111. if image[j*25:(j*25)+25,i*25:(i*25)+25].shape == block_.shape:
  112. pass
  113. plate_name = res
  114. res_json = {}
  115. if confidence/7>0.0:
  116. res_json["Name"] = plate_name.decode()
  117. res_json["Type"] = td.plateType[ptype]
  118. res_json["Confidence"] = confidence/7;
  119. res_json["x"] = int(rect[0])
  120. res_json["y"] = int(rect[1])
  121. res_json["w"] = int(rect[2])
  122. res_json["h"] = int(rect[3])
  123. # print "车牌:",res,"置信度:",confidence/7
  124. jsons.append(res_json)
  125. else:
  126. pass
  127. # print "不确定的车牌:", res, "置信度:", confidence
  128. print jsons
  129. print json.dumps(jsons,ensure_ascii=False,encoding="gb2312")
  130. return json.dumps(jsons,ensure_ascii=False,encoding="gb2312")
  131. def SimpleRecognizePlate(image):
  132. t0 = time.time()
  133. images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
  134. res_set = []
  135. for j,plate in enumerate(images):
  136. plate, rect, origin_plate =plate
  137. # plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
  138. plate =cv2.resize(plate,(136,36*2))
  139. t1 = time.time()
  140. ptype = td.SimplePredict(plate)
  141. if ptype>0 and ptype<5:
  142. plate = cv2.bitwise_not(plate)
  143. image_rgb = fm.findContoursAndDrawBoundingBox(plate)
  144. image_rgb = fv.finemappingVertical(image_rgb)
  145. cache.verticalMappingToFolder(image_rgb)
  146. image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_RGB2GRAY)
  147. # image_gray = horizontalSegmentation(image_gray)
  148. cv2.imshow("image_gray",image_gray)
  149. # cv2.waitKey()
  150. cv2.imwrite("./"+str(j)+".jpg",image_gray)
  151. # cv2.imshow("image",image_gray)
  152. # cv2.waitKey(0)
  153. print "校正",time.time() - t1,"s"
  154. # cv2.imshow("image,",image_gray)
  155. # cv2.waitKey(0)
  156. t2 = time.time()
  157. val = segmentation.slidingWindowsEval(image_gray)
  158. # print val
  159. print "分割和识别",time.time() - t2,"s"
  160. if len(val)==3:
  161. blocks, res, confidence = val
  162. if confidence/7>0.7:
  163. image = drawRectBox(image,rect,res)
  164. res_set.append(res)
  165. for i,block in enumerate(blocks):
  166. block_ = cv2.resize(block,(25,25))
  167. block_ = cv2.cvtColor(block_,cv2.COLOR_GRAY2BGR)
  168. image[j * 25:(j * 25) + 25, i * 25:(i * 25) + 25] = block_
  169. if image[j*25:(j*25)+25,i*25:(i*25)+25].shape == block_.shape:
  170. pass
  171. if confidence>0:
  172. print "车牌:",res,"置信度:",confidence/7
  173. else:
  174. pass
  175. # print "不确定的车牌:", res, "置信度:", confidence
  176. print time.time() - t0,"s"
  177. return image,res_set