ayymen commited on
Commit
1abadf7
1 Parent(s): 2c62fa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -1,36 +1,35 @@
1
  import os
2
- os.environ['USE_TORCH'] = '1'
3
-
4
  from doctr.io import DocumentFile
5
- from doctr.models import ocr_predictor
6
  import gradio as gr
7
- from PIL import Image
8
 
9
- predictor = ocr_predictor(pretrained=True)
 
 
10
 
11
- title="DocTR OCR (PDL Demo)"
12
- description="Upload an image to get the OCR results !"
13
 
14
- def greet(img):
15
  img.save("out.jpg")
16
  doc = DocumentFile.from_images("out.jpg")
17
- output=predictor(doc)
18
- res=""
19
  for obj in output.pages:
20
- for obj1 in obj.blocks:
21
- for obj2 in obj1.lines:
22
- for obj3 in obj2.words:
23
- res=res + " " + obj3.value
24
- res=res + "\n"
25
- res=res + "\n"
26
  _output_name = "RESULT_OCR.txt"
27
- open(_output_name, 'w').close() # clear file
28
  with open(_output_name, "w", encoding="utf-8", errors="ignore") as f:
29
  f.write(res)
30
  print("Writing into file")
31
  return res, _output_name
32
 
33
- demo = gr.Interface(fn=greet,
34
  inputs=gr.Image(type="pil"),
35
  outputs=["text", "file"],
36
  title=title,
@@ -38,4 +37,4 @@ demo = gr.Interface(fn=greet,
38
  examples=[["Examples/Book.png"],["Examples/News.png"],["Examples/Manuscript.jpg"],["Examples/Files.jpg"]]
39
  )
40
 
41
- demo.launch(debug=True)
 
1
  import os
 
 
2
  from doctr.io import DocumentFile
3
+ from doctr.models import ocr_predictor, from_hub
4
  import gradio as gr
 
5
 
6
+ os.environ['USE_TORCH'] = '1'
7
+ reco_model = from_hub('ayymen/crnn_mobilenet_v3_large_gen_hw')
8
+ predictor = ocr_predictor(reco_arch=reco_model, pretrained=True)
9
 
10
+ title = "Tifinagh OCR"
11
+ description = "Upload an image to get the OCR results !"
12
 
13
+ def ocr(img):
14
  img.save("out.jpg")
15
  doc = DocumentFile.from_images("out.jpg")
16
+ output = predictor(doc)
17
+ res = ""
18
  for obj in output.pages:
19
+ for obj1 in obj.blocks:
20
+ for obj2 in obj1.lines:
21
+ for obj3 in obj2.words:
22
+ res=res + " " + obj3.value
23
+ res=res + "\n"
24
+ res=res + "\n\n"
25
  _output_name = "RESULT_OCR.txt"
26
+ open(_output_name, 'w', encoding="utf-8").close() # clear file
27
  with open(_output_name, "w", encoding="utf-8", errors="ignore") as f:
28
  f.write(res)
29
  print("Writing into file")
30
  return res, _output_name
31
 
32
+ demo = gr.Interface(fn=ocr,
33
  inputs=gr.Image(type="pil"),
34
  outputs=["text", "file"],
35
  title=title,
 
37
  examples=[["Examples/Book.png"],["Examples/News.png"],["Examples/Manuscript.jpg"],["Examples/Files.jpg"]]
38
  )
39
 
40
+ demo.launch(debug=True)