-
Notifications
You must be signed in to change notification settings - Fork 188
修改了模块名,具体描述了索要实现的具体功能 #2934
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
Open
shengzhanpeng
wants to merge
24
commits into
OpenHUTB:main
Choose a base branch
from
shengzhanpeng:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+961
−1
Open
修改了模块名,具体描述了索要实现的具体功能 #2934
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f12b24b
完成选题并提交
shengzhanpeng df4a38b
Merge branch 'OpenHUTB:main' into main
shengzhanpeng daed23e
Merge branch 'OpenHUTB:main' into main
shengzhanpeng ed021d8
完成选题并提交
shengzhanpeng 5f91799
Merge remote-tracking branch 'origin/main'
shengzhanpeng ae09b6e
提交
shengzhanpeng 97cb689
进行首次提交,选题为无人车
shengzhanpeng d06efb1
提交车辆代理
shengzhanpeng e5193de
提交车辆代理
shengzhanpeng 59e103d
提交车辆代理
shengzhanpeng cc6a03a
提交车辆代理
shengzhanpeng f995e68
Merge remote-tracking branch 'origin/main'
shengzhanpeng 24ec975
Merge remote-tracking branch 'origin/main'
shengzhanpeng 83398e0
Merge remote-tracking branch 'origin/main'
shengzhanpeng 1f7e013
Merge branch 'OpenHUTB:main' into main
shengzhanpeng 455e063
Merge remote-tracking branch 'origin/main'
shengzhanpeng a48fc57
Merge remote-tracking branch 'origin/main'
shengzhanpeng cd44b41
Merge remote-tracking branch 'origin/main'
shengzhanpeng 9ae3dfe
Merge remote-tracking branch 'origin/main'
shengzhanpeng 105d32d
Merge remote-tracking branch 'origin/main'
shengzhanpeng 8a2f2f9
Update and rename Traffic_Light_Detection.py to Traffic_light_detecti…
shengzhanpeng 03c0107
Update Traffic_light_detection.py
shengzhanpeng 3d54cb9
Update README.md
shengzhanpeng efa0575
Update and rename src/Driving_Car/instance segmentation camera.py to …
shengzhanpeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import cv2 | ||
| import numpy as np | ||
| from PIL import Image, ImageDraw | ||
| import random | ||
|
|
||
| # -------------------------- 1. 模拟无人车行驶场景(生成实时帧) -------------------------- | ||
| def generate_driving_frame(): | ||
| """生成模拟无人车前方视角的帧(道路+随机障碍物)""" | ||
| img_width, img_height = 1280, 720 # 适配识别分辨率 | ||
| frame = Image.new("RGB", (img_width, img_height), (100, 100, 100)) # 灰色天空背景 | ||
| draw = ImageDraw.Draw(frame) | ||
|
|
||
| # 绘制道路(中间黑色路面,两侧白色标线) | ||
| road_width = 800 | ||
| road_left = (img_width - road_width) // 2 | ||
| road_right = road_left + road_width | ||
| # 黑色路面 | ||
| draw.rectangle([road_left, 0, road_right, img_height], fill=(50, 50, 50)) | ||
| # 白色边线 | ||
| draw.line([(road_left, 0), (road_left, img_height)], fill=(255, 255, 255), width=10) | ||
| draw.line([(road_right, 0), (road_right, img_height)], fill=(255, 255, 255), width=10) | ||
| # 中间虚线 | ||
| for y in range(0, img_height, 60): | ||
| draw.rectangle([(img_width//2 - 10, y), (img_width//2 + 10, y + 30)], fill=(255, 255, 255)) | ||
|
|
||
| # 随机生成障碍物(车辆/行人,位置在道路中间) | ||
| obstacle_type = random.choice(["car", "pedestrian", "car", "none"]) # 大概率生成车辆,偶尔行人/无障碍物 | ||
| obstacle_pos_y = random.randint(int(img_height * 0.4), int(img_height * 0.8)) # 前方不同距离 | ||
| obstacle_size = random.randint(80, 200) # 大小=距离(越大越近) | ||
|
|
||
| if obstacle_type == "car": | ||
| # 绘制模拟车辆(矩形+车轮) | ||
| car_x = random.randint(road_left + 50, road_right - 50 - obstacle_size) | ||
| # 车身 | ||
| draw.rectangle([(car_x, obstacle_pos_y), (car_x + obstacle_size, obstacle_pos_y + obstacle_size//2)], fill=(255, 0, 0)) | ||
| # 车轮 | ||
| wheel_size = obstacle_size // 6 | ||
| draw.ellipse([(car_x + wheel_size, obstacle_pos_y + obstacle_size//2 - wheel_size), | ||
| (car_x + 2*wheel_size, obstacle_pos_y + obstacle_size//2)], fill=(0,0,0)) | ||
| draw.ellipse([(car_x + obstacle_size - 2*wheel_size, obstacle_pos_y + obstacle_size//2 - wheel_size), | ||
| (car_x + obstacle_size - wheel_size, obstacle_pos_y + obstacle_size//2)], fill=(0,0,0)) | ||
| elif obstacle_type == "pedestrian": | ||
| # 绘制模拟行人(圆形+矩形) | ||
| ped_x = random.randint(road_left + 50, road_right - 50 - obstacle_size//2) | ||
| # 身体 | ||
| draw.rectangle([(ped_x + obstacle_size//4, obstacle_pos_y), (ped_x + 3*obstacle_size//4, obstacle_pos_y + obstacle_size)], fill=(0,0,255)) | ||
| # 头部 | ||
| draw.ellipse([(ped_x, obstacle_pos_y - obstacle_size//4), (ped_x + obstacle_size//2, obstacle_pos_y)], fill=(255, 255, 0)) | ||
|
|
||
| # 转换为OpenCV格式(BGR) | ||
| return cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR), obstacle_type # 返回帧+真实障碍物类型(用于验证) | ||
|
|
||
| # -------------------------- 2. 障碍物识别核心逻辑(与Carla版本一致) -------------------------- | ||
| def detect_obstacles(frame): | ||
| # 预处理:灰度化+高斯模糊 | ||
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | ||
| blur = cv2.GaussianBlur(gray, (5, 5), 0) | ||
|
|
||
| # Canny边缘检测 | ||
| edges = cv2.Canny(blur, 50, 150) | ||
|
|
||
| # 形态学处理(连接断裂边缘) | ||
| kernel = np.ones((7, 7), np.uint8) | ||
| dilated = cv2.dilate(edges, kernel, iterations=1) | ||
| eroded = cv2.erode(dilated, kernel, iterations=1) | ||
|
|
||
| # 轮廓检测 | ||
| contours, _ = cv2.findContours(eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | ||
|
|
||
| has_obstacle = False | ||
| danger_distance = False | ||
| obstacle_area = 0 | ||
|
|
||
| # 感兴趣区域(前方路面) | ||
| frame_height, frame_width = frame.shape[:2] | ||
| roi_top = int(frame_height * 0.3) | ||
| cv2.line(frame, (0, roi_top), (frame_width, roi_top), (255, 0, 0), 2) | ||
| cv2.putText(frame, "Forward Area", (30, roi_top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2) | ||
|
|
||
| for cnt in contours: | ||
| area = cv2.contourArea(cnt) | ||
| if area < 2000: # 过滤小噪点 | ||
| continue | ||
|
|
||
| # 轮廓中心(只关注道路中间区域) | ||
| M = cv2.moments(cnt) | ||
| if M["m00"] == 0: | ||
| continue | ||
| cx = int(M["m10"] / M["m00"]) | ||
| cy = int(M["m01"] / M["m00"]) | ||
|
|
||
| # 限制在道路中间60%区域 | ||
| if cx > frame_width * 0.2 and cx < frame_width * 0.8 and cy > roi_top: | ||
| has_obstacle = True | ||
| obstacle_area = area | ||
|
|
||
| # 绘制标注 | ||
| cv2.drawContours(frame, [cnt], -1, (0, 0, 255), 3) | ||
| cv2.circle(frame, (cx, cy), 5, (255, 0, 0), -1) | ||
|
|
||
| # 危险距离判断(面积越大越近) | ||
| if area > 15000: | ||
| danger_distance = True | ||
|
|
||
| # 显示识别结果 | ||
| if has_obstacle: | ||
| if danger_distance: | ||
| cv2.putText(frame, "⚠️ DANGER: OBSTACLE AHEAD!", (30, 60), | ||
| cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 4) | ||
| else: | ||
| cv2.putText(frame, "⚠️ OBSTACLE DETECTED", (30, 60), | ||
| cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 0), 4) | ||
| else: | ||
| cv2.putText(frame, "✅ No Obstacle", (30, 60), | ||
| cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4) | ||
|
|
||
| return has_obstacle, danger_distance, frame | ||
|
|
||
| # -------------------------- 3. 实时运行模拟(流畅无延迟) -------------------------- | ||
| def run_obstacle_simulation(): | ||
| print("🚗 无人车障碍物识别模拟启动(按 'q' 键退出)") | ||
| print("模拟场景:随机生成道路、车辆、行人,实时检测前方障碍物") | ||
|
|
||
| while True: | ||
| # 生成模拟行驶帧 | ||
| frame, _ = generate_driving_frame() | ||
| # 执行障碍物识别 | ||
| _, _, annotated_frame = detect_obstacles(frame) | ||
| # 实时显示(10ms刷新,无延迟) | ||
| cv2.imshow("Obstacle Detection Simulation", annotated_frame) | ||
|
|
||
| # 按q退出 | ||
| if cv2.waitKey(10) & 0xFF == ord('q'): | ||
| break | ||
|
|
||
| cv2.destroyAllWindows() | ||
| print("✅ 模拟结束") | ||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| run_obstacle_simulation() | ||
| except Exception as e: | ||
| print(f"❌ 运行错误:{e}") | ||
| cv2.destroyAllWindows() |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 模块名太大了,需要细化 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 无人车 | ||
| 研究无人车的图像识别系统和,和自动驾驶功能 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import cv2 | ||
| import numpy as np | ||
| from PIL import Image, ImageDraw | ||
|
|
||
| # -------------------------- 1. 快速生成模拟帧(精简绘制,缩小分辨率) -------------------------- | ||
| def generate_traffic_light_frame(light_color="red"): | ||
| """快速生成红绿灯帧(800x600分辨率,精简绘制逻辑)""" | ||
| img_width, img_height = 800, 600 # 缩小分辨率,减少计算量 | ||
| background_color = (30, 30, 30) # 简化背景 | ||
| dark_color = (60, 60, 60) | ||
| light_colors = { | ||
| "red": (255, 30, 30), | ||
| "yellow": (255, 255, 30), | ||
| "green": (30, 255, 30) | ||
| } | ||
|
|
||
| # 快速创建图片(减少冗余绘制) | ||
| img = Image.new("RGB", (img_width, img_height), background_color) | ||
| draw = ImageDraw.Draw(img) | ||
|
|
||
| # 简化红绿灯绘制(只保留核心灯体,取消复杂装饰) | ||
| light_radius = 40 | ||
| light_positions = [ | ||
| (img_width//2, img_height//3), | ||
| (img_width//2, img_height//2), | ||
| (img_width//2, 2*img_height//3) | ||
| ] | ||
|
|
||
| for i, pos in enumerate(light_positions): | ||
| color = dark_color if not ( | ||
| (i==0 and light_color=="red") or | ||
| (i==1 and light_color=="yellow") or | ||
| (i==2 and light_color=="green") | ||
| ) else light_colors[light_color] | ||
| # 仅绘制核心灯体(取消光晕、简化边框) | ||
| draw.ellipse( | ||
| [pos[0]-light_radius, pos[1]-light_radius, | ||
| pos[0]+light_radius, pos[1]+light_radius], | ||
| fill=color, outline=(200,200,200), width=3 | ||
| ) | ||
|
|
||
| # 快速转换为OpenCV格式 | ||
| return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) | ||
|
|
||
| # -------------------------- 2. 优化识别逻辑(减少计算量) -------------------------- | ||
| def detect_traffic_light(frame): | ||
| hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | ||
|
|
||
| # 简化HSV阈值(减少判断耗时) | ||
| color_ranges = { | ||
| "red": [[(0, 120, 70), (10, 255, 255)], [(170, 120, 70), (180, 255, 255)]], | ||
| "yellow": [(22, 120, 70), (32, 255, 255)], | ||
| "green": [(45, 120, 70), (70, 255, 255)] | ||
| } | ||
|
|
||
| light_detected = "unknown" | ||
| max_light_area = 0 | ||
|
|
||
| for color, ranges in color_ranges.items(): | ||
| mask = np.zeros_like(hsv[:, :, 0]) | ||
| # 简化循环逻辑 | ||
| if color == "red": | ||
| mask = cv2.inRange(hsv, np.array(ranges[0][0]), np.array(ranges[0][1])) + \ | ||
| cv2.inRange(hsv, np.array(ranges[1][0]), np.array(ranges[1][1])) | ||
| else: | ||
| mask = cv2.inRange(hsv, np.array(ranges[0]), np.array(ranges[1])) | ||
|
|
||
| # 缩小形态学核(减少运算量) | ||
| kernel = np.ones((5, 5), np.uint8) | ||
| mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) | ||
| mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) | ||
|
|
||
| # 快速轮廓检测 | ||
| contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | ||
| for cnt in contours: | ||
| area = cv2.contourArea(cnt) | ||
| perimeter = cv2.arcLength(cnt, True) | ||
| if perimeter == 0: | ||
| continue | ||
| circularity = 4 * np.pi * area / (perimeter ** 2) | ||
| # 适配小分辨率的面积阈值 | ||
| if area > 3000 and circularity > 0.65: | ||
| if area > max_light_area: | ||
| max_light_area = area | ||
| light_detected = color | ||
|
|
||
| # 简化绘制标注 | ||
| cv2.putText( | ||
| frame, f"TL: {light_detected.upper()}", | ||
| (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3 | ||
| ) | ||
| return light_detected, frame | ||
|
|
||
| # -------------------------- 3. 高速运行循环(无延迟切换) -------------------------- | ||
| def run_fast_simulation(): | ||
| print("🚀 快速版模拟启动(按 'q' 退出)") | ||
| light_sequence = ["red", "yellow", "green"] | ||
| index = 0 | ||
| frame_count = 0 # 按帧切换,无强制休眠 | ||
|
|
||
| while True: | ||
| # 每15帧切换一次灯态(约0.3秒切换,流畅无延迟) | ||
| if frame_count % 15 == 0: | ||
| current_light = light_sequence[index % len(light_sequence)] | ||
| index += 1 | ||
|
|
||
| # 快速生成+识别 | ||
| frame = generate_traffic_light_frame(current_light) | ||
| _, annotated_frame = detect_traffic_light(frame) | ||
|
|
||
| # 高速显示(10ms刷新一次) | ||
| cv2.imshow("Fast Traffic Light Detection", annotated_frame) | ||
| frame_count += 1 | ||
|
|
||
| # 按q立即退出 | ||
| if cv2.waitKey(10) & 0xFF == ord('q'): | ||
| break | ||
|
|
||
| cv2.destroyAllWindows() | ||
| print("✅ 模拟结束") | ||
|
|
||
| if __name__ == "__main__": | ||
| run_fast_simulation() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不能放在项目根目录下,要放在自己模块文件夹内