eoeooe commited on
Commit
2e6613e
·
verified ·
1 Parent(s): c833ec2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -3,18 +3,26 @@ from doctr.io import DocumentFile
3
  from doctr.models import ocr_predictor
4
  import gradio as gr
5
 
 
6
  os.environ['USE_TORCH'] = '1'
7
 
8
- # ใช้โมเดล OCR ภาษาอังกฤษที่มีใน Doctr
9
  predictor = ocr_predictor(pretrained=True)
10
 
 
11
  title = "English OCR"
12
- description = """Upload an image to extract English text!"""
13
 
 
14
  def ocr(img):
 
15
  img.save("out.jpg")
16
  doc = DocumentFile.from_images("out.jpg")
 
 
17
  output = predictor(doc)
 
 
18
  res = ""
19
  for page in output.pages:
20
  for block in page.blocks:
@@ -23,20 +31,25 @@ def ocr(img):
23
  res += word.value + " "
24
  res += "\n"
25
  res += "\n"
26
- _output_name = "RESULT_OCR.txt"
27
- with open(_output_name, "w", encoding="utf-8", errors="ignore") as f:
 
 
28
  f.write(res)
29
- return res, _output_name
 
30
 
 
31
  demo = gr.Interface(
32
  fn=ocr,
33
- inputs=[gr.Image(type="pil")],
34
  outputs=[
35
  gr.Textbox(lines=20, label="Full Text"),
36
  gr.File(label="Download OCR Results")
37
  ],
38
  title=title,
39
- description=description,
40
  )
41
 
 
42
  demo.launch(debug=True)
 
3
  from doctr.models import ocr_predictor
4
  import gradio as gr
5
 
6
+ # เปิดใช้ PyTorch GPU/CPU
7
  os.environ['USE_TORCH'] = '1'
8
 
9
+ # โหลดโมเดล OCR ภาษาอังกฤษ (pretrained)
10
  predictor = ocr_predictor(pretrained=True)
11
 
12
+ # Title/Description สำหรับ Gradio
13
  title = "English OCR"
14
+ description = """Upload an image to extract English text from it!"""
15
 
16
+ # ฟังก์ชัน OCR
17
  def ocr(img):
18
+ # บันทึกไฟล์ชั่วคราว
19
  img.save("out.jpg")
20
  doc = DocumentFile.from_images("out.jpg")
21
+
22
+ # ทำ OCR
23
  output = predictor(doc)
24
+
25
+ # รวมผลลัพธ์เป็น string
26
  res = ""
27
  for page in output.pages:
28
  for block in page.blocks:
 
31
  res += word.value + " "
32
  res += "\n"
33
  res += "\n"
34
+
35
+ # บันทึกผลเป็นไฟล์ txt
36
+ output_name = "RESULT_OCR.txt"
37
+ with open(output_name, "w", encoding="utf-8", errors="ignore") as f:
38
  f.write(res)
39
+
40
+ return res, output_name
41
 
42
+ # สร้าง Gradio Interface
43
  demo = gr.Interface(
44
  fn=ocr,
45
+ inputs=[gr.Image(type="pil")], # รับภาพจากผู้ใช้
46
  outputs=[
47
  gr.Textbox(lines=20, label="Full Text"),
48
  gr.File(label="Download OCR Results")
49
  ],
50
  title=title,
51
+ description=description
52
  )
53
 
54
+ # Launch
55
  demo.launch(debug=True)