HeshamAI commited on
Commit
8543088
·
verified ·
1 Parent(s): 020f6f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -90
app.py CHANGED
@@ -59,21 +59,21 @@ class DicomAnalyzer:
59
  return None, f"Error loading DICOM file: {str(e)}"
60
 
61
  def normalize_image(self, image):
62
- try:
63
- normalized = cv2.normalize(
64
- image,
65
- None,
66
- alpha=0,
67
- beta=255,
68
- norm_type=cv2.NORM_MINMAX,
69
- dtype=cv2.CV_8U
70
- )
71
- if len(normalized.shape) == 2:
72
- normalized = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
73
- return normalized
74
- except Exception as e:
75
- print(f"Error normalizing image: {str(e)}")
76
- return None
77
 
78
  def reset_view(self):
79
  self.zoom_factor = 1.0
@@ -94,32 +94,32 @@ class DicomAnalyzer:
94
  return self.update_display()
95
 
96
  def handle_keyboard(self, key):
97
- try:
98
- print(f"Handling key press: {key}")
99
- pan_amount = int(5 * self.zoom_factor) # Reduced pan amount for smoother movement
100
-
101
- original_pan_x = self.pan_x
102
- original_pan_y = self.pan_y
103
-
104
- if key == 'ArrowLeft':
105
- self.pan_x = max(0, self.pan_x - pan_amount)
106
- elif key == 'ArrowRight':
107
- self.pan_x = min(self.max_pan_x, self.pan_x + pan_amount)
108
- elif key == 'ArrowUp':
109
- self.pan_y = max(0, self.pan_y - pan_amount)
110
- elif key == 'ArrowDown':
111
- self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
112
-
113
- # Print debug info
114
- print(f"Pan X: {self.pan_x} (was {original_pan_x})")
115
- print(f"Pan Y: {self.pan_y} (was {original_pan_y})")
116
- print(f"Max Pan X: {self.max_pan_x}")
117
- print(f"Max Pan Y: {self.max_pan_y}")
118
-
119
- return self.update_display()
120
- except Exception as e:
121
- print(f"Error handling keyboard input: {str(e)}")
122
- return self.display_image
123
 
124
  def analyze_roi(self, evt: gr.SelectData):
125
  try:
@@ -171,63 +171,63 @@ class DicomAnalyzer:
171
  return self.display_image, f"Error analyzing ROI: {str(e)}"
172
 
173
  def update_display(self):
174
- try:
175
- if self.original_display is None:
176
- return None
177
 
178
- height, width = self.original_display.shape[:2]
179
- new_height = int(height * self.zoom_factor)
180
- new_width = int(width * self.zoom_factor)
181
 
182
- # Create zoomed image
183
- zoomed = cv2.resize(self.original_display, (new_width, new_height),
184
- interpolation=cv2.INTER_CUBIC)
185
 
186
- # Draw marks
187
- for x, y, diameter in self.marks:
188
- zoomed_x = int(x * self.zoom_factor)
189
- zoomed_y = int(y * self.zoom_factor)
190
- zoomed_diameter = int(diameter * self.zoom_factor)
191
-
192
- # Draw main circle
193
- cv2.circle(zoomed,
194
- (zoomed_x, zoomed_y),
195
- zoomed_diameter // 2,
196
- (0, 255, 255), # BGR: Yellow
197
- 1,
198
- lineType=cv2.LINE_AA)
199
-
200
- # Draw dots on circle
201
- num_points = 8
202
- for i in range(num_points):
203
- angle = 2 * np.pi * i / num_points
204
- point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
205
- point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
206
  cv2.circle(zoomed,
207
- (point_x, point_y),
208
- 1,
209
  (0, 255, 255), # BGR: Yellow
210
- -1,
211
  lineType=cv2.LINE_AA)
 
 
 
 
 
 
 
 
 
 
 
 
 
212
 
213
- # Calculate pan limits
214
- self.max_pan_x = max(0, new_width - width)
215
- self.max_pan_y = max(0, new_height - height)
216
-
217
- # Ensure pan values are within bounds
218
- self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
219
- self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
220
-
221
- # Extract visible portion
222
- visible = zoomed[
223
- int(self.pan_y):int(self.pan_y + height),
224
- int(self.pan_x):int(self.pan_x + width)
225
- ]
226
 
227
- return visible
228
- except Exception as e:
229
- print(f"Error updating display: {str(e)}")
230
- return self.original_display
231
 
232
  def format_results(self):
233
  if not self.results:
 
59
  return None, f"Error loading DICOM file: {str(e)}"
