yalouini commited on
Commit
7cf0f5d
1 Parent(s): 8b9d376

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -40
app.py CHANGED
@@ -18,46 +18,47 @@ EMPTY = ' ' # Better than '.' for printing
18
 
19
 
20
 
21
- def solve(file_obj):
22
- cycle = 0
23
- sprite = 1 # middle position, we have pixels at 0 and 2
24
- total = 0
25
- X = 1
26
- crt = []
27
- with open(file_obj.name) as f:
28
- data = f.read().split("\n")
29
- for line in data:
30
- if cycle in [sprite-1, sprite, sprite+1]:
31
- crt.append(PIXEL)
32
- else:
33
- crt.append(EMPTY)
34
- cycle += 1
35
- if cycle in CYCLES:
36
- total += X * cycle
37
- if line.startswith("addx"):
38
- if cycle in [sprite-1, sprite, sprite+1]:
39
- crt.append(PIXEL)
40
- else:
41
- crt.append(EMPTY)
42
- cycle += 1
43
- if cycle in CYCLES:
44
- total += X * cycle
45
- X += int(line.split()[-1])
46
- sprite = X
47
- # Go back to first position after 40 pixels
48
- cycle = cycle % DISPLAY_WIDTH
49
 
50
- # print("Solution to part 1: ", total)
51
- res = "\n".join(["".join(crt[i:i+DISPLAY_WIDTH]) for i in range(0, len(crt), DISPLAY_WIDTH)])
52
- print(res)
53
 
54
- # Bonus: make an image and then use OCR
55
- # to extract the text.
56
- 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)])
57
- plt.imshow(img, cmap="binary")
58
- plt.axis('off')
59
- plt.savefig("day_10.png")
60
- img_path = "day_10.png"
 
61
  ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
62
  result = ocr.ocr(img_path, cls=True)[0]
63
  image = Image.open(img_path).convert('RGB')
@@ -68,7 +69,7 @@ def solve(file_obj):
68
  # im_show = draw_ocr(image, boxes, txts, scores)
69
  # im_show = Image.fromarray(im_show)
70
  # im_show.save('result.jpg')
71
- return txts, 'day_10.png'
72
 
73
  title = 'Cathode-Ray Tube'
74
  description = 'Day 10 2022 AoC using OCR!!!'
@@ -76,7 +77,7 @@ article = "<p style='text-align: center'>Day 10 2022 AoC using OCR!!!</p>"
76
  css = ".output_image {height: 40rem !important; width: 100% !important;}"
77
  gr.Interface(
78
  solve,
79
- [gr.File(file_types='text', label='Input')],
80
  [gr.Textbox(label="OCR result"), gr.outputs.Image(type='file', label='OCR image')],
81
  title=title,
82
  description=description,
 
18
 
19
 
20
 
21
+ def solve(img):
22
+ # cycle = 0
23
+ # sprite = 1 # middle position, we have pixels at 0 and 2
24
+ # total = 0
25
+ # X = 1
26
+ # crt = []
27
+ # with open(file_obj.name) as f:
28
+ # data = f.read().split("\n")
29
+ # for line in data:
30
+ # if cycle in [sprite-1, sprite, sprite+1]:
31
+ # crt.append(PIXEL)
32
+ # else:
33
+ # crt.append(EMPTY)
34
+ # cycle += 1
35
+ # if cycle in CYCLES:
36
+ # total += X * cycle
37
+ # if line.startswith("addx"):
38
+ # if cycle in [sprite-1, sprite, sprite+1]:
39
+ # crt.append(PIXEL)
40
+ # else:
41
+ # crt.append(EMPTY)
42
+ # cycle += 1
43
+ # if cycle in CYCLES:
44
+ # total += X * cycle
45
+ # X += int(line.split()[-1])
46
+ # sprite = X
47
+ # # Go back to first position after 40 pixels
48
+ # cycle = cycle % DISPLAY_WIDTH
49
 
50
+ # # print("Solution to part 1: ", total)
51
+ # res = "\n".join(["".join(crt[i:i+DISPLAY_WIDTH]) for i in range(0, len(crt), DISPLAY_WIDTH)])
52
+ # print(res)
53
 
54
+ # # Bonus: make an image and then use OCR
55
+ # # to extract the text.
56
+ # 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)])
57
+ # plt.imshow(img, cmap="binary")
58
+ # plt.axis('off')
59
+ # plt.savefig("day_10.png")
60
+ img_path = img.name
61
+ # img_path = "day_10.png"
62
  ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
63
  result = ocr.ocr(img_path, cls=True)[0]
64
  image = Image.open(img_path).convert('RGB')
 
69
  # im_show = draw_ocr(image, boxes, txts, scores)
70
  # im_show = Image.fromarray(im_show)
71
  # im_show.save('result.jpg')
72
+ return txts, img
73
 
74
  title = 'Cathode-Ray Tube'
75
  description = 'Day 10 2022 AoC using OCR!!!'
 
77
  css = ".output_image {height: 40rem !important; width: 100% !important;}"
78
  gr.Interface(
79
  solve,
80
+ [gr.inputs.Image(type='file', label='Input')],
81
  [gr.Textbox(label="OCR result"), gr.outputs.Image(type='file', label='OCR image')],
82
  title=title,
83
  description=description,