ammariii08 commited on
Commit
36d27d7
·
verified ·
1 Parent(s): e28d8f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -52
app.py CHANGED
@@ -1,62 +1,26 @@
1
  import gradio as gr
 
2
  import cv2
3
- import time
4
  import numpy as np
5
- from sahi import AutoDetectionModel
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
13
- confidence_threshold=0.01,
14
- device="cpu" # Change to 'cuda:0' if you have a GPU
15
- )
16
-
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,
34
- slice_height=256,
35
- slice_width=256,
36
- overlap_height_ratio=0.2,
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
 
54
- demo = gr.Interface(
55
- fn=run_inference,
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)
 
 
1
  import gradio as gr
2
+ import torch
3
  import cv2
 
4
  import numpy as np
5
+ from ultralytics import YOLO
 
 
6
 
7
+ def load_model(model_path="DDR.pt"):
8
+ return YOLO(model_path)
 
 
 
 
 
 
 
 
9
 
10
+ model = load_model()
 
 
 
 
 
 
 
11
 
12
+ def predict(image):
13
+ results = model(image, conf=0.01)
14
+ pred_img = results[0].plot() # Visualize detections
15
+ return pred_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ iface = gr.Interface(
18
+ fn=predict,
19
  inputs=gr.Image(type="numpy"),
20
+ outputs=gr.Image(type="numpy"),
21
+ title="DDR-Detection",
22
+ description="Upload an image, and the model will detect objects using YOLO11.",
23
  )
24
 
25
+ if __name__ == "__main__":
26
+ iface.launch()