File size: 3,070 Bytes
c5ce691
5c2d8e2
f8a6370
b82544e
5c2d8e2
 
c5ce691
 
 
 
6c6c372
bc852d4
c5ce691
 
 
 
 
 
 
 
 
 
7cf0f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5ce691
7cf0f5d
 
 
1bf5f83
7cf0f5d
 
 
 
 
 
 
 
c5ce691
 
 
 
 
 
b14190c
 
8b9d376
 
7cf0f5d
c5ce691
01b57fb
6c6c372
 
 
c5ce691
 
7cf0f5d
c9128f6
c5ce691
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
# TODO: Is it possible to put these into requirements.txt?
os.system('wget http://nz2.archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.19_amd64.deb')
os.system('dpkg -i libssl1.1_1.1.1f-1ubuntu2.19_amd64.deb')
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
import matplotlib.pylab as plt
import numpy as np

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(img):
    # cycle = 0
    # sprite = 1 # middle position, we have pixels at 0 and 2
    # total = 0
    # X = 1
    # crt = []
    # with open(file_obj.name) as f:
    #     data = f.read().split("\n")
    # for line in data:
    #     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
    #     # 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(res)
    
    # # Bonus: make an image and then use OCR
    # # to extract the text.
    # img = np.array([[1 if c == PIXEL else 0 for c in crt[i:i+DISPLAY_WIDTH]] for i in range(0, len(crt), DISPLAY_WIDTH)])
    # plt.imshow(img, cmap="binary")
    # plt.axis('off')
    # plt.savefig("day_10.png")
    img_path = img.name
    # 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]
    # TODO: Need to upload a font
    # im_show = draw_ocr(image, boxes, txts, scores)
    # im_show = Image.fromarray(im_show)
    # im_show.save('result.jpg')
    return txts, img

title = 'Cathode-Ray Tube'
description = 'Day 10 2022 AoC using OCR!!!'
article = "<p style='text-align: center'>Day 10 2022 AoC using OCR!!!</p>"
css = ".output_image {height: 40rem !important; width: 100% !important;}"
gr.Interface(
    solve,
    [gr.inputs.Image(type='file', label='Input')],
    [gr.Textbox(label="OCR result"), gr.outputs.Image(type='file', label='OCR image')],
    title=title,
    description=description,
    article=article,
    css=css,
    enable_queue=True
    ).launch(debug=True)