Esempio n. 1
0
def pdf2latex(model, img_path):

    buckets = [[240, 100], [320, 80], [400, 80], [400, 100], [480, 80],
               [480, 100], [560, 80], [560, 100], [640, 80], [640, 100],
               [720, 80], [720, 100], [720, 120], [720, 200], [800, 100],
               [800, 320], [1000, 200], [1000, 400], [1200, 200], [1600, 200],
               [1600, 1600]]

    dir_output = "tmp/"
    name = img_path.split('/')[-1].split('.')[0]
    run(
        "magick convert -density {} -quality {} {} {}".format(
            200, 100, img_path, dir_output + "{}.png".format(name)), TIMEOUT)
    img_path = dir_output + "{}.png".format(name)
    crop_image(img_path, img_path)
    pad_image(img_path, img_path, buckets=buckets)
    downsample_image(img_path, img_path, 2)

    img = imread(img_path)

    img = greyscale(img)
    hyps = model.predict(img)

    # model.logger.info(hyps[0])

    return hyps[0], img_path
Esempio n. 2
0
def img2latex(model, img_path):
    if not os.path.exists(img_path):
        print('image not exists')
    if img_path[-3:] == "png":
        img = imread(img_path)
    elif img_path[-3:] == "pdf":
        # call magick to convert the pdf into a png file
        buckets = [[240, 100], [320, 80], [400, 80], [400, 100], [480, 80],
                   [480, 100], [560, 80], [560, 100], [640, 80], [640, 100],
                   [720, 80], [720, 100], [720, 120], [720, 200], [800, 100],
                   [800, 320], [1000, 200], [1000, 400], [1200, 200],
                   [1600, 200], [1600, 1600]]

        dir_output = "tmp/"
        name = img_path.split('/')[-1].split('.')[0]
        run(
            "magick convert -density {} -quality {} {} {}".format(
                200, 100, img_path, dir_output + "{}.png".format(name)),
            TIMEOUT)
        img_path = dir_output + "{}.png".format(name)
        crop_image(img_path, img_path)
        pad_image(img_path, img_path, buckets=buckets)
        downsample_image(img_path, img_path, 2)

        img = imread(img_path)

    img = greyscale(img)
    hyps = model.predict(img)
    return hyps[0]
Esempio n. 3
0
def interactive_shell(model):
    """Creates interactive shell to play with model
    """
    model.logger.info("""
This is an interactive mode.
To exit, enter 'exit'.
Enter a path to a file
input> data/images_test/0.png""")

    while True:
        try:
            # for python 2
            img_path = raw_input("input> ")
        except NameError:
            # for python 3
            img_path = input("input> ")

        if img_path == "exit":
            break

        dir_output = "/tmp/"

        if img_path[-3:] == "png":
            img = Image.open(img_path)
            img = img.resize((80, 100), Image.BILINEAR)
            img.show()
            img = numpy.array(img)

            # crop_image(img_path, tmp_img)
            # pad_image(tmp_img, tmp_img, buckets=None)
            # downsample_image(tmp_img, tmp_img, 2)
            # img = imread(tmp_img)

        elif img_path[-3:] == "pdf":
            # call magick to convert the pdf into a png file
            buckets = [[240, 100], [320, 80], [400, 80], [400, 100], [480, 80],
                       [480, 100],
                       [560, 80], [560, 100], [640, 80], [640, 100], [720, 80],
                       [720, 100], [720, 120], [720, 200], [800, 100],
                       [800, 320], [1000, 200], [1000, 400], [1200, 200],
                       [1600, 200], [1600, 1600]]

            name = img_path.split('/')[-1].split('.')[0]
            run(
                "magick convert -density {} -quality {} {} {}".format(
                    200, 100, img_path, dir_output + "{}.png".format(name)),
                TIMEOUT)
            img_path = dir_output + "{}.png".format(name)
            crop_image(img_path, img_path)
            pad_image(img_path, img_path, buckets=buckets)
            downsample_image(img_path, img_path, 2)

            img = imread(img_path)

        img = greyscale(img)
        hyps = model.predict(img)

        model.logger.info(hyps[0])
