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 8.2 kB

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