111 lines
4.6 KiB
Python
111 lines
4.6 KiB
Python
import json
|
|
import os
|
|
|
|
import streamlit as st
|
|
from PIL import Image
|
|
|
|
st.set_page_config(layout="wide")
|
|
|
|
st.title("OCR 결과 비교 시각화 도구")
|
|
st.write("`flow.py` 실행 후 생성된 `ocr_compare_*.json` 파일을 업로드하세요.")
|
|
|
|
uploaded_file = st.file_uploader("JSON 파일 선택", type="json")
|
|
|
|
if uploaded_file is not None:
|
|
try:
|
|
data = json.load(uploaded_file)
|
|
|
|
if not data:
|
|
st.warning("JSON 파일에 처리된 문서가 없습니다.")
|
|
else:
|
|
# 문서 선택 드롭다운
|
|
source_images = [
|
|
item.get("source_image", f"문서 {i+1}") for i, item in enumerate(data)
|
|
]
|
|
selected_doc_name = st.selectbox("분석할 문서를 선택하세요:", source_images)
|
|
|
|
selected_doc = next(
|
|
(
|
|
item
|
|
for item in data
|
|
if item.get("source_image") == selected_doc_name
|
|
),
|
|
None,
|
|
)
|
|
|
|
if selected_doc:
|
|
st.header(f"📄 원본 문서: `{selected_doc['source_image']}`")
|
|
|
|
cropped_regions = selected_doc.get("cropped_regions", [])
|
|
|
|
if not cropped_regions:
|
|
st.warning("이 문서에는 추출된 영역이 없습니다.")
|
|
else:
|
|
st.subheader("✂️ 영역별 OCR 결과 비교")
|
|
|
|
for i, region in enumerate(cropped_regions):
|
|
st.markdown("---")
|
|
|
|
region_image_path_from_json = region.get("region_image_path")
|
|
|
|
# JSON에 저장된 경로는 실행 환경에 따라 다를 수 있으므로, 파일 이름만 추출하여
|
|
# 이 스크립트가 위치한 곳의 'crops' 디렉토리 기준으로 경로를 재구성합니다.
|
|
image_filename = os.path.basename(region_image_path_from_json)
|
|
current_script_dir = os.path.dirname(__file__)
|
|
local_image_path = os.path.join(
|
|
current_script_dir, "crops", image_filename
|
|
)
|
|
|
|
if os.path.exists(local_image_path):
|
|
image = Image.open(local_image_path)
|
|
|
|
col1, col2, col3 = st.columns([1, 2, 2])
|
|
|
|
with col1:
|
|
st.image(
|
|
image,
|
|
caption=f"영역 #{i+1} ({region.get('label')})",
|
|
use_column_width=True,
|
|
)
|
|
st.write(f"**BBox:** `{region.get('bbox')}`")
|
|
st.write(
|
|
f"**Page:** `{region.get('page_index', 0) + 1}`"
|
|
)
|
|
|
|
with col2:
|
|
st.markdown("##### **Paddle OCR 결과**")
|
|
paddle_result = region.get("paddle_ocr_result", "N/A")
|
|
st.text_area(
|
|
"Paddle",
|
|
paddle_result,
|
|
height=200,
|
|
key=f"paddle_{i}_{selected_doc_name}",
|
|
)
|
|
|
|
with col3:
|
|
st.markdown("##### **Pytesseract OCR 결과**")
|
|
pytesseract_result = region.get(
|
|
"pytesseract_ocr_result", "N/A"
|
|
)
|
|
st.text_area(
|
|
"Pytesseract",
|
|
pytesseract_result,
|
|
height=200,
|
|
key=f"pytesseract_{i}_{selected_doc_name}",
|
|
)
|
|
else:
|
|
st.warning(
|
|
f"이미지 파일을 찾을 수 없습니다: `{local_image_path}`"
|
|
)
|
|
st.info(
|
|
f"(JSON 원본 경로: `{region_image_path_from_json}`)"
|
|
)
|
|
|
|
except json.JSONDecodeError:
|
|
st.error("올바른 JSON 파일이 아닙니다. 파일이 손상되었을 수 있습니다.")
|
|
except Exception as e:
|
|
st.error(f"파일을 처리하는 중 오류가 발생했습니다: {e}")
|
|
|
|
else:
|
|
st.info("시각화할 OCR 결과 파일을 업로드해주세요.")
|