Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,58 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
```python
|
6 |
+
# Example Code: Try on google colab
|
7 |
+
|
8 |
+
# Install required libraries
|
9 |
+
!pip install ultralytics --quiet
|
10 |
+
!pip install huggingface_hub --quiet
|
11 |
+
import cv2
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
from ultralytics import YOLO
|
14 |
+
from huggingface_hub import hf_hub_download
|
15 |
+
from google.colab import files
|
16 |
+
import os
|
17 |
+
|
18 |
+
# Download the YOLO model from Hugging Face
|
19 |
+
model_path = hf_hub_download(repo_id="krishnamishra8848/Road_Detection", filename="best.pt")
|
20 |
+
|
21 |
+
# Load the YOLO model
|
22 |
+
model = YOLO(model_path)
|
23 |
+
|
24 |
+
# Upload a photo
|
25 |
+
print("Please upload an image:")
|
26 |
+
uploaded = files.upload()
|
27 |
+
|
28 |
+
for filename in uploaded.keys():
|
29 |
+
# Read the uploaded image
|
30 |
+
image = cv2.imread(filename)
|
31 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
32 |
+
|
33 |
+
# Perform inference
|
34 |
+
results = model(image)
|
35 |
+
|
36 |
+
# Draw bounding boxes and class names
|
37 |
+
for result in results[0].boxes:
|
38 |
+
box = result.xyxy[0].cpu().numpy() # Bounding box (x_min, y_min, x_max, y_max)
|
39 |
+
cls = int(result.cls[0].cpu().numpy()) # Class ID
|
40 |
+
conf = result.conf[0].cpu().numpy() # Confidence score
|
41 |
+
label = f"{model.names[cls]}: {conf:.2f}" # Label with class name and confidence
|
42 |
+
|
43 |
+
# Draw the bounding box
|
44 |
+
cv2.rectangle(image_rgb, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
|
45 |
+
|
46 |
+
# Draw the class name and confidence score
|
47 |
+
cv2.putText(image_rgb, label, (int(box[0]), int(box[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
48 |
+
|
49 |
+
# Display the image with bounding boxes
|
50 |
+
plt.figure(figsize=(10, 10))
|
51 |
+
plt.imshow(image_rgb)
|
52 |
+
plt.axis('off')
|
53 |
+
plt.show()
|
54 |
+
|
55 |
+
# Save the processed image
|
56 |
+
output_filename = "output_" + filename
|
57 |
+
cv2.imwrite(output_filename, cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR))
|
58 |
+
print(f"Processed image saved as {output_filename}")
|