Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,69 +4,165 @@ import cv2
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
|
|
|
| 7 |
CLIENT = InferenceHTTPClient(
|
| 8 |
api_url="https://serverless.roboflow.com",
|
| 9 |
api_key="DIAhXQf6AUsyM1PRfdFa"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
def draw_detections(image, predictions):
|
|
|
|
| 13 |
img_array = np.array(image)
|
|
|
|
| 14 |
if 'predictions' in predictions:
|
| 15 |
for pred in predictions['predictions']:
|
| 16 |
-
x
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def detect_road_lanes(image):
|
|
|
|
| 26 |
if image is None:
|
| 27 |
-
return None, "Please upload an image."
|
|
|
|
| 28 |
try:
|
| 29 |
result = CLIENT.infer(image, model_id="road-lean/1")
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
-
return image, f"Error: {e}"
|
| 40 |
|
| 41 |
def detect_road_signs(image):
|
|
|
|
| 42 |
if image is None:
|
| 43 |
-
return None, "Please upload an image."
|
|
|
|
| 44 |
try:
|
| 45 |
result = CLIENT.infer(image, model_id="road-sign-peqgi/1")
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
-
return image, f"Error: {e}"
|
| 56 |
|
|
|
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
gr.Markdown("# π Road Detection System")
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
with gr.Row():
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
lane_text = gr.Textbox(lines=5)
|
| 64 |
-
gr.Button("Detect
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
with gr.Row():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
sign_text = gr.Textbox(lines=5)
|
| 70 |
-
gr.Button("Detect
|
|
|
|
| 71 |
|
| 72 |
demo.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Initialize Roboflow client
|
| 8 |
CLIENT = InferenceHTTPClient(
|
| 9 |
api_url="https://serverless.roboflow.com",
|
| 10 |
api_key="DIAhXQf6AUsyM1PRfdFa"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
def draw_detections(image, predictions, color=(0, 255, 0), prefix=""):
|
| 14 |
+
"""Draw bounding boxes and labels on the image"""
|
| 15 |
img_array = np.array(image)
|
| 16 |
+
|
| 17 |
if 'predictions' in predictions:
|
| 18 |
for pred in predictions['predictions']:
|
| 19 |
+
x = int(pred['x'])
|
| 20 |
+
y = int(pred['y'])
|
| 21 |
+
width = int(pred['width'])
|
| 22 |
+
height = int(pred['height'])
|
| 23 |
+
confidence = pred['confidence']
|
| 24 |
+
class_name = pred['class']
|
| 25 |
+
|
| 26 |
+
# Calculate bounding box coordinates
|
| 27 |
+
x1 = int(x - width / 2)
|
| 28 |
+
y1 = int(y - height / 2)
|
| 29 |
+
x2 = int(x + width / 2)
|
| 30 |
+
y2 = int(y + height / 2)
|
| 31 |
+
|
| 32 |
+
# Draw rectangle
|
| 33 |
+
cv2.rectangle(img_array, (x1, y1), (x2, y2), color, 2)
|
| 34 |
+
|
| 35 |
+
# Draw label with prefix
|
| 36 |
+
label = f"{prefix}{class_name}: {confidence:.2f}"
|
| 37 |
+
cv2.putText(img_array, label, (x1, y1 - 10),
|
| 38 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 39 |
+
|
| 40 |
+
return img_array
|
| 41 |
+
|
| 42 |
+
def detect_both(image):
|
| 43 |
+
"""Detect both road lanes and road signs in one image"""
|
| 44 |
+
if image is None:
|
| 45 |
+
return None, "Please upload an image first."
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Detect road lanes
|
| 49 |
+
lanes_result = CLIENT.infer(image, model_id="road-lean/1")
|
| 50 |
+
|
| 51 |
+
# Detect road signs
|
| 52 |
+
signs_result = CLIENT.infer(image, model_id="road-sign-peqgi/1")
|
| 53 |
+
|
| 54 |
+
# Draw both detections on the same image
|
| 55 |
+
img_array = np.array(image)
|
| 56 |
+
|
| 57 |
+
# Draw lanes in green
|
| 58 |
+
img_array = draw_detections(Image.fromarray(img_array), lanes_result,
|
| 59 |
+
color=(0, 255, 0), prefix="Lane: ")
|
| 60 |
+
|
| 61 |
+
# Draw signs in blue
|
| 62 |
+
img_array = draw_detections(Image.fromarray(img_array), signs_result,
|
| 63 |
+
color=(255, 0, 0), prefix="Sign: ")
|
| 64 |
+
|
| 65 |
+
output_image = Image.fromarray(img_array)
|
| 66 |
+
|
| 67 |
+
# Format results text
|
| 68 |
+
num_lanes = len(lanes_result.get('predictions', []))
|
| 69 |
+
num_signs = len(signs_result.get('predictions', []))
|
| 70 |
+
|
| 71 |
+
results_text = f"π£οΈ Road Lanes Detected: {num_lanes}\n"
|
| 72 |
+
if num_lanes > 0:
|
| 73 |
+
for i, pred in enumerate(lanes_result.get('predictions', []), 1):
|
| 74 |
+
results_text += f" {i}. {pred['class']}: {pred['confidence']:.2%}\n"
|
| 75 |
+
else:
|
| 76 |
+
results_text += " No lanes detected\n"
|
| 77 |
+
|
| 78 |
+
results_text += f"\nπ¦ Road Signs Detected: {num_signs}\n"
|
| 79 |
+
if num_signs > 0:
|
| 80 |
+
for i, pred in enumerate(signs_result.get('predictions', []), 1):
|
| 81 |
+
results_text += f" {i}. {pred['class']}: {pred['confidence']:.2%}\n"
|
| 82 |
+
else:
|
| 83 |
+
results_text += " No signs detected\n"
|
| 84 |
+
|
| 85 |
+
results_text += f"\nπ Total Detections: {num_lanes + num_signs}"
|
| 86 |
+
results_text += "\n\nπ’ Green boxes = Road Lanes\nπ΅ Blue boxes = Road Signs"
|
| 87 |
+
|
| 88 |
+
return output_image, results_text
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return image, f"Error: {str(e)}"
|
| 91 |
|
| 92 |
def detect_road_lanes(image):
|
| 93 |
+
"""Detect road lanes only"""
|
| 94 |
if image is None:
|
| 95 |
+
return None, "Please upload an image first."
|
| 96 |
+
|
| 97 |
try:
|
| 98 |
result = CLIENT.infer(image, model_id="road-lean/1")
|
| 99 |
+
img_array = draw_detections(image, result, color=(0, 255, 0))
|
| 100 |
+
output_image = Image.fromarray(img_array)
|
| 101 |
+
|
| 102 |
+
num_detections = len(result.get('predictions', []))
|
| 103 |
+
results_text = f"Detections: {num_detections}\n\n"
|
| 104 |
+
|
| 105 |
+
if num_detections == 0:
|
| 106 |
+
results_text += "No road lanes detected."
|
| 107 |
+
else:
|
| 108 |
+
for i, pred in enumerate(result.get('predictions', []), 1):
|
| 109 |
+
results_text += f"{i}. {pred['class']}: {pred['confidence']:.2%}\n"
|
| 110 |
+
|
| 111 |
+
return output_image, results_text
|
| 112 |
except Exception as e:
|
| 113 |
+
return image, f"Error: {str(e)}"
|
| 114 |
|
| 115 |
def detect_road_signs(image):
|
| 116 |
+
"""Detect road signs only"""
|
| 117 |
if image is None:
|
| 118 |
+
return None, "Please upload an image first."
|
| 119 |
+
|
| 120 |
try:
|
| 121 |
result = CLIENT.infer(image, model_id="road-sign-peqgi/1")
|
| 122 |
+
img_array = draw_detections(image, result, color=(255, 0, 0))
|
| 123 |
+
output_image = Image.fromarray(img_array)
|
| 124 |
+
|
| 125 |
+
num_detections = len(result.get('predictions', []))
|
| 126 |
+
results_text = f"Detections: {num_detections}\n\n"
|
| 127 |
+
|
| 128 |
+
if num_detections == 0:
|
| 129 |
+
results_text += "No road signs detected."
|
| 130 |
+
else:
|
| 131 |
+
for i, pred in enumerate(result.get('predictions', []), 1):
|
| 132 |
+
results_text += f"{i}. {pred['class']}: {pred['confidence']:.2%}\n"
|
| 133 |
+
|
| 134 |
+
return output_image, results_text
|
| 135 |
except Exception as e:
|
| 136 |
+
return image, f"Error: {str(e)}"
|
| 137 |
|
| 138 |
+
# Create interface
|
| 139 |
with gr.Blocks() as demo:
|
| 140 |
gr.Markdown("# π Road Detection System")
|
| 141 |
+
gr.Markdown("Upload an image to detect road lanes and road signs using AI.")
|
| 142 |
+
|
| 143 |
+
with gr.Tab("π― Detect Both"):
|
| 144 |
+
gr.Markdown("### Detect both road lanes and road signs in one image")
|
| 145 |
+
with gr.Row():
|
| 146 |
+
both_input = gr.Image(type="pil", label="Upload Image")
|
| 147 |
+
both_output = gr.Image(label="Detection Results")
|
| 148 |
+
both_text = gr.Textbox(label="Details", lines=10)
|
| 149 |
+
both_btn = gr.Button("π Detect All", variant="primary", size="lg")
|
| 150 |
+
both_btn.click(detect_both, inputs=both_input, outputs=[both_output, both_text])
|
| 151 |
+
|
| 152 |
+
with gr.Tab("π£οΈ Road Lanes Only"):
|
| 153 |
with gr.Row():
|
| 154 |
+
lane_input = gr.Image(type="pil", label="Upload Image")
|
| 155 |
+
lane_output = gr.Image(label="Detection Results")
|
| 156 |
+
lane_text = gr.Textbox(label="Details", lines=5)
|
| 157 |
+
lane_btn = gr.Button("Detect Road Lanes", variant="primary")
|
| 158 |
+
lane_btn.click(detect_road_lanes, inputs=lane_input, outputs=[lane_output, lane_text])
|
| 159 |
+
|
| 160 |
+
with gr.Tab("π¦ Road Signs Only"):
|
| 161 |
with gr.Row():
|
| 162 |
+
sign_input = gr.Image(type="pil", label="Upload Image")
|
| 163 |
+
sign_output = gr.Image(label="Detection Results")
|
| 164 |
+
sign_text = gr.Textbox(label="Details", lines=5)
|
| 165 |
+
sign_btn = gr.Button("Detect Road Signs", variant="primary")
|
| 166 |
+
sign_btn.click(detect_road_signs, inputs=sign_input, outputs=[sign_output, sign_text])
|
| 167 |
|
| 168 |
demo.launch()
|