Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,9 @@ import os
|
|
6 |
import subprocess
|
7 |
from datetime import datetime
|
8 |
import shutil
|
9 |
-
import moviepy.
|
|
|
|
|
10 |
|
11 |
|
12 |
# Flag to track if subprocess commands have been executed
|
@@ -53,24 +55,29 @@ def run_inference():
|
|
53 |
print(f"Error while running the script: {e}")
|
54 |
|
55 |
# Function to read and display the DICOM image
|
56 |
-
|
57 |
-
|
58 |
-
dicom_data = pydicom.dcmread(dicom_files[selected_slice])
|
59 |
-
|
60 |
-
# Display the DICOM image
|
61 |
plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
|
62 |
plt.axis("off")
|
63 |
plt.title("DICOM Image")
|
64 |
plt.tight_layout()
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
if uploaded_zip_file is not None:
|
68 |
try:
|
69 |
install_dependencies()
|
70 |
|
71 |
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
|
|
|
72 |
|
73 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
74 |
full_path = os.path.abspath(temp_dir)
|
75 |
|
76 |
st.write(f"Temporary directory path: {full_path}")
|
@@ -82,15 +89,27 @@ if uploaded_zip_file is not None:
|
|
82 |
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
83 |
dicom_files.sort() # Sort the files
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
except Exception as e:
|
96 |
st.error(f"Error: {str(e)}")
|
|
|
6 |
import subprocess
|
7 |
from datetime import datetime
|
8 |
import shutil
|
9 |
+
import moviepy.video.io.ImageSequenceClip
|
10 |
+
from io import BytesIO
|
11 |
+
from tkinter import Tcl
|
12 |
|
13 |
|
14 |
# Flag to track if subprocess commands have been executed
|
|
|
55 |
print(f"Error while running the script: {e}")
|
56 |
|
57 |
# Function to read and display the DICOM image
|
58 |
+
def convert_dicom_to_png(dicom_data, output_dir, index):
|
59 |
+
# Convert DICOM to a PNG image
|
|
|
|
|
|
|
60 |
plt.imshow(dicom_data.pixel_array, cmap=plt.cm.bone)
|
61 |
plt.axis("off")
|
62 |
plt.title("DICOM Image")
|
63 |
plt.tight_layout()
|
64 |
+
img_buffer = BytesIO()
|
65 |
+
plt.savefig(img_buffer, format="png")
|
66 |
+
img_buffer.seek(0)
|
67 |
+
output_path = os.path.join(output_dir, f"image_{index:03d}.png")
|
68 |
+
with open(output_path, "wb") as f:
|
69 |
+
f.write(img_buffer.read())
|
70 |
+
return output_path
|
71 |
|
72 |
if uploaded_zip_file is not None:
|
73 |
try:
|
74 |
install_dependencies()
|
75 |
|
76 |
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
|
77 |
+
output_image_dir = "/home/user/app/C2C/images_for_video"
|
78 |
|
79 |
os.makedirs(temp_dir, exist_ok=True)
|
80 |
+
os.makedirs(output_image_dir, exist_ok=True)
|
81 |
full_path = os.path.abspath(temp_dir)
|
82 |
|
83 |
st.write(f"Temporary directory path: {full_path}")
|
|
|
89 |
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
90 |
dicom_files.sort() # Sort the files
|
91 |
|
92 |
+
if len(dicom_files) == 0:
|
93 |
+
st.error("No DICOM files found in the ZIP archive.")
|
94 |
+
else:
|
95 |
+
# Convert DICOM slices to PNG frames and save them to the output folder
|
96 |
+
for i, dicom_file in enumerate(dicom_files):
|
97 |
+
dicom_data = pydicom.dcmread(dicom_file)
|
98 |
+
convert_dicom_to_png(dicom_data, output_image_dir, i)
|
99 |
+
|
100 |
+
image_folder = output_image_dir
|
101 |
+
fps = 20
|
102 |
+
image_files = [
|
103 |
+
os.path.join(image_folder, img)
|
104 |
+
for img in Tcl().call("lsort", "-dict", os.listdir(image_folder))
|
105 |
+
if img.endswith(".png")
|
106 |
+
]
|
107 |
+
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(
|
108 |
+
image_files, fps=fps
|
109 |
+
)
|
110 |
+
clip.write_videofile("aaa.mp4")
|
111 |
+
|
112 |
+
st.video("aaa.mp4", format="video/mp4")
|
113 |
|
114 |
except Exception as e:
|
115 |
st.error(f"Error: {str(e)}")
|