!pip install ultralytics --quiet
!pip install huggingface_hub --quiet
import cv2
import matplotlib.pyplot as plt
from ultralytics import YOLO
from huggingface_hub import hf_hub_download
from google.colab import files
import os
model_path = hf_hub_download(repo_id="krishnamishra8848/Road_Detection", filename="best.pt")
model = YOLO(model_path)
print("Please upload an image:")
uploaded = files.upload()
for filename in uploaded.keys():
image = cv2.imread(filename)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = model(image)
for result in results[0].boxes:
box = result.xyxy[0].cpu().numpy()
cls = int(result.cls[0].cpu().numpy())
conf = result.conf[0].cpu().numpy()
label = f"{model.names[cls]}: {conf:.2f}"
cv2.rectangle(image_rgb, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
cv2.putText(image_rgb, label, (int(box[0]), int(box[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
plt.figure(figsize=(10, 10))
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
output_filename = "output_" + filename
cv2.imwrite(output_filename, cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR))
print(f"Processed image saved as {output_filename}")