Update app.py
#1
by
K1Z3M1112
- opened
app.py
CHANGED
|
@@ -3,62 +3,113 @@ from PIL import ImageDraw
|
|
| 3 |
from PIL import Image
|
| 4 |
import streamlit as st
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
|
| 8 |
def load_image(image_file):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
def init_session_states():
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Not required as they are already installed through requirements and also seems to cause errors
|
| 20 |
-
# os.system('pip install git+git://github.com/jaidedai/easyocr.git')
|
| 21 |
-
# os.system('pip install git+https://github.com/huggingface/transformers.git --upgrade')
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
init_session_states()
|
| 26 |
-
import easyocr
|
| 27 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 28 |
-
|
| 29 |
-
def text_recognition(image):
|
| 30 |
-
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 31 |
-
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
| 32 |
-
#processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
|
| 33 |
-
#model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def main():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if uploaded_file is not None:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
st.write(file_details)
|
|
|
|
|
|
|
| 47 |
image = load_image(uploaded_file)
|
| 48 |
-
st.image(image,width=500)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import streamlit as st
|
| 5 |
import os
|
| 6 |
+
import easyocr
|
| 7 |
|
| 8 |
def load_image(image_file):
|
| 9 |
+
img = PIL.Image.open(image_file)
|
| 10 |
+
return img
|
| 11 |
|
| 12 |
def init_session_states():
|
| 13 |
+
if 'disp' not in st.session_state:
|
| 14 |
+
st.session_state['disp'] = st.empty()
|
| 15 |
+
st.session_state['disp'].text("กำลังตั้งค่า Environment...")
|
| 16 |
+
if 'init' not in st.session_state:
|
| 17 |
+
st.session_state['init'] = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
init_session_states()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def draw_bounding_boxes(image, results):
|
| 22 |
+
"""วาด bounding boxes บนภาพ"""
|
| 23 |
+
draw = ImageDraw.Draw(image)
|
| 24 |
+
|
| 25 |
+
for (bbox, text, confidence) in results:
|
| 26 |
+
# แปลง coordinates
|
| 27 |
+
top_left = tuple(bbox[0])
|
| 28 |
+
bottom_right = tuple(bbox[2])
|
| 29 |
+
|
| 30 |
+
# วาดสี่เหลี่ยม
|
| 31 |
+
draw.rectangle([top_left, bottom_right], outline="red", width=3)
|
| 32 |
+
|
| 33 |
+
# วาดข้อความ
|
| 34 |
+
draw.text(top_left, f"{text} ({confidence:.2f})", fill="blue")
|
| 35 |
+
|
| 36 |
+
return image
|
| 37 |
|
| 38 |
def main():
|
| 39 |
+
st.session_state['disp'].text("Environment พร้อมใช้งานแล้ว!")
|
| 40 |
+
|
| 41 |
+
st.title("📝 Image Text Detection")
|
| 42 |
+
st.write("ใช้ EasyOCR สำหรับตรวจจับและจดจำข้อความจากภาพ")
|
| 43 |
+
|
| 44 |
+
# เลือกภาษา
|
| 45 |
+
st.sidebar.subheader("🌐 เลือกภาษา")
|
| 46 |
+
languages = {
|
| 47 |
+
"English": "en",
|
| 48 |
+
"Thai": "th",
|
| 49 |
+
"Chinese Simplified": "ch_sim",
|
| 50 |
+
"Japanese": "ja",
|
| 51 |
+
"Korean": "ko",
|
| 52 |
+
"French": "fr",
|
| 53 |
+
"German": "de"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
selected_langs = st.sidebar.multiselect(
|
| 57 |
+
"เลือกภาษาที่ต้องการตรวจจับ:",
|
| 58 |
+
options=list(languages.keys()),
|
| 59 |
+
default=["English", "Thai"]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# แปลงเป็นรหัสภาษา
|
| 63 |
+
lang_codes = [languages[lang] for lang in selected_langs]
|
| 64 |
+
|
| 65 |
+
uploaded_file = st.file_uploader("เลือกไฟล์ภาพ", type=['jpeg','jpg','png'])
|
| 66 |
+
|
| 67 |
if uploaded_file is not None:
|
| 68 |
+
# แสดงรายละเอียดไฟล์
|
| 69 |
+
file_details = {
|
| 70 |
+
"ชื่อไฟล์": uploaded_file.name,
|
| 71 |
+
"ประเภท": uploaded_file.type,
|
| 72 |
+
"ขนาด": f"{uploaded_file.size / 1024:.2f} KB",
|
| 73 |
+
"ภาษาที่ตรวจจับ": ", ".join(selected_langs)
|
| 74 |
+
}
|
| 75 |
st.write(file_details)
|
| 76 |
+
|
| 77 |
+
# โหลดและแสดงภาพ
|
| 78 |
image = load_image(uploaded_file)
|
| 79 |
+
st.image(image, width=500, caption="ภาพต้นฉบับ")
|
| 80 |
+
|
| 81 |
+
# ตรวจจับข้อความ
|
| 82 |
+
with st.spinner(f"กำลังตรวจจับข้อความใน {len(selected_langs)} ภาษา..."):
|
| 83 |
+
reader = easyocr.Reader(lang_codes, gpu=False)
|
| 84 |
+
results = reader.readtext(image)
|
| 85 |
+
|
| 86 |
+
st.success("ตรวจจับข้อความสำเร็จ!")
|
| 87 |
+
|
| 88 |
+
# แสดงผลลัพธ์
|
| 89 |
+
st.subheader("🔍 ผลลัพธ์การตรวจจับ")
|
| 90 |
+
|
| 91 |
+
# วาด bounding boxes
|
| 92 |
+
if results:
|
| 93 |
+
annotated_image = draw_bounding_boxes(image.copy(), results)
|
| 94 |
+
st.image(annotated_image, width=500, caption="ภาพที่มีการวาด Bounding Boxes")
|
| 95 |
+
|
| 96 |
+
# แสดงข้อความที่ตรวจจับได้
|
| 97 |
+
st.subheader("📄 ข้อความที่ตรวจจับได้")
|
| 98 |
+
|
| 99 |
+
for i, (bbox, text, confidence) in enumerate(results):
|
| 100 |
+
st.write(f"**ข้อความ {i+1}:**")
|
| 101 |
+
st.write(f"- ข้อความ: `{text}`")
|
| 102 |
+
st.write(f"- ความมั่นใจ: `{confidence:.2%}`")
|
| 103 |
+
st.write(f"- ตำแหน่ง: {bbox}")
|
| 104 |
+
st.write("---")
|
| 105 |
+
|
| 106 |
+
# รวมข้อความทั้งหมด
|
| 107 |
+
all_text = " ".join([text for (_, text, _) in results])
|
| 108 |
+
st.subheader("📝 ข้อความรวม")
|
| 109 |
+
st.text_area("ข้อความที่ตรวจจับได้ทั้งหมด:", all_text, height=100)
|
| 110 |
+
|
| 111 |
+
else:
|
| 112 |
+
st.warning("❌ ไม่พบข้อความในภาพนี้")
|
| 113 |
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
main()
|