HeshamAI commited on
Commit
599600e
·
verified ·
1 Parent(s): 3b4da09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -27
app.py CHANGED
@@ -72,6 +72,16 @@ class DicomAnalyzer:
72
  return self.update_display()
73
  return None
74
 
 
 
 
 
 
 
 
 
 
 
75
  def update_display(self):
76
  """Update display with current zoom and pan"""
77
  try:
@@ -98,12 +108,12 @@ class DicomAnalyzer:
98
  display_diameter = int(diameter * self.zoom_factor)
99
 
100
  # Only draw if mark is in view
101
- if (0 <= display_x < new_width and 0 <= display_y < new_height):
102
  cv2.circle(zoomed,
103
  (display_x, display_y),
104
  display_diameter // 2,
105
  (0, 255, 255), # Yellow in BGR
106
- 2, # Increase thickness for better visibility
107
  lineType=cv2.LINE_AA)
108
 
109
  # Extract visible portion
@@ -121,7 +131,7 @@ class DicomAnalyzer:
121
  """Handle keyboard inputs for pan"""
122
  try:
123
  # Pan amount depends on zoom level
124
- pan_amount = int(50 / self.zoom_factor)
125
 
126
  if key == 'ArrowLeft':
127
  self.pan_x = max(0, self.pan_x - pan_amount)
@@ -177,17 +187,6 @@ class DicomAnalyzer:
177
  print(f"Error analyzing ROI: {str(e)}")
178
  return self.display_image, f"Error analyzing ROI: {str(e)}"
179
 
180
- def update_zoom(self, zoom_factor, image):
181
- try:
182
- if self.original_display is None:
183
- return image
184
-
185
- self.zoom_factor = zoom_factor
186
- return self.update_display()
187
- except Exception as e:
188
- print(f"Error updating zoom: {str(e)}")
189
- return image
190
-
191
  def format_results(self):
192
  if not self.results:
193
  return "No measurements yet"
@@ -257,14 +256,12 @@ def create_interface():
257
  step=1,
258
  label="ROI Diameter (pixels)"
259
  )
260
- zoom_slider = gr.Slider(
261
- minimum=1.0,
262
- maximum=20.0,
263
- value=1.0,
264
- step=0.5,
265
- label="Zoom Factor"
266
- )
267
- reset_btn = gr.Button("Reset View")
268
 
269
  with gr.Column():
270
  image_display = gr.Image(label="DICOM Image", interactive=True, elem_id="image_display")
@@ -284,7 +281,7 @@ def create_interface():
284
  ### Controls:
285
  - Use arrow keys to pan when zoomed in
286
  - Click points to measure
287
- - Press 'Reset View' to center image and reset zoom
288
  """)
289
 
290
  def update_diameter(x):
@@ -309,9 +306,16 @@ def create_interface():
309
  outputs=gr.Textbox(label="Status")
310
  )
311
 
312
- zoom_slider.change(
313
- fn=analyzer.update_zoom,
314
- inputs=[zoom_slider, image_display],
 
 
 
 
 
 
 
315
  outputs=image_display
316
  )
317
 
@@ -367,4 +371,4 @@ def create_interface():
367
 
368
  if __name__ == "__main__":
369
  interface = create_interface()
370
- interface.launch()
 
72
  return self.update_display()
73
  return None
74
 
75
+ def zoom_in(self, image):
76
+ """Increase zoom factor"""
77
+ self.zoom_factor = min(20.0, self.zoom_factor + 0.5)
78
+ return self.update_display()
79
+
80
+ def zoom_out(self, image):
81
+ """Decrease zoom factor"""
82
+ self.zoom_factor = max(1.0, self.zoom_factor - 0.5)
83
+ return self.update_display()
84
+
85
  def update_display(self):
86
  """Update display with current zoom and pan"""
87
  try:
 
108
  display_diameter = int(diameter * self.zoom_factor)
109
 
110
  # Only draw if mark is in view
111
+ if (0 <= display_x < width and 0 <= display_y < height):
112
  cv2.circle(zoomed,
113
  (display_x, display_y),
114
  display_diameter // 2,
115
  (0, 255, 255), # Yellow in BGR
116
+ 1,
117
  lineType=cv2.LINE_AA)
118
 
119
  # Extract visible portion
 
131
  """Handle keyboard inputs for pan"""
132
  try:
133
  # Pan amount depends on zoom level
134
+ pan_amount = int(20 * self.zoom_factor)
135
 
136
  if key == 'ArrowLeft':
137
  self.pan_x = max(0, self.pan_x - pan_amount)
 
187
  print(f"Error analyzing ROI: {str(e)}")
188
  return self.display_image, f"Error analyzing ROI: {str(e)}"
189
 
 
 
 
 
 
 
 
 
 
 
 
190
  def format_results(self):
191
  if not self.results:
192
  return "No measurements yet"
 
256
  step=1,
257
  label="ROI Diameter (pixels)"
258
  )
259
+
260
+ # Zoom controls
261
+ with gr.Row():
262
+ zoom_in_btn = gr.Button("Zoom In (+)")
263
+ zoom_out_btn = gr.Button("Zoom Out (-)")
264
+ reset_btn = gr.Button("Reset View")
 
 
265
 
266
  with gr.Column():
267
  image_display = gr.Image(label="DICOM Image", interactive=True, elem_id="image_display")
 
281
  ### Controls:
282
  - Use arrow keys to pan when zoomed in
283
  - Click points to measure
284
+ - Use Zoom In/Out buttons or Reset View to adjust zoom level
285
  """)
286
 
287
  def update_diameter(x):
 
306
  outputs=gr.Textbox(label="Status")
307
  )
308
 
309
+ # Zoom button handlers
310
+ zoom_in_btn.click(
311
+ fn=analyzer.zoom_in,
312
+ inputs=image_display,
313
+ outputs=image_display
314
+ )
315
+
316
+ zoom_out_btn.click(
317
+ fn=analyzer.zoom_out,
318
+ inputs=image_display,
319
  outputs=image_display
320
  )
321
 
 
371
 
372
  if __name__ == "__main__":
373
  interface = create_interface()
374
+ interface.launch()