ken4 commited on
Commit
4a16e0d
·
verified ·
1 Parent(s): 07a8a9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -19
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import pandas as pd
2
  import PIL
3
- from PIL import Image
4
- from PIL import ImageDraw
5
  import gradio as gr
6
  import torch
7
  import easyocr
8
 
9
- # Download example files
10
  urls = {
11
  "english.png": "https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png",
12
  "thai.jpg": "https://github.com/JaidedAI/EasyOCR/raw/master/examples/thai.jpg",
@@ -16,7 +15,6 @@ urls = {
16
  "korean.png": "https://github.com/JaidedAI/EasyOCR/raw/master/examples/korean.png",
17
  "Hindi.jpeg": "https://i.imgur.com/mwQFd7G.jpeg"
18
  }
19
-
20
  for filename, url in urls.items():
21
  torch.hub.download_url_to_file(url, filename)
22
 
@@ -28,7 +26,7 @@ def draw_boxes(image, bounds, color='yellow', width=2):
28
  return image
29
 
30
  def inference(img_file, lang):
31
- img_path = img_file.name # file object -> path
32
  reader = easyocr.Reader(lang)
33
  bounds = reader.readtext(img_path)
34
 
@@ -38,16 +36,9 @@ def inference(img_file, lang):
38
 
39
  df = pd.DataFrame(bounds)[[1, 2]]
40
  df.columns = ["text", "confidence"]
41
-
42
  return "result.jpg", df
43
 
44
- choices = [
45
- "abq","ady","af","ang","ar","as","ava","az","be","bg","bh","bho","bn","bs","ch_sim","ch_tra",
46
- "che","cs","cy","da","dar","de","en","es","et","fa","fr","ga","gom","hi","hr","hu","id","inh","is",
47
- "it","ja","kbd","kn","ko","ku","la","lbe","lez","lt","lv","mah","mai","mi","mn","mr","ms","mt","ne",
48
- "new","nl","no","oc","pi","pl","pt","ro","ru","rs_cyrillic","rs_latin","sck","sk","sl","sq","sv","sw",
49
- "ta","tab","te","th","tjk","tl","tr","ug","uk","ur","uz","vi"
50
- ]
51
 
52
  examples = [
53
  ["english.png", ["en"]],
@@ -62,24 +53,22 @@ examples = [
62
  with gr.Blocks(css=".output_image, .input_image {height: 40rem; width: 100%;}") as demo:
63
 
64
  gr.Markdown("# **EasyOCR**")
65
- gr.Markdown("Upload an image and select output languages.")
66
 
67
  with gr.Row():
68
- # 🔥 FIX DI SINI → tidak auto-masuk galeri lagi
69
- image_input = gr.Image(type="file", label="Input Image")
70
  lang_input = gr.CheckboxGroup(choices=choices, value=["en"], label="Language")
71
 
72
  run_btn = gr.Button("Run OCR")
73
 
74
  with gr.Row():
75
  output_img = gr.Image(label="Detected Image")
76
- output_df = gr.Dataframe(headers=["text", "confidence"], label="Detected Text")
77
 
78
- gr.Examples(examples, [image_input, lang_input], [output_img, output_df], fn=inference)
79
 
80
  run_btn.click(
81
  inference,
82
- inputs=[image_input, lang_input],
83
  outputs=[output_img, output_df]
84
  )
85
 
 
1
  import pandas as pd
2
  import PIL
3
+ from PIL import Image, ImageDraw
 
4
  import gradio as gr
5
  import torch
6
  import easyocr
7
 
8
+ # Download examples
9
  urls = {
10
  "english.png": "https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png",
11
  "thai.jpg": "https://github.com/JaidedAI/EasyOCR/raw/master/examples/thai.jpg",
 
15
  "korean.png": "https://github.com/JaidedAI/EasyOCR/raw/master/examples/korean.png",
16
  "Hindi.jpeg": "https://i.imgur.com/mwQFd7G.jpeg"
17
  }
 
18
  for filename, url in urls.items():
19
  torch.hub.download_url_to_file(url, filename)
20
 
 
26
  return image
27
 
28
  def inference(img_file, lang):
29
+ img_path = img_file.name
30
  reader = easyocr.Reader(lang)
31
  bounds = reader.readtext(img_path)
32
 
 
36
 
37
  df = pd.DataFrame(bounds)[[1, 2]]
38
  df.columns = ["text", "confidence"]
 
39
  return "result.jpg", df
40
 
41
+ choices = [ ... (list tetap sama) ... ]
 
 
 
 
 
 
42
 
43
  examples = [
44
  ["english.png", ["en"]],
 
53
  with gr.Blocks(css=".output_image, .input_image {height: 40rem; width: 100%;}") as demo:
54
 
55
  gr.Markdown("# **EasyOCR**")
 
56
 
57
  with gr.Row():
58
+ file_input = gr.File(label="Upload Image", file_types=["image"])
 
59
  lang_input = gr.CheckboxGroup(choices=choices, value=["en"], label="Language")
60
 
61
  run_btn = gr.Button("Run OCR")
62
 
63
  with gr.Row():
64
  output_img = gr.Image(label="Detected Image")
65
+ output_df = gr.Dataframe(headers=["text", "confidence"])
66
 
67
+ gr.Examples(examples, [file_input, lang_input], [output_img, output_df], fn=inference)
68
 
69
  run_btn.click(
70
  inference,
71
+ inputs=[file_input, lang_input],
72
  outputs=[output_img, output_df]
73
  )
74