Esempio n. 4
0
def img2latex(model,
              img_path,
              downsample_image_ratio=1,
              cropping=False,
              padding=False,
              img_augment=None,
              gray_scale=True):

    dir_output = "tmp/"
    run(['mkdir -p tmp'], TIMEOUT)
    name = img_path.split('/')[-1].split('.')[0]
    buckets = [[240, 100], [320, 80], [400, 80], [400, 100], [480, 80],
               [480, 100], [560, 80], [560, 100], [640, 80], [640, 100],
               [720, 80], [720, 100], [720, 120], [720, 200], [800, 100],
               [800, 320], [1000, 200], [1000, 400], [1200, 200], [1600, 200],
               [1600, 1600]]

    img_path_tmp = dir_output + "{}.png".format(name)

    if cropping:
        crop_image(img_path, img_path_tmp)

    if padding:
        pad_image(img_path_tmp if cropping else img_path,
                  img_path_tmp,
                  buckets=buckets)

    if downsample_image_ratio != 1:
        if cropping or padding:
            downsample_image(img_path_tmp,
                             img_path_tmp,
                             ratio=downsample_image_ratio)
        else:
            downsample_image(img_path,
                             img_path_tmp,
                             ratio=downsample_image_ratio)

    if cropping or padding or downsample_image_ratio != 1:
        img = imread(img_path_tmp)
    else:
        img = imread(img_path)

    if img_augment:
        img = img_augment.augment_image(img)

    img_obj = Image.fromarray(img)
    img_obj.save(img_path_tmp)

    if gray_scale:
        last = greyscale(img)
    else:
        last = img

    hyps = model.predict(last)

    return hyps[0], img, os.path.abspath(img_path_tmp)
Esempio n. 5
0
def pdf2latex(model, pdf_path):
    """
    Make prediction for PDF
    :param model: model to be used
    :param pdf_path: PDF location
    :return:
    """
    buckets = [
        [240, 100],
        [320, 80],
        [400, 80],
        [400, 100],
        [480, 80],
        [480, 100],
        [560, 80],
        [560, 100],
        [640, 80],
        [640, 100],
        [720, 80],
        [720, 100],
        [720, 120],
        [720, 200],
        [800, 100],
        [800, 320],
        [1000, 200],
        [1000, 400],
        [1200, 200],
        [1600, 200],
        [1600, 1600],
    ]

    dir_output = "tmp/"
    name = pdf_path.split("/")[-1].split(".")[0]
    run(
        "magick convert -density {} -quality {} {} {}".format(
            200, 100, pdf_path, dir_output + "{}.png".format(name)
        ),
        TIMEOUT,
    )
    pdf_path = dir_output + "{}.png".format(name)
    crop_image(pdf_path, pdf_path)
    pad_image(pdf_path, pdf_path, buckets=buckets)
    downsample_image(pdf_path, pdf_path, 2)

    img = imread(pdf_path)

    img = greyscale(img)
    hyps = model.predict(img)

    # model.logger.info(hyps[0])

    return hyps[0], pdf_path
Esempio n. 6
0
def index():
	if request.method == 'GET': 
		return render_template('index.html')

	if request.method == 'POST':
	    file = request.files['image']
	    filename = secure_filename(file.filename)
	    savedpath = os.path.join(UPLOAD_FOLDER, filename)
	    file.save(savedpath)
	    print("New uploaded file: [%s]" % savedpath)

	    try:

	    	if filename[-3:] == "png":

	        	img = imread(savedpath)

	    	elif filename[-3:] == "pdf":
	                # call magick to convert the pdf into a png file
	        	buckets = [
	            	[240, 100], [320, 80], [400, 80], [400, 100], [480, 80], [480, 100],
	            	[560, 80], [560, 100], [640, 80], [640, 100], [720, 80], [720, 100],
	            	[720, 120], [720, 200], [800, 100], [800, 320], [1000, 200],
	            	[1000, 400], [1200, 200], [1600, 200], [1600, 1600]
	        	]

	        	dir_output = "tmp/"
	        	name = savedpath.split('/')[-1].split('.')[0]

	        	run("magick convert -density {} -quality {} {} {}".format(200, 100,
					savedpath, dir_output+"{}.png".format(name)), TIMEOUT)
	        	savedpath = dir_output + "{}.png".format(name)
	        	crop_image(savedpath, savedpath)
	        	pad_image(savedpath, savedpath, buckets=buckets)
	        	downsample_image(savedpath, savedpath, 2)

	        	img = imread(savedpath)
	        else:
	        	raise "Unsupported file format"
	       	img = greyscale(img)
	       	hyps = model.predict(img)
	       	model.logger.info(hyps[0])
	       	message = hyps[0]

	    except Exception as e:
	    	print(e)
	    	message = "Error loading. Try a different image"

	    return render_template('index.html', latexstr=message, filename=file.filename)

	return "NOT Support Method"
