Spaces:
Runtime error
Runtime error
Add application file
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546#Falah with Gradioimport gradio as grfrom transformers import pipelinefrom PIL import Image, ImageDraw
checkpoint = "google/owlvit-base-patch32"detector = pipeline(model=checkpoint, task="zero-shot-object-detection")
def detect_and_visualize_objects(image): # Convert the image to RGB format image = image.convert("RGB")
# Process the image using the object detection model predictions = detector( image, candidate_labels=["human face", "rocket", "nasa badge", "star-spangled banner"], )
# Draw bounding boxes and labels on the image draw = ImageDraw.Draw(image) for prediction in predictions: box = prediction["box"] label = prediction["label"] score = prediction["score"]
xmin, ymin, xmax, ymax = box.values() draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1) draw.text((xmin, ymin), f"{label}: {round(score, 2)}", fill="white")
# Return the annotated image return image
# Define the Gradio interfaceimage_input = gr.inputs.Image(type="pil")image_output = gr.outputs.Image(type="pil")
iface = gr.Interface( fn=detect_and_visualize_objects, inputs=image_input, outputs=image_output, title="Object Detection", description="Detect objects in an image using a pre-trained model and visualize the results.",)
# Launch the Gradio interfaceiface.launch(debug=True)app.pyAdded+46