Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -97,82 +97,6 @@ class DicomAnalyzer:
|
|
| 97 |
print(f"Error drawing circle: {str(e)}")
|
| 98 |
return image
|
| 99 |
|
| 100 |
-
def process_image1(self, file):
|
| 101 |
-
image, image_display, dicom_data = self.load_dicom(file)
|
| 102 |
-
self.current_image1 = image
|
| 103 |
-
self.image_display1 = image_display
|
| 104 |
-
self.dicom_data1 = dicom_data
|
| 105 |
-
return image_display
|
| 106 |
-
|
| 107 |
-
def process_image2(self, file):
|
| 108 |
-
image, image_display, dicom_data = self.load_dicom(file)
|
| 109 |
-
self.current_image2 = image
|
| 110 |
-
self.image_display2 = image_display
|
| 111 |
-
self.dicom_data2 = dicom_data
|
| 112 |
-
return image_display
|
| 113 |
-
|
| 114 |
-
def handle_click(self, evt: gr.SelectData, is_image1=True):
|
| 115 |
-
if is_image1 and self.current_image1 is None:
|
| 116 |
-
return self.image_display1, "Please load Image 1 first"
|
| 117 |
-
elif not is_image1 and self.current_image2 is None:
|
| 118 |
-
return self.image_display2, "Please load Image 2 first"
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
x, y = evt.index
|
| 122 |
-
if is_image1:
|
| 123 |
-
marked_image = self.draw_circle(self.image_display1, x, y, is_image1=True)
|
| 124 |
-
self.image_display1 = marked_image
|
| 125 |
-
results = self.analyze_point(self.current_image1, self.dicom_data1, x, y)
|
| 126 |
-
else:
|
| 127 |
-
marked_image = self.draw_circle(self.image_display2, x, y, is_image1=False)
|
| 128 |
-
self.image_display2 = marked_image
|
| 129 |
-
results = self.analyze_point(self.current_image2, self.dicom_data2, x, y)
|
| 130 |
-
|
| 131 |
-
if results:
|
| 132 |
-
results['Image'] = "Image 1" if is_image1 else "Image 2"
|
| 133 |
-
results['Point'] = f"({x}, {y})"
|
| 134 |
-
self.results.append(results)
|
| 135 |
-
|
| 136 |
-
return marked_image, self.format_results()
|
| 137 |
-
except Exception as e:
|
| 138 |
-
print(f"Error in handle_click: {str(e)}")
|
| 139 |
-
return (self.image_display1 if is_image1 else self.image_display2), f"Error: {str(e)}"
|
| 140 |
-
|
| 141 |
-
def format_results(self):
|
| 142 |
-
if not self.results:
|
| 143 |
-
return "No results yet"
|
| 144 |
-
df = pd.DataFrame(self.results)
|
| 145 |
-
return df.to_string()
|
| 146 |
-
|
| 147 |
-
def clear_results(self):
|
| 148 |
-
self.results = []
|
| 149 |
-
self.marks1 = []
|
| 150 |
-
self.marks2 = []
|
| 151 |
-
if self.current_image1 is not None:
|
| 152 |
-
self.image_display1 = cv2.cvtColor(
|
| 153 |
-
cv2.normalize(self.current_image1, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8),
|
| 154 |
-
cv2.COLOR_GRAY2BGR
|
| 155 |
-
)
|
| 156 |
-
if self.current_image2 is not None:
|
| 157 |
-
self.image_display2 = cv2.cvtColor(
|
| 158 |
-
cv2.normalize(self.current_image2, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8),
|
| 159 |
-
cv2.COLOR_GRAY2BGR
|
| 160 |
-
)
|
| 161 |
-
return "Results cleared", self.image_display1, self.image_display2
|
| 162 |
-
|
| 163 |
-
def save_results(self):
|
| 164 |
-
try:
|
| 165 |
-
if not self.results:
|
| 166 |
-
return None, "No results to save"
|
| 167 |
-
|
| 168 |
-
df = pd.DataFrame(self.results)
|
| 169 |
-
temp_file = os.path.join(tempfile.gettempdir(), "analysis_results.xlsx")
|
| 170 |
-
df.to_excel(temp_file, index=False, engine='openpyxl')
|
| 171 |
-
return temp_file, "Results saved successfully. Click to download."
|
| 172 |
-
except Exception as e:
|
| 173 |
-
print(f"Error saving results: {str(e)}")
|
| 174 |
-
return None, f"Error saving results: {str(e)}"
|
| 175 |
-
|
| 176 |
def update_circle_diameter(self, value):
|
| 177 |
self.circle_diameter = value
|
| 178 |
return f"Circle diameter set to {value}"
|
|
@@ -184,25 +108,14 @@ def create_interface():
|
|
| 184 |
gr.Markdown("# CT DICOM Image Analyzer")
|
| 185 |
|
| 186 |
with gr.Row():
|
| 187 |
-
file1 = gr.File(label="Upload
|
| 188 |
-
image1 = gr.Image(label="Image
|
| 189 |
-
file1.change(fn=analyzer.
|
| 190 |
-
|
| 191 |
-
file2 = gr.File(label="Upload second DICOM file")
|
| 192 |
-
image2 = gr.Image(label="Image 2", interactive=True, type="numpy")
|
| 193 |
-
file2.change(fn=analyzer.process_image2, inputs=file2, outputs=image2)
|
| 194 |
|
| 195 |
circle_diameter = gr.Slider(minimum=1, maximum=20, value=9, step=1, label="Circle Diameter")
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
results = gr.Textbox(label="Results", interactive=False)
|
| 199 |
-
clear_btn = gr.Button("Clear Results")
|
| 200 |
-
save_btn = gr.Button("Save Results")
|
| 201 |
|
| 202 |
-
|
| 203 |
-
image2.select(fn=lambda evt: analyzer.handle_click(evt, is_image1=False), outputs=[image2, results])
|
| 204 |
-
clear_btn.click(fn=analyzer.clear_results, outputs=[results, image1, image2])
|
| 205 |
-
save_btn.click(fn=analyzer.save_results, outputs=["file", results])
|
| 206 |
|
| 207 |
return interface
|
| 208 |
|
|
|
|
| 97 |
print(f"Error drawing circle: {str(e)}")
|
| 98 |
return image
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def update_circle_diameter(self, value):
|
| 101 |
self.circle_diameter = value
|
| 102 |
return f"Circle diameter set to {value}"
|
|
|
|
| 108 |
gr.Markdown("# CT DICOM Image Analyzer")
|
| 109 |
|
| 110 |
with gr.Row():
|
| 111 |
+
file1 = gr.File(label="Upload DICOM file")
|
| 112 |
+
image1 = gr.Image(label="DICOM Image", interactive=True, type="numpy")
|
| 113 |
+
file1.change(fn=analyzer.load_dicom, inputs=file1, outputs=image1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
circle_diameter = gr.Slider(minimum=1, maximum=20, value=9, step=1, label="Circle Diameter")
|
| 116 |
+
status = gr.Textbox(label="Status", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
circle_diameter.change(fn=analyzer.update_circle_diameter, inputs=circle_diameter, outputs=status)
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
return interface
|
| 121 |
|