60
 
61
  def normalize_image(self, image):
62
+ try:
63
+ normalized = cv2.normalize(
64
+ image,
65
+ None,
66
+ alpha=0,
67
+ beta=255,
68
+ norm_type=cv2.NORM_MINMAX,
69
+ dtype=cv2.CV_8U
70
+ )
71
+ if len(normalized.shape) == 2:
72
+ normalized = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
73
+ return normalized
74
+ except Exception as e:
75
+ print(f"Error normalizing image: {str(e)}")
76
+ return None
77
 
78
  def reset_view(self):
79
  self.zoom_factor = 1.0
 
94
  return self.update_display()
95
 
96
  def handle_keyboard(self, key):
97
+ try:
98
+ print(f"Handling key press: {key}")
99
+ pan_amount = int(5 * self.zoom_factor) # Reduced pan amount for smoother movement
100
+
101
+ original_pan_x = self.pan_x
102
+ original_pan_y = self.pan_y
103
+
104
+ if key == 'ArrowLeft':
105
+ self.pan_x = max(0, self.pan_x - pan_amount)
106
+ elif key == 'ArrowRight':
107
+ self.pan_x = min(self.max_pan_x, self.pan_x + pan_amount)
108
+ elif key == 'ArrowUp':
109
+ self.pan_y = max(0, self.pan_y - pan_amount)
110
+ elif key == 'ArrowDown':
111
+ self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
112
+
113
+ # Print debug info
114
+ print(f"Pan X: {self.pan_x} (was {original_pan_x})")
115
+ print(f"Pan Y: {self.pan_y} (was {original_pan_y})")
116
+ print(f"Max Pan X: {self.max_pan_x}")
117
+ print(f"Max Pan Y: {self.max_pan_y}")
118
+
119
+ return self.update_display()
120
+ except Exception as e:
121
+ print(f"Error handling keyboard input: {str(e)}")
122
+ return self.display_image
123
 
124
  def analyze_roi(self, evt: gr.SelectData):
125
  try:
 
171
  return self.display_image, f"Error analyzing ROI: {str(e)}"
172
 
173
  def update_display(self):
174
+ try:
175
+ if self.original_display is None:
176
+ return None
177
 
178
+ height, width = self.original_display.shape[:2]
179
+ new_height = int(height * self.zoom_factor)
180
+ new_width = int(width * self.zoom_factor)
181
 
182
+ # Create zoomed image
183
+ zoomed = cv2.resize(self.original_display, (new_width, new_height),
184
+ interpolation=cv2.INTER_CUBIC)
185
 
186
+ # Draw marks
187
+ for x, y, diameter in self.marks:
188
+ zoomed_x = int(x * self.zoom_factor)
189
+ zoomed_y = int(y * self.zoom_factor)
190
+ zoomed_diameter = int(diameter * self.zoom_factor)
191
+
192
+ # Draw main circle
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  cv2.circle(zoomed,
194
+ (zoomed_x, zoomed_y),
195
+ zoomed_diameter // 2,
196
  (0, 255, 255), # BGR: Yellow
197
+ 1,
198
  lineType=cv2.LINE_AA)
199
+
200
+ # Draw dots on circle
201
+ num_points = 8
202
+ for i in range(num_points):
203
+ angle = 2 * np.pi * i / num_points
204
+ point_x = int(zoomed_x + (zoomed_diameter/2) * np.cos(angle))
205
+ point_y = int(zoomed_y + (zoomed_diameter/2) * np.sin(angle))
206
+ cv2.circle(zoomed,
207
+ (point_x, point_y),
208
+ 1,
209
+ (0, 255, 255), # BGR: Yellow
210
+ -1,
211
+ lineType=cv2.LINE_AA)
212
 
213
+ # Calculate pan limits
214
+ self.max_pan_x = max(0, new_width - width)
215
+ self.max_pan_y = max(0, new_height - height)
216
+
217
+ # Ensure pan values are within bounds
218
+ self.pan_x = min(max(0, self.pan_x), self.max_pan_x)
219
+ self.pan_y = min(max(0, self.pan_y), self.max_pan_y)
220
+
221
+ # Extract visible portion
222
+ visible = zoomed[
223
+ int(self.pan_y):int(self.pan_y + height),
224
+ int(self.pan_x):int(self.pan_x + width)
225
+ ]
226
 
227
+ return visible
228
+ except Exception as e:
229
+ print(f"Error updating display: {str(e)}")
230
+ return self.original_display
231
 
232
  def format_results(self):
233
  if not self.results: