def set_text_region(file_path):
    img = cv2.imread(file_path)
    height = img.shape[0]
    width = img.shape[1]
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
    (ret, binary) = cv2.threshold(sobel, 0, 255, (cv2.THRESH_OTSU + cv2.THRESH_BINARY))
    element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (int((width / 2)), 5))
    element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (int((width / 3)), 2))
    dilation = cv2.dilate(binary, element2, iterations=1)
    erosion = cv2.erode(dilation, element1, iterations=1)
    dilation2 = cv2.dilate(erosion, element2, iterations=2)
    region = []
    (contours, hierarchy) = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for i in range(len(contours)):
        cnt = contours[i]
        area = cv2.contourArea(cnt)
        if (area < ((height * width) / 6)):
            continue
        rect = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        height = abs((box[0][1] - box[2][1]))
        width = abs((box[0][0] - box[2][0]))
        if (height > (width * 1.3)):
            continue
        region.append(box)
    return (img, region)
def set_text_region(file_path):
    img = cv2.imread(file_path)
    height = img.shape[0]
    width = img.shape[1]
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
    (ret, binary) = cv2.threshold(sobel, 0, 255, (cv2.THRESH_OTSU + cv2.THRESH_BINARY))
    element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (int((width / 2)), 5))
    element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (int((width / 3)), 2))
    dilation = cv2.dilate(binary, element2, iterations=1)
    erosion = cv2.erode(dilation, element1, iterations=1)
    dilation2 = cv2.dilate(erosion, element2, iterations=2)
    region = []
    (contours, hierarchy) = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for i in range(len(contours)):
        cnt = contours[i]
        area = cv2.contourArea(cnt)
        if (area < ((height * width) / 6)):
            continue
        rect = cv2.minAreaRect(cnt)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        height = abs((box[0][1] - box[2][1]))
        width = abs((box[0][0] - box[2][0]))
        if (height > (width * 1.3)):
            continue
        region.append(box)
    return (img, region)