Untitled
unknown
plain_text
a year ago
2.0 kB
9
Indexable
import cv2 import numpy as np def detect_potholes(frame): # Convert the frame to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to reduce noise and improve edge detection blur = cv2.GaussianBlur(gray, (5, 5), 0) # Edge detection edges = cv2.Canny(blur, 50, 150) # Find contours in the edged image contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # Filter the contours pothole_contours = [] for cnt in contours: # Calculate contour area and remove small areas area = cv2.contourArea(cnt) if area > 500: # this threshold is an example, adjust it for your needs # Approximate the contour to a circle perimeter = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.02 * perimeter, True) # Contours with more than 5 vertices (more circular in shape) could be potential potholes if len(approx) > 5: pothole_contours.append(cnt) return pothole_contours # Load your video cap = cv2.VideoCapture('path_to_video.mp4') # Check if video opened successfully if not cap.isOpened(): print("Error opening video stream or file") # Read until video is completed while cap.isOpened(): ret, frame = cap.read() if ret: # Perform pothole detection on the frame potholes = detect_potholes(frame) # Draw bounding rectangles around detected potholes for cnt in potholes: x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Display the resulting frame with detected potholes cv2.imshow('Frame', frame) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break else: break # When everything done, release the video capture object cap.release() cv2.destroyAllWindows()
Editor is loading...
Leave a Comment