Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,109 @@ 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
|
|
@@ -37,97 +140,23 @@ def main():
|
|
| 37 |
|
| 38 |
st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if return_type == 'box':
|
| 58 |
-
rect = st_cropper(
|
| 59 |
-
img,
|
| 60 |
-
realtime_update=realtime_update,
|
| 61 |
-
box_color=box_color,
|
| 62 |
-
aspect_ratio=aspect_ratio,
|
| 63 |
-
return_type=return_type
|
| 64 |
-
)
|
| 65 |
-
raw_image = np.asarray(img).astype('uint8')
|
| 66 |
-
left, top, width, height = tuple(map(int, rect.values()))
|
| 67 |
-
st.write(rect)
|
| 68 |
-
masked_image = np.zeros(raw_image.shape, dtype='uint8')
|
| 69 |
-
masked_image[top:top + height, left:left + width] = raw_image[top:top + height, left:left + width]
|
| 70 |
-
st.image(Image.fromarray(masked_image))
|
| 71 |
-
else:
|
| 72 |
-
# Get a cropped image from the frontend
|
| 73 |
-
cropped_img = st_cropper(
|
| 74 |
-
img,
|
| 75 |
-
realtime_update=realtime_update,
|
| 76 |
-
box_color=box_color,
|
| 77 |
-
aspect_ratio=aspect_ratio,
|
| 78 |
-
return_type=return_type
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
# Manipulate cropped image at will
|
| 82 |
-
st.write("Preview")
|
| 83 |
-
_ = cropped_img.thumbnail((1000, 1000))
|
| 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 = ()
|
| 100 |
-
check = st.button("ตรวจสอบ",type="primary",use_container_width=True)
|
| 101 |
-
if check:
|
| 102 |
-
st.header("สรุปผล")
|
| 103 |
-
predictions, raw_outputs = model.predict([txt_input])
|
| 104 |
-
wrongword_lst = []
|
| 105 |
-
|
| 106 |
-
for i in predictions[0]:
|
| 107 |
-
word = i.values()
|
| 108 |
-
label = list(word)[0]
|
| 109 |
-
if label == "1":
|
| 110 |
-
wrong_word = list(i.keys())[0]
|
| 111 |
-
wrongword_lst.append(wrong_word)
|
| 112 |
-
|
| 113 |
-
word_tuple = ()
|
| 114 |
-
for i in predictions[0]:
|
| 115 |
-
label = i.values()
|
| 116 |
-
word = list(i.keys())[0]
|
| 117 |
-
label = list(label)[0]
|
| 118 |
-
if label == "1":
|
| 119 |
-
word_tuple = (*word_tuple, (f'{word} ', 'มีการล็อคสเปค', '#F41B15'))
|
| 120 |
-
else:
|
| 121 |
-
word_tuple = (*word_tuple, f'{word} ')
|
| 122 |
-
word_output = wrongword_lst
|
| 123 |
-
|
| 124 |
-
if word_output != "":
|
| 125 |
-
annotated_text(list(word_tuple))
|
| 126 |
-
|
| 127 |
-
col1, col2 = st.columns(2)
|
| 128 |
-
with col1:
|
| 129 |
-
st.header("คำที่สุ่มเสี่ยง")
|
| 130 |
-
st.write(word_output)
|
| 131 |
|
| 132 |
|
| 133 |
if __name__ == '__main__':
|
|
@@ -144,15 +173,21 @@ if __name__ == '__main__':
|
|
| 144 |
Hardware_Type = False
|
| 145 |
# Model initialization
|
| 146 |
_NER_TAGS = ['0', '1']
|
| 147 |
-
try:
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
except:
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
aib_logo = Image.open(r'aib_logo.png')
|
| 158 |
add_logo = st.sidebar.image(aib_logo)
|
|
|
|
| 5 |
from annotated_text import annotated_text
|
| 6 |
from simpletransformers.ner import NERModel
|
| 7 |
import pytesseract
|
| 8 |
+
import os
|
| 9 |
+
def process_image(img_file):
|
| 10 |
+
st.write("Crop ส่วนที่ต้องการ (คุณลักษณะ)")
|
| 11 |
+
aspect_ratio = None
|
| 12 |
+
|
| 13 |
+
col1, col2 = st.sidebar.columns(2)
|
| 14 |
+
with col1:
|
| 15 |
+
box_color = st.color_picker(label="Box Color", value='#0000FF')
|
| 16 |
+
return_type = "image"
|
| 17 |
+
realtime_update = st.sidebar.checkbox(label="Update in Real Time", value=True)
|
| 18 |
+
|
| 19 |
+
img = Image.open(img_file)
|
| 20 |
+
width, height = img.size
|
| 21 |
+
|
| 22 |
+
if not realtime_update:
|
| 23 |
+
st.write("Double click to save crop")
|
| 24 |
+
|
| 25 |
+
if return_type == 'box':
|
| 26 |
+
rect = st_cropper(
|
| 27 |
+
img,
|
| 28 |
+
realtime_update=realtime_update,
|
| 29 |
+
box_color=box_color,
|
| 30 |
+
aspect_ratio=aspect_ratio,
|
| 31 |
+
return_type=return_type
|
| 32 |
+
)
|
| 33 |
+
raw_image = np.asarray(img).astype('uint8')
|
| 34 |
+
left, top, width, height = tuple(map(int, rect.values()))
|
| 35 |
+
st.write(rect)
|
| 36 |
+
masked_image = np.zeros(raw_image.shape, dtype='uint8')
|
| 37 |
+
masked_image[top:top + height, left:left + width] = raw_image[top:top + height, left:left + width]
|
| 38 |
+
st.image(Image.fromarray(masked_image))
|
| 39 |
+
else:
|
| 40 |
+
# Get a cropped image from the frontend
|
| 41 |
+
cropped_img = st_cropper(
|
| 42 |
+
img,
|
| 43 |
+
realtime_update=realtime_update,
|
| 44 |
+
box_color=box_color,
|
| 45 |
+
aspect_ratio=aspect_ratio,
|
| 46 |
+
return_type=return_type
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Manipulate cropped image at will
|
| 50 |
+
st.write("Preview")
|
| 51 |
+
_ = cropped_img.thumbnail((1000, 1000))
|
| 52 |
+
|
| 53 |
+
desired_dpi = 300
|
| 54 |
+
|
| 55 |
+
# Resize the image
|
| 56 |
+
resized_image = resize_image(cropped_img, desired_dpi)
|
| 57 |
+
|
| 58 |
+
# Display the image
|
| 59 |
+
st.image(resized_image)
|
| 60 |
+
# Perform OCR
|
| 61 |
+
# extracted_text =
|
| 62 |
+
|
| 63 |
+
# Display extracted text
|
| 64 |
+
txt_input = st.text_area("โปรดตรวจสอบความถูกต้อง", perform_ocr(cropped_img))
|
| 65 |
+
|
| 66 |
+
word_output = ""
|
| 67 |
+
word_tuple = ()
|
| 68 |
+
check = st.button("Check", type="primary", use_container_width=True)
|
| 69 |
+
if check:
|
| 70 |
+
st.header("สรุปผล")
|
| 71 |
+
predictions, raw_outputs = model.predict([txt_input])
|
| 72 |
+
wrongword_lst = []
|
| 73 |
+
|
| 74 |
+
for i in predictions[0]:
|
| 75 |
+
word = i.values()
|
| 76 |
+
label = list(word)[0]
|
| 77 |
+
if label == "1":
|
| 78 |
+
wrong_word = list(i.keys())[0]
|
| 79 |
+
wrongword_lst.append(wrong_word)
|
| 80 |
+
|
| 81 |
+
word_tuple = ()
|
| 82 |
+
for i in predictions[0]:
|
| 83 |
+
label = i.values()
|
| 84 |
+
word = list(i.keys())[0]
|
| 85 |
+
label = list(label)[0]
|
| 86 |
+
if label == "1":
|
| 87 |
+
word_tuple = (*word_tuple, (f'{word} ', 'มีการล็อคสเปค', '#F41B15'))
|
| 88 |
+
else:
|
| 89 |
+
word_tuple = (*word_tuple, f'{word} ')
|
| 90 |
+
word_output = wrongword_lst
|
| 91 |
+
|
| 92 |
+
if word_output != "":
|
| 93 |
+
annotated_text(list(word_tuple))
|
| 94 |
+
|
| 95 |
+
col1, col2 = st.columns(2)
|
| 96 |
+
with col1:
|
| 97 |
+
st.header("คำที่สุ่มเสี่ยง")
|
| 98 |
+
st.write(word_output)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def get_sample_images(folder):
|
| 106 |
+
sample_images = []
|
| 107 |
+
for filename in os.listdir(folder):
|
| 108 |
+
if filename.endswith(".jpg") or filename.endswith(".png"):
|
| 109 |
+
sample_images.append(os.path.join(folder, filename))
|
| 110 |
+
return sample_images
|
| 111 |
# Function to resize the image based on desired DPI
|
| 112 |
def resize_image(image, desired_dpi):
|
| 113 |
# Calculate the current DPI
|
|
|
|
| 140 |
|
| 141 |
st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 142 |
|
| 143 |
+
img_files = st.sidebar.file_uploader(label='Upload a file', type=['png', 'jpg'], accept_multiple_files=False)
|
| 144 |
+
sample_folder = "test_img"
|
| 145 |
+
sample_images = get_sample_images(sample_folder)
|
| 146 |
+
|
| 147 |
+
# Display the sample images in the sidebar
|
| 148 |
+
selection = st.sidebar.selectbox("Select a sample", ["Browse files Mode"] + sample_images, format_func=lambda x: os.path.basename(x))
|
| 149 |
+
if selection and selection != "Browse files Mode":
|
| 150 |
+
img_file = selection
|
| 151 |
+
process_image(img_file)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
elif img_files:
|
| 157 |
+
img_file = img_files
|
| 158 |
+
process_image(img_file)
|
| 159 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
if __name__ == '__main__':
|
|
|
|
| 173 |
Hardware_Type = False
|
| 174 |
# Model initialization
|
| 175 |
_NER_TAGS = ['0', '1']
|
| 176 |
+
# try:
|
| 177 |
+
model = NERModel(
|
| 178 |
+
model_name=r"model",
|
| 179 |
+
model_type="camembert",
|
| 180 |
+
labels=_NER_TAGS,
|
| 181 |
+
use_cuda=Hardware_Type
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
# except:
|
| 185 |
+
# st.warning("Cuda not available, Please checking your hardware")
|
| 186 |
+
# Get the sample images
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
main()
|
| 190 |
+
# Display the selected image
|
| 191 |
+
|
| 192 |
aib_logo = Image.open(r'aib_logo.png')
|
| 193 |
add_logo = st.sidebar.image(aib_logo)
|