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.5 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
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. res, confidence = e2e.recognizeOne(origin_plate)
  87. print "res",res
  88. cv2.imwrite("./"+str(j)+"_rough.jpg",plate)
  89. # print "车牌类型:",ptype
  90. # plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
  91. plate =cv2.resize(plate,(136,int(36*2.5)))
  92. t1 = time.time()
  93. ptype = td.SimplePredict(plate)
  94. if ptype>0 and ptype<4:
  95. plate = cv2.bitwise_not(plate)
  96. # demo = verticalEdgeDetection(plate)
  97. image_rgb = fm.findContoursAndDrawBoundingBox(plate)
  98. image_rgb = fv.finemappingVertical(image_rgb)
  99. cache.verticalMappingToFolder(image_rgb)
  100. # print time.time() - t1,"校正"
  101. print "e2e:",e2e.recognizeOne(image_rgb)[0]
  102. image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
  103. cv2.imwrite("./"+str(j)+".jpg",image_gray)
  104. # image_gray = horizontalSegmentation(image_gray)
  105. t2 = time.time()
  106. res, confidence = e2e.recognizeOne(image_rgb)
  107. res_json = {}
  108. if confidence > 0.6:
  109. res_json["Name"] = res
  110. res_json["Type"] = td.plateType[ptype]
  111. res_json["Confidence"] = confidence;
  112. res_json["x"] = int(rect[0])
  113. res_json["y"] = int(rect[1])
  114. res_json["w"] = int(rect[2])
  115. res_json["h"] = int(rect[3])
  116. jsons.append(res_json)
  117. print json.dumps(jsons,ensure_ascii=False,encoding="gb2312")
  118. return json.dumps(jsons,ensure_ascii=False,encoding="gb2312")
  119. def SimpleRecognizePlateByE2E(image):
  120. t0 = time.time()
  121. images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
  122. res_set = []
  123. for j,plate in enumerate(images):
  124. plate, rect, origin_plate =plate
  125. # plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
  126. plate =cv2.resize(plate,(136,36*2))
  127. res,confidence = e2e.recognizeOne(origin_plate)
  128. print "res",res
  129. t1 = time.time()
  130. ptype = td.SimplePredict(plate)
  131. if ptype>0 and ptype<5:
  132. # pass
  133. plate = cv2.bitwise_not(plate)
  134. image_rgb = fm.findContoursAndDrawBoundingBox(plate)
  135. image_rgb = fv.finemappingVertical(image_rgb)
  136. image_rgb = fv.finemappingVertical(image_rgb)
  137. cache.verticalMappingToFolder(image_rgb)
  138. #cv2.imwrite("./"+str(j)+".jpg",image_rgb)
  139. res,confidence = e2e.recognizeOne(image_rgb)
  140. print res,confidence
  141. res_set.append([[],res,confidence])
  142. if confidence>0.7:
  143. image = drawRectBox(image, rect, res+" "+str(round(confidence,3)))
  144. return image,res_set
  145. def SimpleRecognizePlate(image):
  146. t0 = time.time()
  147. images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
  148. res_set = []
  149. for j,plate in enumerate(images):
  150. plate, rect, origin_plate =plate
  151. # plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
  152. plate =cv2.resize(plate,(136,36*2))
  153. t1 = time.time()
  154. ptype = td.SimplePredict(plate)
  155. if ptype>0 and ptype<5:
  156. plate = cv2.bitwise_not(plate)
  157. image_rgb = fm.findContoursAndDrawBoundingBox(plate)
  158. image_rgb = fv.finemappingVertical(image_rgb)
  159. cache.verticalMappingToFolder(image_rgb)
  160. print "e2e:", e2e.recognizeOne(image_rgb)
  161. image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_RGB2GRAY)
  162. # image_gray = horizontalSegmentation(image_gray)
  163. cv2.imshow("image_gray",image_gray)
  164. # cv2.waitKey()
  165. cv2.imwrite("./"+str(j)+".jpg",image_gray)
  166. # cv2.imshow("image",image_gray)
  167. # cv2.waitKey(0)
  168. print "校正",time.time() - t1,"s"
  169. # cv2.imshow("image,",image_gray)
  170. # cv2.waitKey(0)
  171. t2 = time.time()
  172. val = segmentation.slidingWindowsEval(image_gray)
  173. # print val
  174. print "分割和识别",time.time() - t2,"s"
  175. if len(val)==3:
  176. blocks, res, confidence = val
  177. if confidence/7>0.7:
  178. image = drawRectBox(image,rect,res)
  179. res_set.append(res)
  180. for i,block in enumerate(blocks):
  181. block_ = cv2.resize(block,(25,25))
  182. block_ = cv2.cvtColor(block_,cv2.COLOR_GRAY2BGR)
  183. image[j * 25:(j * 25) + 25, i * 25:(i * 25) + 25] = block_
  184. if image[j*25:(j*25)+25,i*25:(i*25)+25].shape == block_.shape:
  185. pass
  186. if confidence>0:
  187. print "车牌:",res,"置信度:",confidence/7
  188. else:
  189. pass
  190. # print "不确定的车牌:", res, "置信度:", confidence
  191. print time.time() - t0,"s"
  192. return image,res_set