table_ceil.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 9 23:11:51 2020
  5. table ceil
  6. @author: chineseocr
  7. """
  8. import cv2
  9. import numpy as np
  10. from utils import minAreaRectbox,measure,eval_angle,draw_lines
  11. from table_detect import table_detect
  12. from table_line import table_line
  13. class table:
  14. def __init__(self, img, tableSize=(416, 416), tableLineSize=(1024,1024), isTableDetect=False):
  15. self.img = img
  16. self.tableSize = tableSize
  17. self.tableLineSize = tableLineSize
  18. self.isTableDetect = isTableDetect
  19. self.img_degree()
  20. self.table_boxes_detect() # 表格定位
  21. self.table_ceil() # 表格单元格定位
  22. self.table_ocr() # 表格文字识别
  23. def img_degree(self):
  24. img,degree =eval_angle(self.img,angleRange=[-15,15])
  25. self.img = img
  26. self.degree = degree
  27. def table_boxes_detect(self):
  28. h,w = self.img.shape[:2]
  29. if self.isTableDetect:
  30. boxes,adBoxes,scores=table_detect(self.img, sc=self.tableSize,thresh=0.2,NMSthresh=0.3)
  31. if len(boxes)==0:
  32. boxes = [[0,0,w,h]]
  33. adBoxes = [[0,0,w,h]]
  34. scores =[0]
  35. else:
  36. boxes = [[0,0,w,h]]
  37. adBoxes = [[0,0,w,h]]
  38. scores =[0]
  39. self.boxes = boxes
  40. self.adBoxes = adBoxes
  41. self.scores = scores
  42. def table_ceil(self):
  43. # 表格单元格
  44. n = len(self.adBoxes)
  45. self.tableCeilBoxes = []
  46. self.childImgs=[]
  47. for i in range(n):
  48. xmin, ymin, xmax, ymax = [int(x) for x in self.adBoxes[i]]
  49. childImg = self.img[ymin:ymax,xmin:xmax]
  50. rowboxes, colboxes = table_line(childImg[..., ::-1], size=self.tableLineSize,
  51. hprob=0.5, vprob=0.5)
  52. tmp = np.zeros(self.img.shape[:2], dtype='uint8')
  53. tmp = draw_lines(tmp, rowboxes+colboxes, color=255, lineW=2)
  54. labels = measure.label(tmp < 255, connectivity=2) # 8连通区域标记
  55. regions = measure.regionprops(labels)
  56. ceilboxes = minAreaRectbox(regions, False, tmp.shape[1], tmp.shape[0], True, True)
  57. ceilboxes = np.array(ceilboxes)
  58. ceilboxes[:, [0, 2, 4, 6]] += xmin
  59. ceilboxes[:, [1, 3, 5, 7]] += ymin
  60. self.tableCeilBoxes.extend(ceilboxes)
  61. self.childImgs.append(childImg)
  62. def table_ocr(self):
  63. pass
  64. if __name__=='__main__':
  65. import time
  66. from utils import draw_boxes
  67. p = 'img/table-detect.jpg'
  68. img = cv2.imread(p)
  69. t = time.time()
  70. tableDetect = table(img)
  71. tableCeilBoxes = tableDetect.tableCeilBoxes
  72. img = tableDetect.img
  73. tmp = np.zeros_like(img)
  74. img = draw_boxes(tmp, tableDetect.tableCeilBoxes,color=(255, 255, 255))
  75. print(time.time()-t)
  76. cv2.imwrite('img/table-ceil.png', img)