wuhp commited on
Commit
f502e4f
·
verified ·
1 Parent(s): eb25988

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -18
app.py CHANGED
@@ -3,7 +3,7 @@ from ultralytics import YOLO
3
  import cv2
4
  import tempfile
5
 
6
- # Dynamic model loader: given a model identifier or path, it returns a YOLO model.
7
  def load_model(model_id: str):
8
  try:
9
  model = YOLO(model_id)
@@ -11,16 +11,16 @@ def load_model(model_id: str):
11
  except Exception as e:
12
  return f"Error loading model: {e}"
13
 
 
14
  def predict_image(model, image):
15
  try:
16
- # Run inference on the image
17
  results = model(image)
18
- # The plot() method overlays predictions (works across detection, segmentation, and OBB models)
19
- annotated_frame = results[0].plot()
20
  return annotated_frame
21
  except Exception as e:
22
  return f"Error during image inference: {e}"
23
 
 
24
  def predict_video(model, video_file):
25
  try:
26
  cap = cv2.VideoCapture(video_file.name)
@@ -47,14 +47,12 @@ def predict_video(model, video_file):
47
  except Exception as e:
48
  return f"Error during video inference: {e}"
49
 
 
50
  def inference(model_id, input_media, media_type):
51
- # Dynamically load the model using the provided identifier
52
  model = load_model(model_id)
53
- if isinstance(model, str):
54
- # This means an error message was returned
55
  return model
56
 
57
- # Process based on media type
58
  if media_type == "Image":
59
  return predict_image(model, input_media)
60
  elif media_type == "Video":
@@ -62,24 +60,20 @@ def inference(model_id, input_media, media_type):
62
  else:
63
  return "Unsupported media type."
64
 
65
- # Create Gradio inputs:
66
- # - A text input for the model identifier
67
- # - A file input for images or videos
68
- # - A radio selection for choosing the media type
69
  model_text_input = gr.Textbox(label="Model Identifier", placeholder="e.g., yolov8n.pt, yolov8-seg.pt, yolov8-obb.pt")
70
- file_input = gr.inputs.File(label="Upload Image/Video File")
71
  media_type_dropdown = gr.Radio(choices=["Image", "Video"], label="Select Media Type", value="Image")
 
72
 
73
- # Decide on output type: for images, use an image output; for video, use a file output.
74
- # For a combined interface, we use File output because it can handle both cases with minimal changes.
75
  iface = gr.Interface(
76
  fn=inference,
77
  inputs=[model_text_input, file_input, media_type_dropdown],
78
- outputs=gr.outputs.File(label="Processed Output"),
79
  title="Dynamic Ultralytics YOLO Inference",
80
  description=(
81
- "Enter the model identifier (for detection, segmentation, or OBB models) and upload an image or video "
82
- "to run inference. The system dynamically loads the specified model and processes your media input."
83
  )
84
  )
85
 
 
3
  import cv2
4
  import tempfile
5
 
6
+ # Function to load the model dynamically based on user input.
7
  def load_model(model_id: str):
8
  try:
9
  model = YOLO(model_id)
 
11
  except Exception as e:
12
  return f"Error loading model: {e}"
13
 
14
+ # Inference for images: runs the model and plots predictions.
15
  def predict_image(model, image):
16
  try:
 
17
  results = model(image)
18
+ annotated_frame = results[0].plot() # This handles detection, segmentation, or OBB models.
 
19
  return annotated_frame
20
  except Exception as e:
21
  return f"Error during image inference: {e}"
22
 
23
+ # Inference for videos: processes the video frame by frame.
24
  def predict_video(model, video_file):
25
  try:
26
  cap = cv2.VideoCapture(video_file.name)
 
47
  except Exception as e:
48
  return f"Error during video inference: {e}"
49
 
50
+ # Unified inference function based on media type.
51
  def inference(model_id, input_media, media_type):
 
52
  model = load_model(model_id)
53
+ if isinstance(model, str): # Indicates an error message.
 
54
  return model
55
 
 
56
  if media_type == "Image":
57
  return predict_image(model, input_media)
58
  elif media_type == "Video":
 
60
  else:
61
  return "Unsupported media type."
62
 
63
+ # Updated Gradio interface components using the new API.
 
 
 
64
  model_text_input = gr.Textbox(label="Model Identifier", placeholder="e.g., yolov8n.pt, yolov8-seg.pt, yolov8-obb.pt")
65
+ file_input = gr.File(label="Upload Image/Video File")
66
  media_type_dropdown = gr.Radio(choices=["Image", "Video"], label="Select Media Type", value="Image")
67
+ output_file = gr.File(label="Processed Output")
68
 
69
+ # Create Gradio interface.
 
70
  iface = gr.Interface(
71
  fn=inference,
72
  inputs=[model_text_input, file_input, media_type_dropdown],
73
+ outputs=output_file,
74
  title="Dynamic Ultralytics YOLO Inference",
75
  description=(
76
+ "Enter the model identifier (supports detection, segmentation, or OBB models) and upload an image or video."
 
77
  )
78
  )
79