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