Esempio n. 7
0
def interactive_shell(model):
    """Creates interactive shell to play with model
    """
    model.logger.info("""
This is an interactive mode.
To exit, enter 'exit'.
Enter a path to a file
input> data/images_test/0.png""")

    while True:
        try:
            # for python 2
            img_path = raw_input("input> ")
        except NameError:
            # for python 3
            img_path = input("input> ")

        if img_path == "exit":
            break
        if not os.path.exists(img_path):
            print('image not exists')
            continue
        if img_path[-3:] == "png":
            img = imread(img_path)

        elif img_path[-3:] == "pdf":
            # call magick to convert the pdf into a png file
            buckets = [[240, 100], [320, 80], [400, 80], [400, 100], [480, 80],
                       [480, 100],
                       [560, 80], [560, 100], [640, 80], [640, 100], [720, 80],
                       [720, 100], [720, 120], [720, 200], [800, 100],
                       [800, 320], [1000, 200], [1000, 400], [1200, 200],
                       [1600, 200], [1600, 1600]]

            dir_output = "tmp/"
            name = img_path.split('/')[-1].split('.')[0]
            run(
                "magick convert -density {} -quality {} {} {}".format(
                    200, 100, img_path, dir_output + "{}.png".format(name)),
                TIMEOUT)
            img_path = dir_output + "{}.png".format(name)
            crop_image(img_path, img_path)
            pad_image(img_path, img_path, buckets=buckets)
            downsample_image(img_path, img_path, 2)

            img = imread(img_path)

        img = greyscale(img)
        hyps = model.predict(img)

        model.logger.info(hyps[0])
Esempio n. 8
0
def img2latex(
    model,
    img,
    downsample_image_ratio=1,
    cropping=False,
    padding=False,
    img_augment=None,
    gray_scale=True,
):
    """
    Predict a latex code for an input equation image.
    :param model: model to be used
    :param img: input equation image
    :param downsample_image_ratio: down sampling ratio
    :param cropping: whether to crop
    :param padding: whether to pad
    :param img_augment: img augmentation filter
    :param gray_scale:whether to gray scale
    :return: latex prediction, processed img, processed img location
    """
    dir_output = "tmp/"
    run(["mkdir -p tmp"], TIMEOUT)
    name = str(uuid.uuid4())
    img_path = os.path.join("tmp/", f"{name}.png")
    img.save(img_path)
    buckets = [
        [240, 100],
        [320, 80],
        [400, 80],
        [400, 100],
        [480, 80],
        [480, 100],
        [560, 80],
        [560, 100],
        [640, 80],
        [640, 100],
        [720, 80],
        [720, 100],
        [720, 120],
        [720, 200],
        [800, 100],
        [800, 320],
        [1000, 200],
        [1000, 400],
        [1200, 200],
        [1600, 200],
        [1600, 1600],
    ]

    img_path_tmp = dir_output + "{}.png".format(name)

    if cropping:
        crop_image(img_path, img_path_tmp)

    if padding:
        pad_image(img_path_tmp if cropping else img_path, img_path_tmp, buckets=buckets)

    if downsample_image_ratio != 1:
        if cropping or padding:
            downsample_image(img_path_tmp, img_path_tmp, ratio=downsample_image_ratio)
        else:
            downsample_image(img_path, img_path_tmp, ratio=downsample_image_ratio)

    if cropping or padding or downsample_image_ratio != 1:
        img = imread(img_path_tmp)
    else:
        img = imread(img_path)

    if img_augment:
        img = img_augment.augment_image(img)

    img_obj = Image.fromarray(img)
    img_obj.save(img_path_tmp)

    if gray_scale:
        last = greyscale(img)
    else:
        last = img

    hyps = model.predict(last)

    return hyps[0], img, os.path.abspath(img_path_tmp)