HeshamAI commited on
Commit
2c34aa3
·
verified ·
1 Parent(s): a6a3ff0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -91
app.py CHANGED
@@ -1,99 +1,69 @@
1
- def load_dicom(self, file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  try:
3
- if file is None:
4
- return None, "No file uploaded"
5
-
6
- if hasattr(file, 'name'):
7
- dicom_data = pydicom.dcmread(file.name)
8
- else:
9
- dicom_data = pydicom.dcmread(file)
10
-
11
- image = dicom_data.pixel_array.astype(np.float32)
12
-
13
- # Store original pixel values before any scaling
14
- self.original_image = image.copy()
15
-
16
- # Apply DICOM scaling for display
17
- rescale_slope = getattr(dicom_data, 'RescaleSlope', 1)
18
- rescale_intercept = getattr(dicom_data, 'RescaleIntercept', 0)
19
- image = (image * rescale_slope) + rescale_intercept
20
-
21
- self.current_image = image
22
- self.dicom_data = dicom_data
23
-
24
- self.display_image = self.normalize_image(image)
25
- self.original_display = self.display_image.copy()
26
-
27
- # Reset view on new image
28
- self.reset_view()
29
- print("DICOM file loaded successfully")
30
-
31
- return self.display_image, "DICOM file loaded successfully"
32
  except Exception as e:
33
- print(f"Error loading DICOM file: {str(e)}")
34
- return None, f"Error loading DICOM file: {str(e)}"
35
-
36
- def normalize_image(self, image):
37
- try:
38
- normalized = cv2.normalize(
39
- image,
40
- None,
41
- alpha=0,
42
- beta=255,
43
- norm_type=cv2.NORM_MINMAX,
44
- dtype=cv2.CV_8U
45
- )
46
- if len(normalized.shape) == 2:
47
- normalized = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
48
- return normalized
49
- except Exception as e:
50
- print(f"Error normalizing image: {str(e)}")
51
- return None
52
-
53
- def reset_view(self):
54
  self.zoom_factor = 1.0
 
 
 
 
 
 
 
55
  self.pan_x = 0
56
  self.pan_y = 0
57
- if self.original_display is not None:
58
- return self.update_display()
59
- return None
60
-
61
- def zoom_in(self, image):
62
- print("Zooming in...")
63
- self.zoom_factor = min(20.0, self.zoom_factor + 0.5)
64
- return self.update_display()
65
-
66
- def zoom_out(self, image):
67
- print("Zooming out...")
68
- self.zoom_factor = max(1.0, self.zoom_factor - 0.5)
69
- return self.update_display()
70
-
71
- def handle_keyboard(self, key):
72
- try:
73
- print(f"Handling key press: {key}")
74
- pan_amount = int(5 * self.zoom_factor)
75
-
76
- original_pan_x = self.pan_x
77
- original_pan_y = self.pan_y
78
-
79
- if key == 'ArrowLeft':
80
- self.pan_x = max(0, self.pan_x - pan_amount)
81
- elif key == 'ArrowRight':
82
- self.pan_x = min(self.max_pan_x, self.pan_x + pan_amount)
83
- elif key == 'ArrowUp':
84
- self.pan_y = max(0, self.pan_y - pan_amount)
85
- elif key == 'ArrowDown':
86
- self.pan_y = min(self.max_pan_y, self.pan_y + pan_amount)
87
-
88
- print(f"Pan X: {self.pan_x} (was {original_pan_x})")
89
- print(f"Pan Y: {self.pan_y} (was {original_pan_y})")
90
- print(f"Max Pan X: {self.max_pan_x}")
91
- print(f"Max Pan Y: {self.max_pan_y}")
92
-
93
- return self.update_display()
94
- except Exception as e:
95
- print(f"Error handling keyboard input: {str(e)}")
96
- return self.display_image
97
  def load_dicom(self, file):
98
  try:
99
  if file is None:
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import pandas as pd
5
+ import pydicom
6
+ import io
7
+ from PIL import Image
8
+ import openpyxl
9
+ from openpyxl.utils import get_column_letter, column_index_from_string
10
+ import logging
11
+ import time
12
+ import traceback
13
+ from functools import wraps
14
+ import sys
15
+
16
+ print("Starting imports completed...")
17
+
18
+ # Set up logging
19
+ logging.basicConfig(
20
+ level=logging.DEBUG,
21
+ format='%(asctime)s - %(levelname)s - %(message)s',
22
+ handlers=[
23
+ logging.FileHandler('dicom_analyzer_debug.log'),
24
+ logging.StreamHandler(sys.stdout)
25
+ ]
26
+ )
27
+
28
+ logger = logging.getLogger(__name__)
29
+
30
+ def debug_decorator(func):
31
+ @wraps(func)
32
+ def wrapper(*args, **kwargs):
33
+ logger.debug(f"Entering {func.__name__}")
34
+ start_time = time.time()
35
  try:
36
+ result = func(*args, **kwargs)
37
+ logger.debug(f"Function {func.__name__} completed successfully")
38
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  except Exception as e:
40
+ logger.error(f"Error in {func.__name__}: {str(e)}")
41
+ logger.error(traceback.format_exc())
42
+ raise
43
+ finally:
44
+ end_time = time.time()
45
+ logger.debug(f"Execution time: {end_time - start_time:.4f} seconds")
46
+ return wrapper
47
+
48
+ class DicomAnalyzer:
49
+ def __init__(self):
50
+ self.results = []
51
+ self.circle_diameter = 9.0 # Changed to float for precise calculations
 
 
 
 
 
 
 
 
 
52
  self.zoom_factor = 1.0
53
+ self.current_image = None
54
+ self.dicom_data = None
55
+ self.display_image = None
56
+ self.marks = [] # Store (x, y, diameter) for each mark
57
+ self.original_image = None
58
+ self.original_display = None
59
+ # Pan position
60
  self.pan_x = 0
61
  self.pan_y = 0
62
+ self.max_pan_x = 0
63
+ self.max_pan_y = 0
64
+ # Circle color in BGR
65
+ self.CIRCLE_COLOR = (0, 255, 255) # BGR Yellow
66
+ print("DicomAnalyzer initialized...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  def load_dicom(self, file):
68
  try:
69
  if file is None: