fumiyaaa commited on
Commit
8d3d2a6
·
verified ·
1 Parent(s): a0c2b76

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -10
app.py CHANGED
@@ -103,16 +103,93 @@ def transcribe_handwriting(image):
103
  return output_text[0] if output_text else "文字を認識できませんでした。"
104
 
105
 
106
- # シンプルなgr.Interfaceを使用
107
- demo = gr.Interface(
108
- fn=transcribe_handwriting,
109
- inputs=gr.Image(type="pil", label="手書き文字の画像をアップロード"),
110
- outputs=gr.Textbox(label="認識結果", lines=10),
111
- title="手書き文字認識システム",
112
- description="**Qwen3-VL-4B-Instruct** を使用した手書き文字のOCR(光学文字認識)システムです。日本語とアルファベットの両方に対応しています。",
113
- article="### 使い方\nスキャンした手書きメモや写真をアップロードしてください。認識精度は文字の明瞭さに依存します。",
114
- allow_flagging="never",
115
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  if __name__ == "__main__":
118
  demo.launch()
 
103
  return output_text[0] if output_text else "文字を認識できませんでした。"
104
 
105
 
106
+ def process_sketch(image_dict):
107
+ """ImageEditorからの入力を処理"""
108
+ if image_dict is None:
109
+ return "手書きしてください。"
110
+
111
+ # gr.ImageEditorの出力は辞書形式: {"background": ..., "layers": [...], "composite": ...}
112
+ if isinstance(image_dict, dict):
113
+ image = image_dict.get("composite")
114
+ if image is None:
115
+ image = image_dict.get("background")
116
+ else:
117
+ image = image_dict
118
+
119
+ if image is None:
120
+ return "手書きしてください。"
121
+
122
+ return transcribe_handwriting(image)
123
+
124
+
125
+ # Gradioインターフェースの構築
126
+ with gr.Blocks(title="手書き文字認識システム") as demo:
127
+ gr.Markdown(
128
+ """
129
+ # 手書き文字認識システム
130
+
131
+ **Qwen3-VL-4B-Instruct** を使用した手書き文字のOCR(光学文字認識)システムです。
132
+ 日本語とアルファベットの両方に対応しています。
133
+ """
134
+ )
135
+
136
+ with gr.Tab("画像アップロード"):
137
+ gr.Markdown("手書き文字が書かれた画像をアップロードしてください。")
138
+ with gr.Row():
139
+ with gr.Column():
140
+ upload_image = gr.Image(
141
+ label="画像をアップロード",
142
+ type="pil",
143
+ height=400,
144
+ )
145
+ upload_btn = gr.Button("文字を認識", variant="primary")
146
+ with gr.Column():
147
+ upload_output = gr.Textbox(
148
+ label="認識結果",
149
+ lines=10,
150
+ )
151
+
152
+ upload_btn.click(
153
+ fn=transcribe_handwriting,
154
+ inputs=upload_image,
155
+ outputs=upload_output,
156
+ )
157
+
158
+ with gr.Tab("手書き入力"):
159
+ gr.Markdown("マウスやタッチで文字を書いてください。")
160
+ with gr.Row():
161
+ with gr.Column():
162
+ sketch_input = gr.ImageEditor(
163
+ label="手書きエリア",
164
+ sources=(),
165
+ brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
166
+ canvas_size=(600, 400),
167
+ type="pil",
168
+ )
169
+ sketch_btn = gr.Button("文字を認識", variant="primary")
170
+ with gr.Column():
171
+ sketch_output = gr.Textbox(
172
+ label="認識結果",
173
+ lines=10,
174
+ )
175
+
176
+ sketch_btn.click(
177
+ fn=process_sketch,
178
+ inputs=sketch_input,
179
+ outputs=sketch_output,
180
+ )
181
+
182
+ gr.Markdown(
183
+ """
184
+ ---
185
+ ### 使い方のヒント
186
+ - **画像アップロード**: スキャンした手書きメモや写真をアップロード
187
+ - **手書き入力**: マウスで直接文字を書いて認識をテスト
188
+ - 認識精度は文字の明瞭さに依存します
189
+
190
+ *Powered by Qwen3-VL-4B-Instruct*
191
+ """
192
+ )
193
 
194
  if __name__ == "__main__":
195
  demo.launch()