Spaces:
Sleeping
Sleeping
import os | |
import sys | |
import tempfile | |
import pyvista as pv | |
import streamlit as st | |
import subprocess | |
from stpyvista import stpyvista | |
from PIL import Image | |
from streamlit_image_zoom import image_zoom | |
######### Import PATH ####### | |
script_dir = os.path.dirname(os.path.abspath(__file__)) | |
imgto3d_path = os.path.join(script_dir, '..', 'image_to_3D') | |
visualize_x3d = os.path.join(script_dir, '..', 'visualize_x3d') | |
sys.path.append(imgto3d_path) | |
sys.path.append(visualize_x3d) | |
######### Preprocessing DICOM Image ######### | |
from visualize_x3d.dicom_toImg import convert_dcm_to_png | |
######### Convert Image to 3D ######### | |
from image_to_3D.ui import process_image | |
try: | |
import torchmcubes | |
import torch | |
import torchvision | |
import fpdf | |
except ImportError: | |
subprocess.check_call(['pip', 'install', 'git+https://github.com/tatsy/torchmcubes.git']) | |
subprocess.check_call(['pip', 'install','fpdf']) | |
status_file=False | |
st.markdown("<h1 style='text-align: center;'>Chào mừng tới Lung x3D Viewer 🎈</h1>", unsafe_allow_html=True) | |
col_1, col_3,col_2 = st.columns([7.4,0.3,10]) | |
with st.sidebar: | |
uploaded_files = st.file_uploader("Truyền ảnh vào đây", type=["png", "jpg", "jpeg", "dicom"], key="Uploader_sidebar") | |
with col_1: | |
with st.expander("Intructions"): | |
st.markdown("1. Để mở ảnh thì bạn có thể ấn vào chọn **Browse Files** và duyệt file. Hoặc bạn có thể chọn **Drag and Drop** và chuyển file vào đó.") | |
st.markdown("2. Sau khi đã truyền ảnh vào hãy ấn **Convert to 3D**. Sau đó vui lòng đợi một lúc để hệ thống xử lý ảnh và đưa ra hình 3D") | |
if uploaded_files: | |
# Handle single file or multiple files | |
if not isinstance(uploaded_files, list): | |
uploaded_files = [uploaded_files] # Convert single file to list | |
for uploaded_file in uploaded_files: | |
# Check the type of uploaded file | |
if hasattr(uploaded_file, 'name'): | |
file_type = uploaded_file.name.split('.')[-1].lower() | |
else: | |
file_type = 'unknown' | |
if file_type in ["jpg", "jpeg", "png"]: | |
img = Image.open(uploaded_file) | |
st.markdown("<div style='display: flex; justify-content: center;'>", unsafe_allow_html=True) | |
width, height = img.size | |
image_zoom(img, mode="both") | |
st.markdown("</div>", unsafe_allow_html=True) | |
btn_convert=st.button("Convert to 3D") | |
if btn_convert: | |
output_file = process_image(uploaded_file, output_filename='temp_image_3d') | |
status_file=True | |
elif file_type in ["dicom", "dcm"]: | |
convert_dcm_to_png(uploaded_file) | |
img = Image.open('a.png').convert('RGB') | |
st.markdown("<div style='display: flex; justify-content: center;'>", unsafe_allow_html=True) | |
width, height = img.size | |
image_zoom(img, mode="both", size=(width // 5, height // 5), keep_aspect_ratio=True, | |
zoom_factor=4.0, increment=0.2) | |
st.markdown("</div>", unsafe_allow_html=True) | |
btn_convert = st.button("Convert to 3D") | |
if btn_convert: | |
output_file = process_image("a.png", output_filename='temp_image_3d') | |
status_file=True | |
else: | |
st.info("Bạn cần truyền ảnh và quét ảnh trước khi xem ảnh 3D") | |
############### VISUALIZE_x3D file ######################### | |
from visualize_x3d.visualize_3d_file import option_stl_1 | |
from visualize_x3d.visualize_3d_file import option_stl_2 | |
from visualize_x3d.glt2stl import convertgtb2stl | |
with col_2: | |
with st.expander("Intructions"): | |
st.markdown("Truyền ảnh vào sau đó ấn nút sau để xem ảnh 3D") | |
if status_file==False: | |
option_stl_1() | |
if status_file==True: | |
file_path=r"temp_image_3d.glb" | |
convertgtb2stl(file_path,"temp_image_3d.stl") | |
option_stl_2("temp_image_3d.stl") | |