penut85420 commited on
Commit
09633a1
1 Parent(s): 8837f64

implement shuffle queue

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import random
 
3
 
4
  import gradio as gr
5
  import numpy as np
@@ -47,9 +48,18 @@ kana_list = [c for c in kana_list if c.strip()]
47
  chars = [os.path.join(dn, fn) for dn, _, ff in os.walk("chars") for fn in ff]
48
 
49
 
50
- def get_char(ch=None, assist=True, kana=True):
 
 
 
 
 
 
 
 
 
51
  if ch is None:
52
- img_path = random.choice(chars)
53
  ch = img_path[-5]
54
  else:
55
  img_path = f"chars/{ch}.png"
@@ -61,16 +71,20 @@ def get_char(ch=None, assist=True, kana=True):
61
  if not kana:
62
  info = f"{to_roma(ch)}"
63
 
64
- return ch, img_path, info
65
 
66
 
67
  font = gr.themes.GoogleFont("Noto Sans")
68
  theme = gr.themes.Soft(font=font)
69
 
70
  with gr.Blocks(theme=theme, title="Kana Writer") as app:
71
- ch, char_img, roma = get_char()
 
 
 
72
  curr_img_path = gr.State(char_img)
73
  curr_char = gr.State(ch)
 
74
  recog = Recognizer("model/model.xml", "model/char_list.txt")
75
 
76
  with gr.Tab("寫字練習"):
@@ -114,35 +128,35 @@ with gr.Blocks(theme=theme, title="Kana Writer") as app:
114
  _, nbest = recog(img)
115
  return ", ".join(parse_item(item) for items in nbest for item in items if valid_item(item))
116
 
117
- def rand_char(assist, kana):
118
- char, img, roma = get_char(None, assist, kana)
119
- return char, img, img, roma, None
120
 
121
  def clear(curr_char):
122
  return curr_char
123
 
124
- def change_opt(curr_char, assist, kana):
125
- char, img, roma = get_char(curr_char, assist, kana)
126
- return char, img, img, roma
127
 
128
  check_btn.click(do_recog, sketch, result, show_progress="minimal")
129
  rand_btn.click(
130
  rand_char,
131
- [assist, kana],
132
- [curr_char, sketch, curr_img_path, header, result],
133
  show_progress="minimal",
134
  )
135
  sketch.clear(clear, curr_img_path, sketch, show_progress="minimal")
136
  assist.change(
137
  change_opt,
138
- [curr_char, assist, kana],
139
- [curr_char, sketch, curr_img_path, header],
140
  show_progress="minimal",
141
  )
142
  kana.change(
143
  change_opt,
144
- [curr_char, assist, kana],
145
- [curr_char, sketch, curr_img_path, header],
146
  show_progress="minimal",
147
  )
148
 
 
1
  import os
2
  import random
3
+ from copy import deepcopy
4
 
5
  import gradio as gr
6
  import numpy as np
 
48
  chars = [os.path.join(dn, fn) for dn, _, ff in os.walk("chars") for fn in ff]
49
 
50
 
51
+ def init_chars():
52
+ curr_chars = deepcopy(chars)
53
+ random.shuffle(curr_chars)
54
+ return curr_chars
55
+
56
+
57
+ def get_char(ch=None, assist=True, kana=True, curr_chars: list = []):
58
+ if not curr_chars:
59
+ curr_chars = init_chars()
60
+
61
  if ch is None:
62
+ img_path = curr_chars.pop()
63
  ch = img_path[-5]
64
  else:
65
  img_path = f"chars/{ch}.png"
 
71
  if not kana:
72
  info = f"{to_roma(ch)}"
73
 
74
+ return ch, img_path, info, curr_chars
75
 
76
 
77
  font = gr.themes.GoogleFont("Noto Sans")
78
  theme = gr.themes.Soft(font=font)
79
 
80
  with gr.Blocks(theme=theme, title="Kana Writer") as app:
81
+ ch = "あ"
82
+ char_img = "chars/あ.png"
83
+ roma = "あ (a)"
84
+
85
  curr_img_path = gr.State(char_img)
86
  curr_char = gr.State(ch)
87
+ curr_chars = gr.State([])
88
  recog = Recognizer("model/model.xml", "model/char_list.txt")
89
 
90
  with gr.Tab("寫字練習"):
 
128
  _, nbest = recog(img)
129
  return ", ".join(parse_item(item) for items in nbest for item in items if valid_item(item))
130
 
131
+ def rand_char(assist, kana, chars):
132
+ char, img, roma, chars = get_char(None, assist, kana, chars)
133
+ return char, img, img, roma, None, chars
134
 
135
  def clear(curr_char):
136
  return curr_char
137
 
138
+ def change_opt(char, assist, kana, chars):
139
+ char, img, roma, chars = get_char(char, assist, kana, chars)
140
+ return char, img, img, roma, chars
141
 
142
  check_btn.click(do_recog, sketch, result, show_progress="minimal")
143
  rand_btn.click(
144
  rand_char,
145
+ [assist, kana, curr_chars],
146
+ [curr_char, sketch, curr_img_path, header, result, curr_chars],
147
  show_progress="minimal",
148
  )
149
  sketch.clear(clear, curr_img_path, sketch, show_progress="minimal")
150
  assist.change(
151
  change_opt,
152
+ [curr_char, assist, kana, curr_chars],
153
+ [curr_char, sketch, curr_img_path, header, curr_chars],
154
  show_progress="minimal",
155
  )
156
  kana.change(
157
  change_opt,
158
+ [curr_char, assist, kana, curr_chars],
159
+ [curr_char, sketch, curr_img_path, header, curr_chars],
160
  show_progress="minimal",
161
  )
162