Start
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system('pip install paddlepaddle')
|
3 |
+
os.system('pip install paddleocr')
|
4 |
+
from paddleocr import PaddleOCR, draw_ocr
|
5 |
+
from PIL import Image
|
6 |
+
import gradio as gr
|
7 |
+
import torch
|
8 |
+
|
9 |
+
torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
|
10 |
+
|
11 |
+
DISPLAY_WIDTH = 40
|
12 |
+
CYCLES = [20, 60, 100, 140, 180, 220]
|
13 |
+
PIXEL = '█' # Better than '#' for printing
|
14 |
+
EMPTY = ' ' # Better than '.' for printing
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
def solve(data, part_2 = False):
|
19 |
+
cycle = 0
|
20 |
+
sprite = 1 # middle position, we have pixels at 0 and 2
|
21 |
+
total = 0
|
22 |
+
X = 1
|
23 |
+
crt = []
|
24 |
+
for line in data.split("\n"):
|
25 |
+
if cycle in [sprite-1, sprite, sprite+1]:
|
26 |
+
crt.append(PIXEL)
|
27 |
+
else:
|
28 |
+
crt.append(EMPTY)
|
29 |
+
cycle += 1
|
30 |
+
if cycle in CYCLES:
|
31 |
+
total += X * cycle
|
32 |
+
if line.startswith("addx"):
|
33 |
+
if cycle in [sprite-1, sprite, sprite+1]:
|
34 |
+
crt.append(PIXEL)
|
35 |
+
else:
|
36 |
+
crt.append(EMPTY)
|
37 |
+
cycle += 1
|
38 |
+
if cycle in CYCLES:
|
39 |
+
total += X * cycle
|
40 |
+
X += int(line.split()[-1])
|
41 |
+
sprite = X
|
42 |
+
if part_2:
|
43 |
+
# Go back to first position after 40 pixels
|
44 |
+
cycle = cycle % DISPLAY_WIDTH
|
45 |
+
|
46 |
+
print("Solution to part 1: ", total)
|
47 |
+
res = "\n".join(["".join(crt[i:i+DISPLAY_WIDTH]) for i in range(0, len(crt), DISPLAY_WIDTH)])
|
48 |
+
print("Solution to part 2:")
|
49 |
+
print(res)
|
50 |
+
|
51 |
+
|
52 |
+
# Bonus: make an image and then use OCR
|
53 |
+
# to extract the text.
|
54 |
+
from PIL import Image
|
55 |
+
import numpy as np
|
56 |
+
import matplotlib.pylab as plt
|
57 |
+
img = np.array(crt)
|
58 |
+
|
59 |
+
plt.imshow(img, cmap="binary")
|
60 |
+
plt.axis('off')
|
61 |
+
plt.savefig("day_10.png")
|
62 |
+
|
63 |
+
print(ocr.ocr("day_10.png"))
|
64 |
+
img_path = "day_10.png"
|
65 |
+
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
|
66 |
+
result = ocr.ocr(img_path, cls=True)[0]
|
67 |
+
image = Image.open(img_path).convert('RGB')
|
68 |
+
boxes = [line[0] for line in result]
|
69 |
+
txts = [line[1][0] for line in result]
|
70 |
+
scores = [line[1][1] for line in result]
|
71 |
+
im_show = draw_ocr(image, boxes, txts, scores,
|
72 |
+
font_path='simfang.ttf')
|
73 |
+
im_show = Image.fromarray(im_show)
|
74 |
+
im_show.save('result.jpg')
|
75 |
+
return 'result.jpg'
|
76 |
+
|
77 |
+
title = 'PaddleOCR'
|
78 |
+
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.'
|
79 |
+
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>"
|
80 |
+
examples = [['example.jpg','en']]
|
81 |
+
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
82 |
+
gr.Interface(
|
83 |
+
solve,
|
84 |
+
[gr.File(file_types='text', label='Input'),
|
85 |
+
gr.outputs.Image(type='file', label='Output')],
|
86 |
+
title=title,
|
87 |
+
description=description,
|
88 |
+
article=article,
|
89 |
+
examples=examples,
|
90 |
+
css=css,
|
91 |
+
enable_queue=True
|
92 |
+
).launch(debug=True)
|