wangjin2000 commited on
Commit
8a0a2dc
·
verified ·
1 Parent(s): ae0e58e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -18,7 +18,7 @@ from yolov5.models.experimental import attempt_load
18
  from yolov5.utils.general import non_max_suppression
19
  from yolov5.utils.augmentations import letterbox
20
 
21
- #'''
22
  # Example URLs for downloading images
23
  file_urls = [
24
  "https://www.dropbox.com/scl/fi/n3bs5xnl2kanqmwv483k3/1_jpg.rf.4a59a63d0a7339d280dd18ef3c2e675a.jpg?rlkey=4n9dnls1byb4wm54ycxzx3ovi&st=ue5xv8yx&dl=0",
@@ -40,7 +40,7 @@ def download_file(url, save_name):
40
  # Download images
41
  for i, url in enumerate(file_urls):
42
  download_file(url, f"image_{i}.jpg")
43
-
44
 
45
  # Load YOLOv5 model (placeholder)
46
  model_path = "best.pt" # Path to your YOLOv5 model
@@ -48,12 +48,12 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Use GPU
48
  model = attempt_load(model_path, device=device) # Placeholder for model loading
49
  model.eval() # Set the model to evaluation mode
50
 
51
- def preprocess_image(image_path):
52
- #def preprocess_image(image):
53
- img0 = cv2.imread(image_path)
54
- print("in preprocess-0 img0.shape:",img0.shape)
55
- #img0 = letterbox(image, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
56
- img = letterbox(img0, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
57
  print("in preprocess-1 img.shape:",img.shape)
58
  img = img.transpose(2, 0, 1)[::-1] # Convert BGR to RGB, to 3x416x416
59
  img = np.ascontiguousarray(img)
@@ -97,9 +97,9 @@ def postprocess(pred, img0_shape, img):
97
  return results
98
 
99
  def detect_objects(image_path):
100
- #dicom_image, dicom_meta = read_and_preprocess_dicom(image_path)
101
- img, img0 = preprocess_image(image_path)
102
- #img, img0 = preprocess_image(dicom_image)
103
  pred = infer(model, img)
104
  results = postprocess(pred, img0.shape, img)
105
  return results
@@ -117,6 +117,7 @@ def show_preds_image(filepath):
117
  img_with_boxes = draw_bounding_boxes(img0, results)
118
  return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
119
 
 
120
  # Define Gradio components
121
  input_component = gr.components.Image(type="filepath", label="Input Image")
122
  output_component = gr.components.Image(type="numpy", label="Output Image")
@@ -170,16 +171,13 @@ def read_and_preprocess_dicom(file_path: str):
170
  # PIL images into NumPy arrays
171
  numpydata = np.asarray(image_pil)
172
  #convert to cv2 format
173
- numpydata = numpydata.reshape((numpydata.shape[0], numpydata.shape[1], 1))
174
  print("In preprocess dicom:", numpydata.shape)
175
  #image = np.array(numpydata)[::-1].copy()
176
  image = np.array(numpydata)[:,:,::-1].copy()
177
-
178
- # <class 'numpy.ndarray'>
179
- print(type(numpydata))
180
 
181
  # shape
182
- print("numpydata.shape:",numpydata.shape)
183
 
184
  # Collect metadata in dictionary format and convert to DataFrame
185
  metadata_dict = {elem.name: str(elem.value) for elem in dicom_data.iterall() if elem.name != 'Pixel Data'}
@@ -236,4 +234,4 @@ def build_interface():
236
  if __name__ == '__main__':
237
  demo = build_interface()
238
  demo.launch
239
- '''
 
18
  from yolov5.utils.general import non_max_suppression
19
  from yolov5.utils.augmentations import letterbox
20
 
21
+ '''
22
  # Example URLs for downloading images
23
  file_urls = [
24
  "https://www.dropbox.com/scl/fi/n3bs5xnl2kanqmwv483k3/1_jpg.rf.4a59a63d0a7339d280dd18ef3c2e675a.jpg?rlkey=4n9dnls1byb4wm54ycxzx3ovi&st=ue5xv8yx&dl=0",
 
40
  # Download images
41
  for i, url in enumerate(file_urls):
42
  download_file(url, f"image_{i}.jpg")
43
+ '''
44
 
45
  # Load YOLOv5 model (placeholder)
46
  model_path = "best.pt" # Path to your YOLOv5 model
 
48
  model = attempt_load(model_path, device=device) # Placeholder for model loading
49
  model.eval() # Set the model to evaluation mode
50
 
51
+ #def preprocess_image(image_path):
52
+ def preprocess_image(image):
53
+ #img0 = cv2.imread(image_path)
54
+ print("in preprocess-0 image.shape:",image.shape)
55
+ img = letterbox(image, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
56
+ #img = letterbox(img0, 640, stride=32, auto=True)[0] # Resize and pad to 640x640
57
  print("in preprocess-1 img.shape:",img.shape)
58
  img = img.transpose(2, 0, 1)[::-1] # Convert BGR to RGB, to 3x416x416
59
  img = np.ascontiguousarray(img)
 
97
  return results
98
 
99
  def detect_objects(image_path):
100
+ dicom_image, dicom_meta = read_and_preprocess_dicom(image_path)
101
+ #img, img0 = preprocess_image(image_path)
102
+ img, img0 = preprocess_image(dicom_image)
103
  pred = infer(model, img)
104
  results = postprocess(pred, img0.shape, img)
105
  return results
 
117
  img_with_boxes = draw_bounding_boxes(img0, results)
118
  return cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
119
 
120
+ '''
121
  # Define Gradio components
122
  input_component = gr.components.Image(type="filepath", label="Input Image")
123
  output_component = gr.components.Image(type="numpy", label="Output Image")
 
171
  # PIL images into NumPy arrays
172
  numpydata = np.asarray(image_pil)
173
  #convert to cv2 format
174
+ numpydata = numpydata.reshape((numpydata.shape[0], numpydata.shape[1], 3))
175
  print("In preprocess dicom:", numpydata.shape)
176
  #image = np.array(numpydata)[::-1].copy()
177
  image = np.array(numpydata)[:,:,::-1].copy()
 
 
 
178
 
179
  # shape
180
+ print("In preprocess dicom-image.shape:",image.shape)
181
 
182
  # Collect metadata in dictionary format and convert to DataFrame
183
  metadata_dict = {elem.name: str(elem.value) for elem in dicom_data.iterall() if elem.name != 'Pixel Data'}
 
234
  if __name__ == '__main__':
235
  demo = build_interface()
236
  demo.launch
237
+