Spaces:
Sleeping
Sleeping
File size: 1,800 Bytes
37010f4 45a33d5 37010f4 45a33d5 37010f4 6db1ec2 37010f4 45a33d5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import gradio as gr
import requests
import cv2
import numpy as np
import os
API_URL = "https://detect.roboflow.com"
API_KEY = os.getenv("API_KEY")
MODEL_ID = "biofarma-x-mit-hacking-medicine-hackathon/1"
def annotate_image(image):
# Convert the input image to a format suitable for OpenCV
image = np.array(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Save the input image
cv2.imwrite("input_image.jpg", image)
# Prepare the request
url = f"{API_URL}/{MODEL_ID}?api_key={API_KEY}"
with open("input_image.jpg", "rb") as file:
response = requests.post(url, files={"file": file})
result = response.json()
# Annotate the image
for prediction in result['predictions']:
x, y, width, height = prediction['x'], prediction['y'], prediction['width'], prediction['height']
left = int(x - width / 2)
top = int(y - height / 2)
right = int(x + width / 2)
bottom = int(y + height / 2)
cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(image, f"{prediction['confidence']:.2f}", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Get the number of detections
detection_count = len(result['predictions'])
# Convert the image back to RGB for display in Gradio
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image, f"Number of detections: {detection_count}"
# Create the Gradio interface
iface = gr.Interface(
fn=annotate_image,
inputs=gr.Image(type="pil"),
outputs=[gr.Image(label='Hasil'), gr.Text(label='Total bakteri terdeteksi')],
title="TB-Bacillus Detection",
description="Upload an image to get annotated results from the model."
)
# Launch the app
iface.launch() |