-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (86 loc) · 3.72 KB
/
Copy pathapp.py
File metadata and controls
106 lines (86 loc) · 3.72 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
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import tempfile
# --- Streamlit Page Setup ---
st.set_page_config(page_title="Lane Detection System", layout="wide")
st.title("🚗 Autonomous Lane Detection & Assistance System")
st.write("Upload an image or a video file to test the Computer Vision layout live.")
# --- Core Computer Vision Pipeline Functions ---
def grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def gaussian_blur(image, kernel_size=5):
return cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
def canny(image, low_threshold=50, high_threshold=150):
return cv2.Canny(image, low_threshold, high_threshold)
def region_of_interest(image):
height, width = image.shape[:2]
mask = np.zeros_like(image)
polygon = np.array([[
(0, height),
(width, height),
(width, height // 2),
(0, height // 2),
]], np.int32)
cv2.fillPoly(mask, polygon, 255)
return cv2.bitwise_and(image, mask)
def draw_lines(image, lines):
line_image = np.zeros_like(image)
if lines is not None:
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
return line_image
def hough_lines(image):
return cv2.HoughLinesP(image, rho=2, theta=np.pi/180, threshold=100, minLineLength=40, maxLineGap=5)
def lane_detection_pipeline(image):
gray = grayscale(image)
blur = gaussian_blur(gray)
edges = canny(blur)
roi = region_of_interest(edges)
lines = hough_lines(roi)
line_image = draw_lines(image, lines)
return cv2.addWeighted(image, 0.8, line_image, 1, 0)
# --- Streamlit Integrated Wrapper Function ---
def process_frame(frame):
"""
Input: frame (a standard OpenCV BGR numpy array)
Output: processed_frame (the frame with lanes drawn on it)
"""
# Simply pipe the incoming image frame through your detection engine
return lane_detection_pipeline(frame)
# --- Interactive Web UI Layout ---
option = st.selectbox("Select Media Type", ("Image", "Video"))
if option == "Image":
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Convert file upload to openCV format safely
image = Image.open(uploaded_file)
frame = np.array(image)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
st.write("Processing image layout...")
result = process_frame(frame)
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Original Input", use_container_width=True)
with col2:
st.image(cv2.cvtColor(result, cv2.COLOR_BGR2RGB), caption="Detected Lanes Output", use_container_width=True)
elif option == "Video":
uploaded_file = st.file_uploader("Upload a driving video segment...", type=["mp4", "mov", "avi"])
if uploaded_file is not None:
# Save upload to a temp file so OpenCV can buffer it frame-by-frame
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(uploaded_file.read())
cap = cv2.VideoCapture(tfile.name)
stframe = st.empty() # Placeholder node to stream frames into dynamically
st.write("Processing video stream elements...")
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process individual frame using your algorithm
processed = process_frame(frame)
# Render frame on screen sequentially (converting BGR to RGB for web display)
stframe.image(cv2.cvtColor(processed, cv2.COLOR_BGR2RGB), use_container_width=True)
cap.release()