-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_laneDetection.py
More file actions
executable file
·135 lines (107 loc) · 5.58 KB
/
local_laneDetection.py
File metadata and controls
executable file
·135 lines (107 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import cv2
import numpy as np
import socket
import time
import requests
# Initialize adaptive contour area tracking
contour_areas_history = []
MAX_HISTORY_SIZE = 10
MIN_CONTOUR_AREA_FACTOR = 0.5 # Start with 50% of the average
# Initialize FPS variables
prev_frame_time = 0
new_frame_time = 0
def calculate_steering_angle(frame, largest_contour):
height, width = frame.shape[:2]
M = cv2.moments(largest_contour)
cx = int(M["m10"] / M["m00"]) if M["m00"] != 0 else width // 2
center_offset = cx - (width // 2)
steering_angle = -float(center_offset) * 0.1
return steering_angle, cx
def apply_steering_control(steering_angle):
if abs(steering_angle) < 14:
return "Go Straight"
elif steering_angle > 0:
return "Turn Left"
else:
return "Turn Right"
def get_adaptive_min_contour_area():
"""Calculate the adaptive minimum contour area based on history"""
if not contour_areas_history:
return 10000 # Default value if no history yet
avg_area = sum(contour_areas_history) / len(contour_areas_history)
return avg_area * MIN_CONTOUR_AREA_FACTOR
def main():
prev_frame_time = time.time()
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
height, width = frame.shape[:2]
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, binary_frame = cv2.threshold(gray_frame, 100, 255, cv2.THRESH_BINARY_INV)
roi_vertices = np.array([[(0, height // 2), (width, height // 2), (width, height), (0, height)]], dtype=np.int32)
mask = np.zeros_like(binary_frame)
cv2.fillPoly(mask, roi_vertices, 255)
roi_result = cv2.bitwise_and(binary_frame, mask)
contours, _ = cv2.findContours(roi_result, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
overlay = np.zeros_like(frame)
# Initialize variables for when no lanes are detected
lanes_detected = False
highlighted_frame = frame.copy()
# Update contour area history if we have contours
if contours:
# Get the largest contour area for history tracking
largest_area = max([cv2.contourArea(cnt) for cnt in contours])
contour_areas_history.append(largest_area)
# Keep history at a reasonable size
if len(contour_areas_history) > MAX_HISTORY_SIZE:
contour_areas_history.pop(0)
# Get the adaptive minimum contour area threshold
MIN_CONTOUR_AREA = get_adaptive_min_contour_area()
# Check if contours exist and the largest one is above our threshold
valid_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > MIN_CONTOUR_AREA]
if valid_contours:
lanes_detected = True
largest_contour = max(valid_contours, key=cv2.contourArea)
cv2.fillPoly(overlay, [largest_contour], (0, 255, 0))
highlighted_frame = cv2.addWeighted(frame, 1, overlay, 0.3, 0)
cv2.drawContours(highlighted_frame, [largest_contour], -1, (0, 255, 255), 2)
steering_angle, center_x = calculate_steering_angle(frame, largest_contour)
steering_direction = apply_steering_control(steering_angle)
# Draw guiding bullet point
cv2.circle(highlighted_frame, (center_x, height - 50), 5, (0, 0, 255), -1)
cv2.line(highlighted_frame, (width // 2, height - 50), (center_x, height - 50), (0, 0, 255), 2)
# Draw steering direction above the bullet point
text_size = cv2.getTextSize(steering_direction, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
text_x = center_x - text_size[0] // 2
text_y = height - 80
cv2.putText(highlighted_frame, steering_direction, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
else:
# No lanes detected or all contours are below threshold
message = "No Lanes Detected"
text_size = cv2.getTextSize(message, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
text_x = width // 2 - text_size[0] // 2
text_y = 2 * (height // 3)
cv2.putText(highlighted_frame, message, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Draw ROI outline
cv2.polylines(highlighted_frame, [roi_vertices], True, (255, 0, 0), 2)
# Display the current adaptive threshold
cv2.putText(highlighted_frame, f'Min Area: {int(MIN_CONTOUR_AREA)}', (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Calculate and display Frame Rates
new_frame_time = time.time()
fps = 0 if (new_frame_time-prev_frame_time == 0) else (1/(new_frame_time-prev_frame_time))
prev_frame_time = new_frame_time
fps = int(fps) if fps > 0 else 0
cv2.putText(highlighted_frame, f'FPS: {fps}', (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
cv2.putText(binary_frame, f'FPS: {fps}', (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
# Display the frames
cv2.imshow('Binary Frame', cv2.resize(binary_frame, (720, 540)))
cv2.imshow('Lane Detection with Steering', cv2.resize(highlighted_frame, (720, 540)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()