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