fifadotjs commited on
Commit
19249ce
·
1 Parent(s): a5fcbf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -28
app.py CHANGED
@@ -1,14 +1,10 @@
1
- import os
2
- os.system('pip install -r requirements.txt')
3
-
4
  import streamlit as st
5
  from PIL import Image
6
  import numpy as np
7
  from streamlit_cropper import st_cropper
8
- from tesserocr import PyTessBaseAPI
9
  from annotated_text import annotated_text
10
  from simpletransformers.ner import NERModel
11
-
12
  # Function to resize the image based on desired DPI
13
  def resize_image(image, desired_dpi):
14
  # Calculate the current DPI
@@ -25,15 +21,7 @@ def resize_image(image, desired_dpi):
25
 
26
  # Function to perform OCR on the image
27
  def perform_ocr(image):
28
-
29
- with PyTessBaseAPI(path=r'model', lang="tha") as api:
30
- api.SetVariable('preserve_interword_spaces', '1')
31
-
32
- im_gray = image.convert("L")
33
- cropped_img_bytes = im_gray.tobytes() # Convert grayscale image to bytes
34
- api.SetImage(Image.frombytes('L', im_gray.size, cropped_img_bytes)) # Set the image from bytes
35
-
36
- extracted_text = api.GetUTF8Text()
37
 
38
  return extracted_text
39
 
@@ -96,16 +84,16 @@ def main():
96
 
97
  desired_dpi = 300
98
 
99
- # Resize the image
100
  resized_image = resize_image(cropped_img, desired_dpi)
101
 
102
- # Display the image
103
  st.image(resized_image)
104
  # Perform OCR
105
- extracted_text = perform_ocr(resized_image)
106
 
107
  # Display extracted text
108
- txt_input = st.text_area("โปรดตรวจสอบความถูกต้อง", extracted_text)
109
 
110
  word_output = ""
111
  word_tuple = ()
@@ -156,15 +144,15 @@ if __name__ == '__main__':
156
  Hardware_Type = False
157
  # Model initialization
158
  _NER_TAGS = ['0', '1']
159
- try:
160
- model = NERModel(
161
- model_name=r"model",
162
- model_type="camembert",
163
- labels=_NER_TAGS,
164
- use_cuda=Hardware_Type
165
- )
166
- main()
167
- except:
168
- st.warning("Cuda not available, Please checking your hardware")
169
  aib_logo = Image.open(r'aib_logo.png')
170
  add_logo = st.sidebar.image(aib_logo)
 
 
 
 
1
  import streamlit as st
2
  from PIL import Image
3
  import numpy as np
4
  from streamlit_cropper import st_cropper
 
5
  from annotated_text import annotated_text
6
  from simpletransformers.ner import NERModel
7
+ import pytesseract
8
  # Function to resize the image based on desired DPI
9
  def resize_image(image, desired_dpi):
10
  # Calculate the current DPI
 
21
 
22
  # Function to perform OCR on the image
23
  def perform_ocr(image):
24
+ extracted_text =pytesseract.image_to_string(image=image, lang='tha')
 
 
 
 
 
 
 
 
25
 
26
  return extracted_text
27
 
 
84
 
85
  desired_dpi = 300
86
 
87
+ # # Resize the image
88
  resized_image = resize_image(cropped_img, desired_dpi)
89
 
90
+ # # Display the image
91
  st.image(resized_image)
92
  # Perform OCR
93
+ # extracted_text =
94
 
95
  # Display extracted text
96
+ txt_input = st.text_area("โปรดตรวจสอบความถูกต้อง", perform_ocr(cropped_img))
97
 
98
  word_output = ""
99
  word_tuple = ()
 
144
  Hardware_Type = False
145
  # Model initialization
146
  _NER_TAGS = ['0', '1']
147
+ # try:
148
+ model = NERModel(
149
+ model_name=r"model",
150
+ model_type="camembert",
151
+ labels=_NER_TAGS,
152
+ use_cuda=Hardware_Type
153
+ )
154
+ main()
155
+ # except:
156
+ # st.warning("Cuda not available, Please checking your hardware")
157
  aib_logo = Image.open(r'aib_logo.png')
158
  add_logo = st.sidebar.image(aib_logo)