Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,36 +55,32 @@ for model in models:
|
|
| 55 |
model["cfg"].MODEL.DEVICE = "cpu"
|
| 56 |
|
| 57 |
|
| 58 |
-
def inference(image, min_score, model_name):
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
model_id = model_name_to_id[model_name]
|
|
|
|
| 62 |
models[model_id]["cfg"].MODEL.ROI_HEADS.SCORE_THRESH_TEST = min_score
|
| 63 |
predictor = DefaultPredictor(models[model_id]["cfg"])
|
| 64 |
|
| 65 |
-
|
| 66 |
-
if isinstance(image, list): # Si es una lista de imágenes
|
| 67 |
-
images = image
|
| 68 |
-
else: # Si es solo una imagen, la convertimos en una lista
|
| 69 |
-
images = [image]
|
| 70 |
-
|
| 71 |
-
for img in images:
|
| 72 |
-
# Convert image from BGR to RGB if necessary
|
| 73 |
-
im = img[:,:,::-1]
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
| 80 |
|
| 81 |
-
# Convert the result from BGR to RGB
|
| 82 |
-
result_image = out.get_image()
|
| 83 |
-
result_image_rgb = result_image[:, :, ::-1] # Convert BGR to RGB
|
| 84 |
-
|
| 85 |
-
results.append(result_image_rgb) # Add the processed image to the list
|
| 86 |
-
|
| 87 |
-
return results # Return all the results
|
| 88 |
|
| 89 |
title = "# DBMDZ Detectron2 Model Demo"
|
| 90 |
description = """
|
|
@@ -99,18 +95,21 @@ with gr.Blocks() as demo:
|
|
| 99 |
gr.Markdown(title)
|
| 100 |
gr.Markdown(description)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Tab("From Image"):
|
| 103 |
-
image_input = gr.
|
| 104 |
|
| 105 |
min_score = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Minimum score")
|
| 106 |
|
| 107 |
model_name = gr.Radio(choices=[model["name"] for model in models], value=models[0]["name"], label="Select Detectron2 model")
|
| 108 |
|
| 109 |
-
|
| 110 |
|
| 111 |
inference_button = gr.Button("Submit")
|
| 112 |
|
| 113 |
-
inference_button.click(fn=inference, inputs=[image_input, min_score, model_name], outputs=
|
| 114 |
|
| 115 |
gr.Markdown(footer)
|
| 116 |
|
|
|
|
| 55 |
model["cfg"].MODEL.DEVICE = "cpu"
|
| 56 |
|
| 57 |
|
| 58 |
+
def inference(image_url, image, min_score, model_name):
|
| 59 |
+
if image_url:
|
| 60 |
+
r = requests.get(image_url)
|
| 61 |
+
if r:
|
| 62 |
+
im = np.frombuffer(r.content, dtype="uint8")
|
| 63 |
+
im = cv2.imdecode(im, cv2.IMREAD_COLOR_BGR2RGB)
|
| 64 |
+
else:
|
| 65 |
+
# Model expect BGR!
|
| 66 |
+
im = image[:,:,::-1]
|
| 67 |
+
|
| 68 |
model_id = model_name_to_id[model_name]
|
| 69 |
+
|
| 70 |
models[model_id]["cfg"].MODEL.ROI_HEADS.SCORE_THRESH_TEST = min_score
|
| 71 |
predictor = DefaultPredictor(models[model_id]["cfg"])
|
| 72 |
|
| 73 |
+
outputs = predictor(im)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
v = Visualizer(im, models[model_id]["metadata"], scale=1.2)
|
| 76 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
| 77 |
+
|
| 78 |
+
# Convertir la imagen de salida de BGR a RGB
|
| 79 |
+
result_image = out.get_image() # Esto sigue estando en BGR
|
| 80 |
+
result_image_rgb = result_image[:, :, ::-1] # Convertir BGR a RGB
|
| 81 |
|
| 82 |
+
return result_image_rgb
|
|
|
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
title = "# DBMDZ Detectron2 Model Demo"
|
| 86 |
description = """
|
|
|
|
| 95 |
gr.Markdown(title)
|
| 96 |
gr.Markdown(description)
|
| 97 |
|
| 98 |
+
with gr.Tab("From URL"):
|
| 99 |
+
url_input = gr.Textbox(label="Image URL", placeholder="https://api.digitale-sammlungen.de/iiif/image/v2/bsb10483966_00008/full/500,/0/default.jpg")
|
| 100 |
+
|
| 101 |
with gr.Tab("From Image"):
|
| 102 |
+
image_input = gr.Image(type="numpy", label="Input Image")
|
| 103 |
|
| 104 |
min_score = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Minimum score")
|
| 105 |
|
| 106 |
model_name = gr.Radio(choices=[model["name"] for model in models], value=models[0]["name"], label="Select Detectron2 model")
|
| 107 |
|
| 108 |
+
output_image = gr.Image(type="pil", label="Output")
|
| 109 |
|
| 110 |
inference_button = gr.Button("Submit")
|
| 111 |
|
| 112 |
+
inference_button.click(fn=inference, inputs=[url_input, image_input, min_score, model_name], outputs=output_image)
|
| 113 |
|
| 114 |
gr.Markdown(footer)
|
| 115 |
|