Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pothole_detection._and_instance_segmentation_yolov8 #14

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pothole_detection._and_instance_segmentation_yolov8
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from ultralytics import YOLO
import cv2
import numpy as np

# Load the model
model = YOLO("best.pt")
class_names = model.names
cap = cv2.VideoCapture('p.mp4')
count = 0

while True:
ret, img = cap.read()
if not ret:
break # Break if no frames are left in the video
count += 1
if count % 3 != 0: # Skip frames to process every 3rd frame
continue

img = cv2.resize(img, (1020, 500))
h, w, _ = img.shape
results = model.predict(img)

for r in results:
boxes = r.boxes # Bounding boxes
masks = r.masks # Segmentation masks if available

if masks is not None:
masks = masks.data.cpu()
for seg, box in zip(masks.numpy(), boxes):
seg = cv2.resize(seg, (w, h))
contours, _ = cv2.findContours((seg).astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
d = int(box.cls)
c = class_names[d]
x, y, x1, y1 = cv2.boundingRect(contour)
cv2.polylines(img, [contour], True, color=(0, 0, 255), thickness=2)
cv2.rectangle(img, (x, y), (x1 + x, y1 + y), (255, 0, 0), 2)
cv2.putText(img, c, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

cv2.imshow('Video', img)
if cv2.waitKey(1) & 0xFF == ord('q'): # Play video with a delay of 1ms
break

cap.release()
cv2.destroyAllWindows()