Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
HarshaVardhan KV1RN21AI067 Lakshmish YSwedith ReddyHarsha Vardhan
Created July 26, 2024

Road detection and driving

he road detection code detects roads in images using image preprocessing and machine learning. It converts images to grayscale, applies Gaus

9
Road detection and driving

Things used in this project

Hardware components

Minisforum Venus UM790 Pro with AMD Ryzen™ 9
Minisforum Venus UM790 Pro with AMD Ryzen™ 9
×1

Software apps and online services

AMD ROCm™ Software
AMD ROCm™ Software

Story

Read more

Code

main

Python
load the model and drun
from SinglePic import run_inference
import cv2
import numpy as np


def decodeVid(vidFile):
    video_capture = cv2.VideoCapture(vidFile)
    steering_angle2 = 40  # Initial steering angle for the second line
    steering_angle1 = 135  # Initial steering angle for the first line
    newVd = True
    height, width = 0,0
    center_x1, center_y1 = 0, 0
    center_x2, center_y2 = 0, 0
    x1, y1 = 0, 0
    x2, y2 = 0, 0
    pt = True

    while video_capture.isOpened():
        ret, frame = video_capture.read()
        if not ret:
            break
    
        processed_frame,driveAbleArea,steerin = run_inference(frame)
        
        
        # Adding lines 
        if newVd:
            height, width = processed_frame.shape[:2]
            center_x2 = 400
            center_y2 = 220
            center_x1 = 300
            center_y1 = 220
            newVd = False

        # Define the length of the lines
        line_length = min(height, width) // 2

        # Create a copy of the frame to draw lines on
        frame_with_lines = processed_frame.copy()

        # Calculate the endpoints of the lines based on the steering angles and positions
        angle_radians2 = np.deg2rad(steering_angle2)
        x2 = int(center_x2 + line_length * np.cos(angle_radians2))
        y2 = int(center_y2 + line_length * np.sin(angle_radians2))
        
        angle_radians1 = np.deg2rad(steering_angle1)
        x1 = int(center_x1 + line_length * np.cos(angle_radians1))
        y1 = int(center_y1 + line_length * np.sin(angle_radians1))

        cv2.line(frame_with_lines, (center_x2, center_y2), (x2, y2), (255,0,0), thickness=2)
        cv2.line(frame_with_lines, (center_x1, center_y1), (x1, y1), (255,0,0), thickness=2)
        
        rgb_image = cv2.cvtColor(frame_with_lines,cv2.COLOR_BGR2RGB)

        steering_angle2 += steerin[0]  
        steering_angle1 += steerin[1]

        '''
        for finding cordinates
        if pt:
            
            for i, cor in enumerate(line1_px):
                if i%150 == 0:
                    print(cor)
            pt = False
        '''

        # Display the processed frame with lines 
        cv2.imshow('Processed Frame', rgb_image)

        # Adjust dynamically
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            break
        elif key == ord('p'):
            print(f"x1 = {x1}\ny1 = {y1}\nx2 = {x2}\ny2 = {y2}")

    # Release the video capture                                                        
    video_capture.release()

    # Close all OpenCV windows
    cv2.destroyAllWindows()

def main():
    decodeVid('dataset\gtaTest.mp4')

if __name__ == "__main__":
    main()

Credits

HarshaVardhan KV
1 project • 0 followers
1RN21AI067 Lakshmish Y
0 projects • 0 followers
Swedith Reddy
0 projects • 0 followers
Harsha Vardhan
0 projects • 0 followers

Comments