Beispiel #1
0
    def __init__(self, input_file):

        self.input_file = input_file
        self.display_image = None
        self.output_file = input_file
        self.r_angle = 0
        self.skew_obj = SkewDetect(self.input_file)
Beispiel #2
0
    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)
Beispiel #3
0
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']
        print(angle)

        #rot_angle = 0
        if angle < 0 and angle >= -45:
            rot_angle = angle
        elif angle < -45 and angle > -90:
            rot_angle = angle + 90
        elif angle > 0 and angle <= 90:
            rot_angle = angle - 90
        else:
            rot_angle = 0
        '''
        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)
        sh = rotated.shape
        rotated = rotated[10:10 + sh[0] - 20, 5:5 + sh[1] - 10]

        if self.display_image:
            self.display(rotated)

        if self.output_file:
            self.saveImage(rotated * 255)
        #return 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()
Beispiel #4
0
class Deskew:

    def __init__(self, input_file):

        self.input_file = input_file
        self.display_image = None
        self.output_file = input_file
        self.r_angle = 0
        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']
        print('Estimated Angle ',angle)

        if angle >= 0 and angle <= 90:
            rot_angle = angle - 90 + self.r_angle
        if angle >= -45 and angle < 0:
            rot_angle = angle + self.r_angle
        if angle >= -90 and angle < -45:
            rot_angle = 90 + angle + self.r_angle

        pilImage = Image.open(self.input_file)
        pilImage = pilImage.rotate(rot_angle,expand=True)
        pilImage.save(self.input_file)

        """
        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()
Beispiel #5
0
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)
        #print(img.shape)
        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, cval=1)

        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()
Beispiel #6
0
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()
Beispiel #7
0
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, input_file=None):

        if (input_file):
            img = io.imread(input_file)
        else:
            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.saveTemp(rotated * 255)
        return (res, rotated * 255)

    def saveImage(self, img):
        path = self.skew_obj.check_path(self.output_file)
        os.remove("temp.jpg")
        io.imsave(path, img.astype(np.uint8))

    def saveTemp(self, img):
        path = "temp.jpg"
        io.imsave(path, img.astype(np.uint8))

    def display(self, img):

        plt.imshow(img)
        plt.show()

    def run(self):

        if self.input_file:
            res_p, rotated = self.deskew()
            res_c, rotated = self.deskew(input_file="temp.jpg")
            count = 1
            while (res_p["Estimated Angle"] != res_c["Estimated Angle"]):
                res_p = res_c
                res_c, rotated = self.deskew(input_file="temp.jpg")
                count += 1
                print(count)
            self.saveImage(rotated)