ammariii08 commited on
Commit
e28d8f1
·
verified ·
1 Parent(s): 8161e32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -6,6 +6,7 @@ from sahi import AutoDetectionModel
6
  from sahi.predict import get_sliced_prediction
7
  from pathlib import Path
8
 
 
9
  detection_model = AutoDetectionModel.from_pretrained(
10
  model_type='ultralytics',
11
  model_path="./DDR.pt", # Replace with your model path
@@ -16,7 +17,17 @@ detection_model = AutoDetectionModel.from_pretrained(
16
  OUTPUT_PATH = "./pred_image.jpg"
17
  TEMP_PNG_PATH = "./pred_image.png"
18
 
 
 
 
 
 
 
 
 
 
19
  def run_inference(image):
 
20
  result = get_sliced_prediction(
21
  image,
22
  detection_model,
@@ -26,16 +37,17 @@ def run_inference(image):
26
  overlap_width_ratio=0.2
27
  )
28
 
 
29
  result.export_visuals(export_dir=Path(TEMP_PNG_PATH).parent, file_name=Path(TEMP_PNG_PATH).name)
30
 
31
- time.sleep(2)
32
-
33
- if not Path(TEMP_PNG_PATH).exists():
34
  raise FileNotFoundError(f"SAHI did not save the PNG file at {TEMP_PNG_PATH}")
35
 
 
36
  processed_image = cv2.imread(TEMP_PNG_PATH)
37
  cv2.imwrite(OUTPUT_PATH, processed_image)
38
- Path(TEMP_PNG_PATH).unlink()
39
 
40
  return OUTPUT_PATH
41
 
@@ -44,7 +56,7 @@ demo = gr.Interface(
44
  inputs=gr.Image(type="numpy"),
45
  outputs=gr.Image(type="filepath"),
46
  title="YOLO11 Object Detection",
47
- description="Upload a DDR image to run inference using YOLO11"
48
  )
49
 
50
- demo.launch()
 
6
  from sahi.predict import get_sliced_prediction
7
  from pathlib import Path
8
 
9
+ # Load the detection model
10
  detection_model = AutoDetectionModel.from_pretrained(
11
  model_type='ultralytics',
12
  model_path="./DDR.pt", # Replace with your model path
 
17
  OUTPUT_PATH = "./pred_image.jpg"
18
  TEMP_PNG_PATH = "./pred_image.png"
19
 
20
+ def wait_for_file(file_path, timeout=10):
21
+ """Poll for the file to exist until the timeout (in seconds) is reached."""
22
+ start_time = time.time()
23
+ while not Path(file_path).exists():
24
+ if time.time() - start_time > timeout:
25
+ return False
26
+ time.sleep(0.5)
27
+ return True
28
+
29
  def run_inference(image):
30
+ # Perform sliced prediction on the input image.
31
  result = get_sliced_prediction(
32
  image,
33
  detection_model,
 
37
  overlap_width_ratio=0.2
38
  )
39
 
40
+ # Export visualization to a temporary PNG file.
41
  result.export_visuals(export_dir=Path(TEMP_PNG_PATH).parent, file_name=Path(TEMP_PNG_PATH).name)
42
 
43
+ # Wait for the PNG file to be created.
44
+ if not wait_for_file(TEMP_PNG_PATH, timeout=10):
 
45
  raise FileNotFoundError(f"SAHI did not save the PNG file at {TEMP_PNG_PATH}")
46
 
47
+ # Read the PNG image, convert it to JPG, and remove the temporary file.
48
  processed_image = cv2.imread(TEMP_PNG_PATH)
49
  cv2.imwrite(OUTPUT_PATH, processed_image)
50
+ Path(TEMP_PNG_PATH).unlink() # Delete the temporary PNG
51
 
52
  return OUTPUT_PATH
53
 
 
56
  inputs=gr.Image(type="numpy"),
57
  outputs=gr.Image(type="filepath"),
58
  title="YOLO11 Object Detection",
59
+ description="Upload an image to run inference using YOLO11"
60
  )
61
 
62
+ demo.launch(share=True)