OCR / app.py
yalouini's picture
Start
c5ce691
raw
history blame
No virus
3.35 kB
import os
os.system('pip install paddlepaddle')
os.system('pip install paddleocr')
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import gradio as gr
import torch
torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
DISPLAY_WIDTH = 40
CYCLES = [20, 60, 100, 140, 180, 220]
PIXEL = '█' # Better than '#' for printing
EMPTY = ' ' # Better than '.' for printing
def solve(data, part_2 = False):
cycle = 0
sprite = 1 # middle position, we have pixels at 0 and 2
total = 0
X = 1
crt = []
for line in data.split("\n"):
if cycle in [sprite-1, sprite, sprite+1]:
crt.append(PIXEL)
else:
crt.append(EMPTY)
cycle += 1
if cycle in CYCLES:
total += X * cycle
if line.startswith("addx"):
if cycle in [sprite-1, sprite, sprite+1]:
crt.append(PIXEL)
else:
crt.append(EMPTY)
cycle += 1
if cycle in CYCLES:
total += X * cycle
X += int(line.split()[-1])
sprite = X
if part_2:
# Go back to first position after 40 pixels
cycle = cycle % DISPLAY_WIDTH
print("Solution to part 1: ", total)
res = "\n".join(["".join(crt[i:i+DISPLAY_WIDTH]) for i in range(0, len(crt), DISPLAY_WIDTH)])
print("Solution to part 2:")
print(res)
# Bonus: make an image and then use OCR
# to extract the text.
from PIL import Image
import numpy as np
import matplotlib.pylab as plt
img = np.array(crt)
plt.imshow(img, cmap="binary")
plt.axis('off')
plt.savefig("day_10.png")
print(ocr.ocr("day_10.png"))
img_path = "day_10.png"
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
result = ocr.ocr(img_path, cls=True)[0]
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores,
font_path='simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
return 'result.jpg'
title = 'PaddleOCR'
description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hub/scene/ocr'>Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)</a> | <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"
examples = [['example.jpg','en']]
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
gr.Interface(
solve,
[gr.File(file_types='text', label='Input'),
gr.outputs.Image(type='file', label='Output')],
title=title,
description=description,
article=article,
examples=examples,
css=css,
enable_queue=True
).launch(debug=True)