HeshamAI commited on
Commit
5f7db75
·
verified ·
1 Parent(s): e29575b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -71
app.py CHANGED
@@ -64,7 +64,6 @@ class DicomAnalyzer:
64
  return None
65
 
66
  def reset_view(self):
67
- """Reset zoom and center the image"""
68
  self.zoom_factor = 1.0
69
  self.pan_x = 0
70
  self.pan_y = 0
@@ -73,12 +72,10 @@ class DicomAnalyzer:
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
 
@@ -129,10 +126,8 @@ class DicomAnalyzer:
129
  return self.original_display
130
 
131
  def handle_keyboard(self, key):
132
- """Handle keyboard inputs for pan"""
133
  try:
134
- print(f"Handling key press: {key}") # Debug print
135
- # Reduce pan amount for finer control
136
  pan_amount = int(10 * self.zoom_factor)
137
 
138
  if key == 'ArrowLeft':
@@ -148,12 +143,12 @@ class DicomAnalyzer:
148
  except Exception as e:
149
  print(f"Error handling keyboard input: {str(e)}")
150
  return self.display_image
151
- def analyze_roi(self, evt: gr.SelectData):
 
152
  try:
153
  if self.current_image is None:
154
  return None, "No image loaded"
155
 
156
- # Convert clicked coordinates to original image coordinates
157
  x = int((evt.index[0] + self.pan_x) / self.zoom_factor)
158
  y = int((evt.index[1] + self.pan_y) / self.zoom_factor)
159
 
@@ -258,7 +253,6 @@ def create_interface():
258
  label="ROI Diameter (pixels)"
259
  )
260
 
261
- # Zoom controls
262
  with gr.Row():
263
  zoom_in_btn = gr.Button("Zoom In (+)")
264
  zoom_out_btn = gr.Button("Zoom Out (-)")
@@ -277,7 +271,6 @@ def create_interface():
277
  file_output = gr.File(label="Download Results")
278
  key_press = gr.Textbox(visible=False, elem_id="key_press")
279
 
280
- # Instructions
281
  gr.Markdown("""
282
  ### Controls:
283
  - Use arrow keys to pan when zoomed in
@@ -286,71 +279,23 @@ def create_interface():
286
  """)
287
 
288
  # Event handlers
289
- file_input.change(
290
- fn=analyzer.load_dicom,
291
- inputs=file_input,
292
- outputs=[image_display, results_display]
293
- )
294
-
295
- image_display.select(
296
- fn=analyzer.analyze_roi,
297
- outputs=[image_display, results_display]
298
- )
299
-
300
- diameter_slider.change(
301
- fn=lambda x: (analyzer.circle_diameter := x, f"Diameter set to {x} pixels")[1],
302
- inputs=diameter_slider,
303
- outputs=gr.Textbox(label="Status")
304
- )
305
-
306
- zoom_in_btn.click(
307
- fn=analyzer.zoom_in,
308
- inputs=image_display,
309
- outputs=image_display
310
- )
311
-
312
- zoom_out_btn.click(
313
- fn=analyzer.zoom_out,
314
- inputs=image_display,
315
- outputs=image_display
316
- )
317
-
318
- reset_btn.click(
319
- fn=analyzer.reset_view,
320
- outputs=image_display
321
- )
322
-
323
- key_press.change(
324
- fn=analyzer.handle_keyboard,
325
- inputs=key_press,
326
- outputs=image_display,
327
- api_name="handle_keyboard"
328
- )
329
-
330
- blank_btn.click(
331
- fn=analyzer.add_blank_row,
332
- inputs=image_display,
333
- outputs=[image_display, results_display]
334
- )
335
 
336
- zero_btn.click(
337
- fn=analyzer.add_zero_row,
338
- inputs=image_display,
339
- outputs=[image_display, results_display]
340
- )
341
 
342
- undo_btn.click(
343
- fn=analyzer.undo_last,
344
- inputs=image_display,
345
- outputs=[image_display, results_display]
346
- )
347
 
348
- save_btn.click(
349
- fn=analyzer.save_results,
350
- outputs=[file_output, results_display]
351
- )
352
 
353
- # JavaScript for keyboard handling
354
  js = """
355
  <script>
356
  document.addEventListener('keydown', function(e) {
 
64
  return None
65
 
66
  def reset_view(self):
 
67
  self.zoom_factor = 1.0
68
  self.pan_x = 0
69
  self.pan_y = 0
 
72
  return None
73
 
74
  def zoom_in(self, image):
 
75
  self.zoom_factor = min(20.0, self.zoom_factor + 0.5)
76
  return self.update_display()
77
 
78
  def zoom_out(self, image):
 
79
  self.zoom_factor = max(1.0, self.zoom_factor - 0.5)
80
  return self.update_display()
81
 
 
126
  return self.original_display
127
 
128
  def handle_keyboard(self, key):
 
129
  try:
130
+ print(f"Handling key press: {key}")
 
131
  pan_amount = int(10 * self.zoom_factor)
132
 
133
  if key == 'ArrowLeft':
 
143
  except Exception as e:
144
  print(f"Error handling keyboard input: {str(e)}")
145
  return self.display_image
146
+
147
+ def analyze_roi(self, evt: gr.SelectData):
148
  try:
149
  if self.current_image is None:
150
  return None, "No image loaded"
151
 
 
152
  x = int((evt.index[0] + self.pan_x) / self.zoom_factor)
153
  y = int((evt.index[1] + self.pan_y) / self.zoom_factor)
154
 
 
253
  label="ROI Diameter (pixels)"
254
  )
255
 
 
256
  with gr.Row():
257
  zoom_in_btn = gr.Button("Zoom In (+)")
258
  zoom_out_btn = gr.Button("Zoom Out (-)")
 
271
  file_output = gr.File(label="Download Results")
272
  key_press = gr.Textbox(visible=False, elem_id="key_press")
273
 
 
274
  gr.Markdown("""
275
  ### Controls:
276
  - Use arrow keys to pan when zoomed in
 
279
  """)
280
 
281
  # Event handlers
282
+ file_input.change(fn=analyzer.load_dicom, inputs=file_input, outputs=[image_display, results_display])
283
+ image_display.select(fn=analyzer.analyze_roi, outputs=[image_display, results_display])
284
+ diameter_slider.change(fn=lambda x: (analyzer.circle_diameter := x, f"Diameter set to {x} pixels")[1],
285
+ inputs=diameter_slider,
286
+ outputs=gr.Textbox(label="Status"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
 
288
+ zoom_in_btn.click(fn=analyzer.zoom_in, inputs=image_display, outputs=image_display)
289
+ zoom_out_btn.click(fn=analyzer.zoom_out, inputs=image_display, outputs=image_display)
290
+ reset_btn.click(fn=analyzer.reset_view, outputs=image_display)
 
 
291
 
292
+ key_press.change(fn=analyzer.handle_keyboard, inputs=key_press, outputs=image_display)
 
 
 
 
293
 
294
+ blank_btn.click(fn=analyzer.add_blank_row, inputs=image_display, outputs=[image_display, results_display])
295
+ zero_btn.click(fn=analyzer.add_zero_row, inputs=image_display, outputs=[image_display, results_display])
296
+ undo_btn.click(fn=analyzer.undo_last, inputs=image_display, outputs=[image_display, results_display])
297
+ save_btn.click(fn=analyzer.save_results, outputs=[file_output, results_display])
298
 
 
299
  js = """
300
  <script>
301
  document.addEventListener('keydown', function(e) {