임시 파일을 생성하고 삭제

This commit is contained in:
2025-09-03 17:56:05 +09:00
parent 13227e6214
commit e9936daad8

20
app.py
View File

@@ -5,6 +5,7 @@ from PIL import Image
import json import json
import os import os
import uuid import uuid
import numpy as np
# 페이지 설정 # 페이지 설정
st.set_page_config(page_title="PaddleOCR 이미지 분석", layout="wide") st.set_page_config(page_title="PaddleOCR 이미지 분석", layout="wide")
@@ -56,16 +57,9 @@ if uploaded_file is not None:
# 분석 시작 버튼 # 분석 시작 버튼
if st.button("분석 시작하기", use_container_width=True): if st.button("분석 시작하기", use_container_width=True):
with st.spinner("이미지를 분석하고 있습니다..."): with st.spinner("이미지를 분석하고 있습니다..."):
# PIL 이미지를 바이트로 변환하여 예측 # PIL 이미지를 numpy 배열로 변환하여 예측
# PPStructureV3는 파일 경로 또는 numpy 배열을 입력으로 받습니다. img_array = np.array(image)
# 업로드된 파일을 임시 저장하여 경로를 전달합니다. output = structure.predict(input=img_array)
temp_image_path = output_dir / f"temp_{unique_id}_{uploaded_file.name}"
# 변환된 이미지를 임시 파일로 저장
image.save(temp_image_path)
# PPStructureV3로 예측 수행
output = structure.predict(input=str(temp_image_path))
if not output: if not output:
st.warning("이미지에서 구조를 감지하지 못했습니다.") st.warning("이미지에서 구조를 감지하지 못했습니다.")
@@ -165,9 +159,9 @@ if uploaded_file is not None:
with st.expander(f"`{json_path.name}` 전체 내용 보기"): with st.expander(f"`{json_path.name}` 전체 내용 보기"):
st.json(json_data) st.json(json_data)
# 임시 파일 삭제 # 임시 파일 삭제 (이제 필요 없음)
if temp_image_path.exists(): # if temp_image_path.exists():
os.remove(temp_image_path) # os.remove(temp_image_path)
except Exception as e: except Exception as e:
st.error(f"이미지 처리 중 예상치 못한 오류가 발생했습니다: {e}") st.error(f"이미지 처리 중 예상치 못한 오류가 발생했습니다: {e}")