From 27deae427d3e32e10d969a5af01843c2ed3ab93d Mon Sep 17 00:00:00 2001 From: Python3pkg Date: Wed, 17 May 2017 22:28:22 -0700 Subject: [PATCH 1/3] Convert to python3 --- alyn/__init__.py | 4 +- alyn/__init__.py.bak | 3 + alyn/deskew.py | 2 +- alyn/deskew.py.bak | 90 +++++++++++++ alyn/skew_detect.py | 4 +- alyn/skew_detect.py.bak | 277 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 375 insertions(+), 5 deletions(-) create mode 100644 alyn/__init__.py.bak create mode 100755 alyn/deskew.py.bak create mode 100755 alyn/skew_detect.py.bak diff --git a/alyn/__init__.py b/alyn/__init__.py index 1b430f8..01c27a0 100644 --- a/alyn/__init__.py +++ b/alyn/__init__.py @@ -1,3 +1,3 @@ """ Import required modules""" -from deskew import * -from skew_detect import * +from .deskew import * +from .skew_detect import * diff --git a/alyn/__init__.py.bak b/alyn/__init__.py.bak new file mode 100644 index 0000000..1b430f8 --- /dev/null +++ b/alyn/__init__.py.bak @@ -0,0 +1,3 @@ +""" Import required modules""" +from deskew import * +from skew_detect import * diff --git a/alyn/deskew.py b/alyn/deskew.py index 029be4f..605b601 100755 --- a/alyn/deskew.py +++ b/alyn/deskew.py @@ -3,7 +3,7 @@ import numpy as np import matplotlib.pyplot as plt -from skew_detect import SkewDetect +from .skew_detect import SkewDetect from skimage import io from skimage.transform import rotate diff --git a/alyn/deskew.py.bak b/alyn/deskew.py.bak new file mode 100755 index 0000000..029be4f --- /dev/null +++ b/alyn/deskew.py.bak @@ -0,0 +1,90 @@ +""" Deskews file after getting skew angle """ +import optparse +import numpy as np +import matplotlib.pyplot as plt + +from skew_detect import SkewDetect +from skimage import io +from skimage.transform import rotate + + +class Deskew: + + def __init__(self, input_file, display_image, output_file, r_angle): + + self.input_file = input_file + self.display_image = display_image + self.output_file = output_file + self.r_angle = r_angle + self.skew_obj = SkewDetect(self.input_file) + + def deskew(self): + + img = io.imread(self.input_file) + res = self.skew_obj.process_single_file() + angle = res['Estimated Angle'] + + if angle >= 0 and angle <= 90: + rot_angle = angle - 90 + self.r_angle + if angle >= -45 and angle < 0: + rot_angle = angle - 90 + self.r_angle + if angle >= -90 and angle < -45: + rot_angle = 90 + angle + self.r_angle + + rotated = rotate(img, rot_angle, resize=True) + + if self.display_image: + self.display(rotated) + + if self.output_file: + self.saveImage(rotated*255) + + def saveImage(self, img): + path = self.skew_obj.check_path(self.output_file) + io.imsave(path, img.astype(np.uint8)) + + def display(self, img): + + plt.imshow(img) + plt.show() + + def run(self): + + if self.input_file: + self.deskew() + + +if __name__ == '__main__': + + parser = optparse.OptionParser() + + parser.add_option( + '-i', + '--input', + default=None, + dest='input_file', + help='Input file name') + parser.add_option( + '-d', '--display', + default=None, + dest='display_image', + help="display the rotated image") + parser.add_option( + '-o', '--output', + default=None, + dest='output_file', + help='Output file name') + parser.add_option( + '-r', '--rotate', + default=0, + dest='r_angle', + help='Rotate the image to desired axis', + type=int) + options, args = parser.parse_args() + deskew_obj = Deskew( + options.input_file, + options.display_image, + options.output_file, + options.r_angle) + + deskew_obj.run() diff --git a/alyn/skew_detect.py b/alyn/skew_detect.py index 2612102..f1fb2e4 100755 --- a/alyn/skew_detect.py +++ b/alyn/skew_detect.py @@ -77,7 +77,7 @@ def compare_sum(self, value): def display(self, data): for i in data: - print i + ": " + str(data[i]) + print(i + ": " + str(data[i])) def calculate_deviation(self, angle): @@ -104,7 +104,7 @@ def run(self): if self.batch_path: self.batch_process() else: - print "Invalid input, nothing to process." + print("Invalid input, nothing to process.") else: self.process_single_file() diff --git a/alyn/skew_detect.py.bak b/alyn/skew_detect.py.bak new file mode 100755 index 0000000..2612102 --- /dev/null +++ b/alyn/skew_detect.py.bak @@ -0,0 +1,277 @@ +""" Calculates skew angle """ +import os +import imghdr +import optparse + +import numpy as np +import matplotlib.pyplot as plt +from skimage import io +from skimage.feature import canny +from skimage.color import rgb2gray +from skimage.transform import hough_line, hough_line_peaks + + +class SkewDetect: + + piby4 = np.pi / 4 + + def __init__( + self, + input_file=None, + batch_path=None, + output_file=None, + sigma=3.0, + display_output=None, + num_peaks=20, + plot_hough=None + ): + + self.sigma = sigma + self.input_file = input_file + self.batch_path = batch_path + self.output_file = output_file + self.display_output = display_output + self.num_peaks = num_peaks + self.plot_hough = plot_hough + + def write_to_file(self, wfile, data): + + for d in data: + wfile.write(d + ': ' + str(data[d]) + '\n') + wfile.write('\n') + + def get_max_freq_elem(self, arr): + + max_arr = [] + freqs = {} + for i in arr: + if i in freqs: + freqs[i] += 1 + else: + freqs[i] = 1 + + sorted_keys = sorted(freqs, key=freqs.get, reverse=True) + max_freq = freqs[sorted_keys[0]] + + for k in sorted_keys: + if freqs[k] == max_freq: + max_arr.append(k) + + return max_arr + + def display_hough(self, h, a, d): + + plt.imshow( + np.log(1 + h), + extent=[np.rad2deg(a[-1]), np.rad2deg(a[0]), d[-1], d[0]], + cmap=plt.cm.gray, + aspect=1.0 / 90) + plt.show() + + def compare_sum(self, value): + if value >= 44 and value <= 46: + return True + else: + return False + + def display(self, data): + + for i in data: + print i + ": " + str(data[i]) + + def calculate_deviation(self, angle): + + angle_in_degrees = np.abs(angle) + deviation = np.abs(SkewDetect.piby4 - angle_in_degrees) + + return deviation + + def run(self): + + if self.display_output: + if self.display_output.lower() == 'yes': + self.display_output = True + else: + self.display_output = False + + if self.plot_hough: + if self.plot_hough.lower() == 'yes': + self.plot_hough = True + else: + self.plot_hough = False + + if self.input_file is None: + if self.batch_path: + self.batch_process() + else: + print "Invalid input, nothing to process." + else: + self.process_single_file() + + def check_path(self, path): + + if os.path.isabs(path): + full_path = path + else: + full_path = os.getcwd() + '/' + str(path) + return full_path + + def process_single_file(self): + + file_path = self.check_path(self.input_file) + res = self.determine_skew(file_path) + + if self.output_file: + output_path = self.check_path(self.output_file) + wfile = open(output_path, 'w') + self.write_to_file(wfile, res) + wfile.close() + + return res + + def batch_process(self): + + wfile = None + + if self.batch_path == '.': + self.batch_path = '' + + abs_path = self.check_path(self.batch_path) + files = os.listdir(abs_path) + + if self.output_file: + out_path = self.check_path(self.output_file) + wfile = open(file_path, 'w') + + for f in files: + file_path = abs_path + '/' + f + if os.path.isdir(file_path): + continue + if imghdr.what(file_path): + res = self.determine_skew(file_path) + if wfile: + self.write_to_file(wfile, res) + if wfile: + wfile.close() + + def determine_skew(self, img_file): + + img = io.imread(img_file, as_grey=True) + edges = canny(img, sigma=self.sigma) + h, a, d = hough_line(edges) + _, ap, _ = hough_line_peaks(h, a, d, num_peaks=self.num_peaks) + + if len(ap) == 0: + return {"Image File": img_file, "Message": "Bad Quality"} + + absolute_deviations = [self.calculate_deviation(k) for k in ap] + average_deviation = np.mean(np.rad2deg(absolute_deviations)) + ap_deg = [np.rad2deg(x) for x in ap] + + bin_0_45 = [] + bin_45_90 = [] + bin_0_45n = [] + bin_45_90n = [] + + for ang in ap_deg: + + deviation_sum = int(90 - ang + average_deviation) + if self.compare_sum(deviation_sum): + bin_45_90.append(ang) + continue + + deviation_sum = int(ang + average_deviation) + if self.compare_sum(deviation_sum): + bin_0_45.append(ang) + continue + + deviation_sum = int(-ang + average_deviation) + if self.compare_sum(deviation_sum): + bin_0_45n.append(ang) + continue + + deviation_sum = int(90 + ang + average_deviation) + if self.compare_sum(deviation_sum): + bin_45_90n.append(ang) + + angles = [bin_0_45, bin_45_90, bin_0_45n, bin_45_90n] + lmax = 0 + + for j in range(len(angles)): + l = len(angles[j]) + if l > lmax: + lmax = l + maxi = j + + if lmax: + ans_arr = self.get_max_freq_elem(angles[maxi]) + ans_res = np.mean(ans_arr) + + else: + ans_arr = self.get_max_freq_elem(ap_deg) + ans_res = np.mean(ans_arr) + + data = { + "Image File": img_file, + "Average Deviation from pi/4": average_deviation, + "Estimated Angle": ans_res, + "Angle bins": angles} + + if self.display_output: + self.display(data) + + if self.plot_hough: + self.display_hough(h, a, d) + return data + +if __name__ == '__main__': + + parser = optparse.OptionParser() + + parser.add_option( + '-b', '--batch', + default=None, + dest='batch_path', + help='Path for batch processing') + parser.add_option( + '-d', '--display', + default=None, + dest='display_output', + help='Display logs') + parser.add_option( + '-i', '--input', + default=None, + dest='input_file', + help='Input file name') + parser.add_option( + '-n', '--num', + default=20, + dest='num_peaks', + help='Number of Hough Transform peaks', + type=int) + parser.add_option( + '-o', '--output', + default=None, + dest='output_file', + help='Output file name') + parser.add_option( + '-p', '--plot', + default=None, + dest='plot_hough', + help='Plot the Hough Transform') + parser.add_option( + '-s', '--sigma', + default=3.0, + dest='sigma', + help='Sigma for Canny Edge Detection', + type=float) + options, args = parser.parse_args() + skew_obj = SkewDetect( + options.input_file, + options.batch_path, + options.output_file, + options.sigma, + options.display_output, + options.num_peaks, + options.plot_hough) + skew_obj.run() From a3f83ca814deceaf7015186faebc239a33ce345d Mon Sep 17 00:00:00 2001 From: tender Date: Mon, 23 Jul 2018 16:05:46 +0800 Subject: [PATCH 2/3] Remove bak files --- alyn/__init__.py.bak | 3 - alyn/deskew.py.bak | 90 ------------- alyn/skew_detect.py.bak | 277 ---------------------------------------- 3 files changed, 370 deletions(-) delete mode 100644 alyn/__init__.py.bak delete mode 100755 alyn/deskew.py.bak delete mode 100755 alyn/skew_detect.py.bak diff --git a/alyn/__init__.py.bak b/alyn/__init__.py.bak deleted file mode 100644 index 1b430f8..0000000 --- a/alyn/__init__.py.bak +++ /dev/null @@ -1,3 +0,0 @@ -""" Import required modules""" -from deskew import * -from skew_detect import * diff --git a/alyn/deskew.py.bak b/alyn/deskew.py.bak deleted file mode 100755 index 029be4f..0000000 --- a/alyn/deskew.py.bak +++ /dev/null @@ -1,90 +0,0 @@ -""" Deskews file after getting skew angle """ -import optparse -import numpy as np -import matplotlib.pyplot as plt - -from skew_detect import SkewDetect -from skimage import io -from skimage.transform import rotate - - -class Deskew: - - def __init__(self, input_file, display_image, output_file, r_angle): - - self.input_file = input_file - self.display_image = display_image - self.output_file = output_file - self.r_angle = r_angle - self.skew_obj = SkewDetect(self.input_file) - - def deskew(self): - - img = io.imread(self.input_file) - res = self.skew_obj.process_single_file() - angle = res['Estimated Angle'] - - if angle >= 0 and angle <= 90: - rot_angle = angle - 90 + self.r_angle - if angle >= -45 and angle < 0: - rot_angle = angle - 90 + self.r_angle - if angle >= -90 and angle < -45: - rot_angle = 90 + angle + self.r_angle - - rotated = rotate(img, rot_angle, resize=True) - - if self.display_image: - self.display(rotated) - - if self.output_file: - self.saveImage(rotated*255) - - def saveImage(self, img): - path = self.skew_obj.check_path(self.output_file) - io.imsave(path, img.astype(np.uint8)) - - def display(self, img): - - plt.imshow(img) - plt.show() - - def run(self): - - if self.input_file: - self.deskew() - - -if __name__ == '__main__': - - parser = optparse.OptionParser() - - parser.add_option( - '-i', - '--input', - default=None, - dest='input_file', - help='Input file name') - parser.add_option( - '-d', '--display', - default=None, - dest='display_image', - help="display the rotated image") - parser.add_option( - '-o', '--output', - default=None, - dest='output_file', - help='Output file name') - parser.add_option( - '-r', '--rotate', - default=0, - dest='r_angle', - help='Rotate the image to desired axis', - type=int) - options, args = parser.parse_args() - deskew_obj = Deskew( - options.input_file, - options.display_image, - options.output_file, - options.r_angle) - - deskew_obj.run() diff --git a/alyn/skew_detect.py.bak b/alyn/skew_detect.py.bak deleted file mode 100755 index 2612102..0000000 --- a/alyn/skew_detect.py.bak +++ /dev/null @@ -1,277 +0,0 @@ -""" Calculates skew angle """ -import os -import imghdr -import optparse - -import numpy as np -import matplotlib.pyplot as plt -from skimage import io -from skimage.feature import canny -from skimage.color import rgb2gray -from skimage.transform import hough_line, hough_line_peaks - - -class SkewDetect: - - piby4 = np.pi / 4 - - def __init__( - self, - input_file=None, - batch_path=None, - output_file=None, - sigma=3.0, - display_output=None, - num_peaks=20, - plot_hough=None - ): - - self.sigma = sigma - self.input_file = input_file - self.batch_path = batch_path - self.output_file = output_file - self.display_output = display_output - self.num_peaks = num_peaks - self.plot_hough = plot_hough - - def write_to_file(self, wfile, data): - - for d in data: - wfile.write(d + ': ' + str(data[d]) + '\n') - wfile.write('\n') - - def get_max_freq_elem(self, arr): - - max_arr = [] - freqs = {} - for i in arr: - if i in freqs: - freqs[i] += 1 - else: - freqs[i] = 1 - - sorted_keys = sorted(freqs, key=freqs.get, reverse=True) - max_freq = freqs[sorted_keys[0]] - - for k in sorted_keys: - if freqs[k] == max_freq: - max_arr.append(k) - - return max_arr - - def display_hough(self, h, a, d): - - plt.imshow( - np.log(1 + h), - extent=[np.rad2deg(a[-1]), np.rad2deg(a[0]), d[-1], d[0]], - cmap=plt.cm.gray, - aspect=1.0 / 90) - plt.show() - - def compare_sum(self, value): - if value >= 44 and value <= 46: - return True - else: - return False - - def display(self, data): - - for i in data: - print i + ": " + str(data[i]) - - def calculate_deviation(self, angle): - - angle_in_degrees = np.abs(angle) - deviation = np.abs(SkewDetect.piby4 - angle_in_degrees) - - return deviation - - def run(self): - - if self.display_output: - if self.display_output.lower() == 'yes': - self.display_output = True - else: - self.display_output = False - - if self.plot_hough: - if self.plot_hough.lower() == 'yes': - self.plot_hough = True - else: - self.plot_hough = False - - if self.input_file is None: - if self.batch_path: - self.batch_process() - else: - print "Invalid input, nothing to process." - else: - self.process_single_file() - - def check_path(self, path): - - if os.path.isabs(path): - full_path = path - else: - full_path = os.getcwd() + '/' + str(path) - return full_path - - def process_single_file(self): - - file_path = self.check_path(self.input_file) - res = self.determine_skew(file_path) - - if self.output_file: - output_path = self.check_path(self.output_file) - wfile = open(output_path, 'w') - self.write_to_file(wfile, res) - wfile.close() - - return res - - def batch_process(self): - - wfile = None - - if self.batch_path == '.': - self.batch_path = '' - - abs_path = self.check_path(self.batch_path) - files = os.listdir(abs_path) - - if self.output_file: - out_path = self.check_path(self.output_file) - wfile = open(file_path, 'w') - - for f in files: - file_path = abs_path + '/' + f - if os.path.isdir(file_path): - continue - if imghdr.what(file_path): - res = self.determine_skew(file_path) - if wfile: - self.write_to_file(wfile, res) - if wfile: - wfile.close() - - def determine_skew(self, img_file): - - img = io.imread(img_file, as_grey=True) - edges = canny(img, sigma=self.sigma) - h, a, d = hough_line(edges) - _, ap, _ = hough_line_peaks(h, a, d, num_peaks=self.num_peaks) - - if len(ap) == 0: - return {"Image File": img_file, "Message": "Bad Quality"} - - absolute_deviations = [self.calculate_deviation(k) for k in ap] - average_deviation = np.mean(np.rad2deg(absolute_deviations)) - ap_deg = [np.rad2deg(x) for x in ap] - - bin_0_45 = [] - bin_45_90 = [] - bin_0_45n = [] - bin_45_90n = [] - - for ang in ap_deg: - - deviation_sum = int(90 - ang + average_deviation) - if self.compare_sum(deviation_sum): - bin_45_90.append(ang) - continue - - deviation_sum = int(ang + average_deviation) - if self.compare_sum(deviation_sum): - bin_0_45.append(ang) - continue - - deviation_sum = int(-ang + average_deviation) - if self.compare_sum(deviation_sum): - bin_0_45n.append(ang) - continue - - deviation_sum = int(90 + ang + average_deviation) - if self.compare_sum(deviation_sum): - bin_45_90n.append(ang) - - angles = [bin_0_45, bin_45_90, bin_0_45n, bin_45_90n] - lmax = 0 - - for j in range(len(angles)): - l = len(angles[j]) - if l > lmax: - lmax = l - maxi = j - - if lmax: - ans_arr = self.get_max_freq_elem(angles[maxi]) - ans_res = np.mean(ans_arr) - - else: - ans_arr = self.get_max_freq_elem(ap_deg) - ans_res = np.mean(ans_arr) - - data = { - "Image File": img_file, - "Average Deviation from pi/4": average_deviation, - "Estimated Angle": ans_res, - "Angle bins": angles} - - if self.display_output: - self.display(data) - - if self.plot_hough: - self.display_hough(h, a, d) - return data - -if __name__ == '__main__': - - parser = optparse.OptionParser() - - parser.add_option( - '-b', '--batch', - default=None, - dest='batch_path', - help='Path for batch processing') - parser.add_option( - '-d', '--display', - default=None, - dest='display_output', - help='Display logs') - parser.add_option( - '-i', '--input', - default=None, - dest='input_file', - help='Input file name') - parser.add_option( - '-n', '--num', - default=20, - dest='num_peaks', - help='Number of Hough Transform peaks', - type=int) - parser.add_option( - '-o', '--output', - default=None, - dest='output_file', - help='Output file name') - parser.add_option( - '-p', '--plot', - default=None, - dest='plot_hough', - help='Plot the Hough Transform') - parser.add_option( - '-s', '--sigma', - default=3.0, - dest='sigma', - help='Sigma for Canny Edge Detection', - type=float) - options, args = parser.parse_args() - skew_obj = SkewDetect( - options.input_file, - options.batch_path, - options.output_file, - options.sigma, - options.display_output, - options.num_peaks, - options.plot_hough) - skew_obj.run() From 5c64ca62d36cb1ec6b1f013ac31e5c19b1dcafb1 Mon Sep 17 00:00:00 2001 From: raliclo Date: Tue, 31 Jul 2018 13:25:46 +0800 Subject: [PATCH 3/3] Add from __future__ import print_function --- alyn/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/alyn/__init__.py b/alyn/__init__.py index 01c27a0..d8b8d4b 100644 --- a/alyn/__init__.py +++ b/alyn/__init__.py @@ -1,3 +1,4 @@ """ Import required modules""" -from .deskew import * -from .skew_detect import * +from deskew import * +from skew_detect import * +from __future__ import print_function