Add deepcopy and debug scripts

This commit is contained in:
Vinayak Mehta
2017-04-10 18:59:48 +05:30
parent 4dd0d2330e
commit 84d354ba10
11 changed files with 568 additions and 21 deletions
+15 -9
View File
@@ -2,7 +2,7 @@ import cv2
import numpy as np
def adaptive_threshold(imagename, invert=False):
def adaptive_threshold(imagename, invert=False, blocksize=15, c=-2):
"""Thresholds an image using OpenCV's adaptiveThreshold.
Parameters
@@ -15,6 +15,15 @@ def adaptive_threshold(imagename, invert=False):
tables with lines in background.
(optional, default: False)
blocksize: int
Size of a pixel neighborhood that is used to calculate a
threshold value for the pixel: 3, 5, 7, and so on.
c: float
Constant subtracted from the mean or weighted mean
(see the details below). Normally, it is positive but may be
zero or negative as well.
Returns
-------
img : object
@@ -27,14 +36,11 @@ def adaptive_threshold(imagename, invert=False):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if invert:
threshold = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,
15, -0.2)
threshold = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, blocksize, c)
else:
threshold = cv2.adaptiveThreshold(
np.invert(gray), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
15, -0.2)
threshold = cv2.adaptiveThreshold(np.invert(gray), 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blocksize, c)
return img, threshold
@@ -137,7 +143,7 @@ def find_table_contours(vertical, horizontal):
x, y, w, h = cv2.boundingRect(c_poly)
cont.append((x, y, w, h))
return cont
def find_table_joints(contours, vertical, horizontal):
"""Finds joints/intersections present inside each table boundary.
+4 -1
View File
@@ -1,6 +1,7 @@
from __future__ import division
import os
import sys
import copy
import types
import logging
import copy_reg
@@ -269,7 +270,9 @@ class Lattice:
table_bbox = find_table_joints(contours, vmask, hmask)
if len(self.mtol) == 1 and self.mtol[0] == 2:
mtolerance = self.mtol * len(table_bbox)
mtolerance = copy.deepcopy(self.mtol) * len(table_bbox)
else:
mtolerance = copy.deepcopy(self.mtol)
if self.debug:
self.debug_images = (img, table_bbox)
+6 -3
View File
@@ -1,4 +1,5 @@
import os
import copy
import subprocess
import pyocr
@@ -100,7 +101,9 @@ class OCR:
self.debug_tables = []
if len(self.mtol) == 1 and self.mtol[0] == 2:
self.mtol = self.mtol * len(table_bbox)
mtolerance = copy.deepcopy(self.mtol) * len(table_bbox)
else:
mtolerance = copy.deepcopy(self.mtol)
page = {}
tables = {}
@@ -111,8 +114,8 @@ class OCR:
cols, rows = list(cols), list(rows)
cols.extend([k[0], k[2]])
rows.extend([k[1], k[3]])
cols = merge_close_values(sorted(cols), mtol=self.mtol[table_no])
rows = merge_close_values(sorted(rows, reverse=True), mtol=self.mtol[table_no])
cols = merge_close_values(sorted(cols), mtol=mtolerance[table_no])
rows = merge_close_values(sorted(rows, reverse=True), mtol=mtolerance[table_no])
cols = [(cols[i], cols[i + 1])
for i in range(0, len(cols) - 1)]
rows = [(rows[i], rows[i + 1])
+7 -2
View File
@@ -1,5 +1,6 @@
from __future__ import division
import os
import copy
import types
import logging
import copy_reg
@@ -332,9 +333,13 @@ class Stream:
table_bbox = {(0, 0, width, height): None}
if len(self.ytol) == 1 and self.ytol[0] == 2:
ytolerance = self.ytol * len(table_bbox)
ytolerance = copy.deepcopy(self.ytol) * len(table_bbox)
else:
ytolerance = copy.deepcopy(self.ytol)
if len(self.mtol) == 1 and self.mtol[0] == 0:
mtolerance = self.mtol * len(table_bbox)
mtolerance = copy.deepcopy(self.mtol) * len(table_bbox)
else:
mtolerance = copy.deepcopy(self.mtol)
page = {}
tables = {}