Yolo-Models
Collection
1 item
β’
Updated
This is a fine-tuned YOLOv8 model specifically trained for pothole detection in road images. The model has been trained on thousands of pothole images and achieves over 95% accuracy in detecting potholes across various road conditions and environments.
pip install ultralytics opencv-python pillow
from ultralytics import YOLO
import cv2
from PIL import Image
import numpy as np
# Load the model
model = YOLO("cazzz307/Pothole-Finetuned-YoloV8")
# Single image inference
def detect_potholes_image(image_path, output_path=None):
"""
Detect potholes in a single image
Args:
image_path (str): Path to input image
output_path (str): Path to save annotated image (optional)
Returns:
results: Detection results
"""
results = model(image_path)
# Print detection results
for result in results:
boxes = result.boxes
if boxes is not None:
print(f"Found {len(boxes)} potholes")
for box in boxes:
confidence = box.conf[0].item()
print(f"Pothole detected with confidence: {confidence:.2f}")
# Save annotated image if output path provided
if output_path:
annotated_frame = results[0].plot()
cv2.imwrite(output_path, annotated_frame)
print(f"Annotated image saved to: {output_path}")
return results
# Video inference
def detect_potholes_video(video_path, output_path=None):
"""
Detect potholes in a video
Args:
video_path (str): Path to input video
output_path (str): Path to save annotated video (optional)
"""
cap = cv2.VideoCapture(video_path)
# Get video properties for output
if output_path:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
frame_count = 0
total_detections = 0
while cap.isOpened():
success, frame = cap.read()
if not success:
break
# Run inference
results = model(frame)
# Count detections
for result in results:
if result.boxes is not None:
total_detections += len(result.boxes)
# Annotate frame
annotated_frame = results[0].plot()
# Save frame if output path provided
if output_path:
out.write(annotated_frame)
# Display frame (optional)
cv2.imshow("Pothole Detection", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
frame_count += 1
cap.release()
if output_path:
out.release()
cv2.destroyAllWindows()
print(f"Processed {frame_count} frames")
print(f"Total potholes detected: {total_detections}")
# Batch processing for multiple images
def detect_potholes_batch(image_folder, output_folder=None):
"""
Process multiple images in a folder
Args:
image_folder (str): Path to folder containing images
output_folder (str): Path to save annotated images (optional)
"""
import os
import glob
image_extensions = ['*.jpg', '*.jpeg', '*.png', '*.bmp', '*.tiff']
image_files = []
for ext in image_extensions:
image_files.extend(glob.glob(os.path.join(image_folder, ext)))
image_files.extend(glob.glob(os.path.join(image_folder, ext.upper())))
total_detections = 0
processed_images = 0
for image_path in image_files:
try:
results = model(image_path)
# Count detections
for result in results:
if result.boxes is not None:
total_detections += len(result.boxes)
print(f"{os.path.basename(image_path)}: {len(result.boxes)} potholes detected")
# Save annotated image if output folder provided
if output_folder:
os.makedirs(output_folder, exist_ok=True)
annotated_frame = results[0].plot()
output_path = os.path.join(output_folder, f"annotated_{os.path.basename(image_path)}")
cv2.imwrite(output_path, annotated_frame)
processed_images += 1
except Exception as e:
print(f"Error processing {image_path}: {str(e)}")
print(f"\nBatch processing complete:")
print(f"Processed images: {processed_images}")
print(f"Total potholes detected: {total_detections}")
# Example usage
if __name__ == "__main__":
# Single image
results = detect_potholes_image("road_image.jpg", "annotated_road.jpg")
# Video processing
# detect_potholes_video("road_video.mp4", "annotated_road_video.mp4")
# Batch processing
# detect_potholes_batch("road_images/", "annotated_images/")
from ultralytics import YOLO
# Load model
model = YOLO("cazzz307/Pothole-Finetuned-YoloV8")
# Advanced inference with custom parameters
def advanced_pothole_detection(image_path, confidence_threshold=0.5, iou_threshold=0.45):
"""
Advanced pothole detection with custom parameters
Args:
image_path (str): Path to input image
confidence_threshold (float): Confidence threshold for detections
iou_threshold (float): IoU threshold for NMS
Returns:
dict: Detection results with detailed information
"""
results = model(
image_path,
conf=confidence_threshold,
iou=iou_threshold,
verbose=False
)
detection_info = {
'total_potholes': 0,
'high_confidence_potholes': 0,
'detections': []
}
for result in results:
if result.boxes is not None:
detection_info['total_potholes'] = len(result.boxes)
for box in result.boxes:
confidence = box.conf[0].item()
bbox = box.xyxy[0].tolist() # [x1, y1, x2, y2]
detection_info['detections'].append({
'confidence': confidence,
'bbox': bbox,
'area': (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
})
if confidence > 0.8:
detection_info['high_confidence_potholes'] += 1
return detection_info
# Example usage
image_path = "test_road.jpg"
results = advanced_pothole_detection(image_path, confidence_threshold=0.6)
print(f"Total potholes: {results['total_potholes']}")
print(f"High confidence potholes: {results['high_confidence_potholes']}")
This model is released under the Apache 2.0 License.