import numpy as np
from matplotlib import pyplot as plt
import cv2


class MarkSpaces(object):

    def __init__(self, local_inputs, local_outputs, data_schema, class_num):

        self.threshold = 128.0

    def train(self, x, force_train=False):
        pass

    def predict(self, x):
        new_x = []

        x = x.get_by_name('Image')
        # load image
        img = x

        # convert to gray
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # threshold the grayscale image
        thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

        # use morphology erode to blur horizontally
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (151, 3))
        morph = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)

        # use morphology open to remove thin lines from dotted lines
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 17))
        morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)

        # find contours
        cntrs = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

        # find the topmost box
        ythresh = 1000000
        for c in cntrs:
            box = cv2.boundingRect(c)
            x, y, w, h = box
            if y < ythresh:
                topbox = box
                ythresh = y

        # Draw contours excluding the topmost box
        result = img.copy()
        for c in cntrs:
            box = cv2.boundingRect(c)
            if box != topbox:
                x, y, w, h = box
                cv2.rectangle(result, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # write result to disk
        cv2.imwrite("text_above_lines_threshold.png", thresh)
        cv2.imwrite("text_above_lines_morph.png", morph)
        cv2.imwrite("text_above_lines_lines.jpg", result)

        # cv2.imshow("GRAY", gray)
        plt.imshow(thresh)
        plt.plot()
        plt.imshow(morph)
        plt.plot()
        plt.imshow(result)
        plt.plot()

import numpy as np
from matplotlib import pyplot as plt
import cv2


class MarkSpaces(object):

    def __init__(self, local_inputs, local_outputs, data_schema, class_num):

        self.threshold = 128.0

    def train(self, x, force_train=False):
        pass

    def predict(self, x):
        new_x = []

        x = x.get_by_name('Image')
        # load image
        img = x

        # convert to gray
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # threshold the grayscale image
        thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

        # use morphology erode to blur horizontally
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (151, 3))
        morph = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)

        # use morphology open to remove thin lines from dotted lines
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 17))
        morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)

        # find contours
        cntrs = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

        # find the topmost box
        ythresh = 1000000
        for c in cntrs:
            box = cv2.boundingRect(c)
            x, y, w, h = box
            if y < ythresh:
                topbox = box
                ythresh = y

        # Draw contours excluding the topmost box
        result = img.copy()
        for c in cntrs:
            box = cv2.boundingRect(c)
            if box != topbox:
                x, y, w, h = box
                cv2.rectangle(result, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # write result to disk
        cv2.imwrite("text_above_lines_threshold.png", thresh)
        cv2.imwrite("text_above_lines_morph.png", morph)
        cv2.imwrite("text_above_lines_lines.jpg", result)

        # cv2.imshow("GRAY", gray)
        plt.imshow(thresh)
        plt.plot()
        plt.imshow(morph)
        plt.plot()
        plt.imshow(result)
        plt.plot()

# load image # convert to gray # threshold the grayscale image # use morphology erode to blur horizontally # use morphology open to remove thin lines from dotted lines # find contours # find the topmost box # Draw contours excluding the topmost box # write result to disk # cv2.imshow("GRAY", gray)