把以上编码写成实验步骤
时间: 2024-12-04 10:28:21 浏览: 11
### 实验步骤:基于计算机视觉的自行车导航系统
#### 准备工作
1. **环境搭建**:
- 安装必要的Python包:`sklearn`, `numpy`, `opencv-python`, `enum`, `base64`, `math`, `struct`, `time`。
- 如果使用OpenNI摄像头,安装OpenNI库和驱动。
- 如果使用普通摄像头,确保摄像头正常连接并可用。
2. **导入依赖**:
```python
from sklearn.cluster import KMeans
import numpy as np
import cv2
from enum import Enum
import base64
import math
import struct
import time
```
#### 初始化参数和类
3. **定义状态枚举**:
```python
class State(Enum):
left = 1
right = 2
center = 3
left_to_center = 4
right_to_center = 5
```
4. **定义自行车参数类**:
```python
class Bike_args:
def __init__(self, _camera):
self.camera = _camera
self.blue_low = np.array([65, 66, 60])
self.blue_upper = np.array([125, 254, 255])
self.yellow_low = np.array([23, 0, 144])
self.yellow_upper = np.array([180, 255, 255])
self.find_current_x_delta_x = 130
self.shifted_x = 22
self.block_detect_y = 290
self.block_detect_delta_y = 80
self.block_h_upper = 30
self.stop_line1_detect_y = 400
self.wait_back_center_data = struct.pack("!BB", 0xA5, 0X01)
self.first_stop_line = struct.pack("!BB", 0xA5, 0X02)
```
5. **定义自行车主类**:
```python
class Bike_class:
def __init__(self, _socketio, _camera, _task=0, _block_nums=3, _skip_times=400, _block_dirc=(3, 3, 3)):
self.socketio = _socketio
self.init_flag = 0
self.kmeans = KMeans(n_clusters=2)
self.bike_args = Bike_args(_camera)
self.M = None
self.M_inverse = None
self.M_inverse_list = None
self.get_warp_M()
self.color_stream = None
self.cap = None
if self.bike_args.camera == 0:
from openni import openni2
openni2.initialize()
dev = openni2.Device.open_any()
self.color_stream = dev.create_color_stream()
self.color_stream.start()
elif self.bike_args.camera == 1:
self.cap = cv2.VideoCapture(0)
self.state = State.center
self.dynamic_center_x = 320
self.left_fit = None
self.right_fit = None
self.skip_frame_times = 0
self.initial_skip_frame_times = 80
self.shift_to_center_x = 0
self.to_center_x = 0
self.dynamic_shift_x = 0
self.task = _task
self.skip_block_nums = 0
self.last_block_state = State.center
self.catch_block_times = 0
self.wait_back_center_flag = 0
self.block_nums = _block_nums
self.stop_skip_times = _skip_times
self.block_direction_list = []
for _ in _block_dirc:
if _ == 1:
self.block_direction_list.append(State.right)
elif _ == 2:
self.block_direction_list.append(State.left)
else:
self.block_direction_list.append(State.center)
self.log_times = 0
self.block_detect_times = 0
self.stop_line_times = 0
self.img = None
self.warp_img = None
self.edges = None
self.line_points_x = []
self.left_line_x = []
self.right_line_x = []
self.leftx_mean = None
self.left_point_source = None
self.rightx_mean = None
self.right_point_source = None
self.catch_point_source = None
self.rightx_mean_list = []
self.leftx_mean_list = []
self.x_mean_list = []
self.x_mean = None
self.x_error = None
self.last_x_error = None
self.error_flag = 0
self.img_size = (640, 480)
self.y = 180
self.wait_back_center_flag_debug = 0
self.record_flag = 0
self.error_x_save_list = []
def show_image(self, _img, flag):
if flag == 0:
frame = cv2.resize(_img, (160, 120))
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 85]
_, compressed_frame = cv2.imencode('.jpg', frame, encode_param)
frame_data = base64.b64encode(compressed_frame).decode('utf-8')
self.socketio.emit('image', {'image_data': frame_data})
else:
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 85]
_, compressed_frame = cv2.imencode('.jpg', _img, encode_param)
frame_data = base64.b64encode(compressed_frame).decode('utf-8')
self.socketio.emit('event_image', {'image_data': frame_data})
def show_log(self, _text):
self.socketio.emit('log', {'log': _text})
def get_warp_M(self):
objdx = 200
objdy = 230
imgdx = 220
imgdy = 250
list_pst = [[172, 330], [461, 330], [75, 475], [546, 475]]
pts1 = np.float32(list_pst)
pts2 = np.float32([[imgdx, imgdy], [imgdx + objdx, imgdy], [imgdx, imgdy + objdy], [imgdx + objdx, imgdy + objdy]])
self.M = cv2.getPerspectiveTransform(pts1, pts2)
self.M_inverse = cv2.getPerspectiveTransform(pts2, pts1)
self.M_inverse_list = self.M_inverse.flatten()
def point_reverse_perspective(self, point):
x, y = point
denom = self.M_inverse_list[6] * x + self.M_inverse_list[7] * y + 1
x_transformed = (self.M_inverse_list[0] * x + self.M_inverse_list[1] * y + self.M_inverse_list[2]) / denom
y_transformed = (self.M_inverse_list[3] * x + self.M_inverse_list[4] * y + self.M_inverse_list[5]) / denom
return (int(x_transformed), int(y_transformed))
def interpolate_value(self, start_value, end_value, initial_times, current_times):
step = (end_value - start_value) / initial_times
current_value = start_value + step * current_times
return int(current_value)
def img_preprocess(self):
self.img = cv2.medianBlur(self.img, 9)
self.warp_img = cv2.warpPerspective(self.img, self.M, self.img_size)
warp_img_hsv = cv2.cvtColor(self.warp_img, cv2.COLOR_BGR2HSV)
blue_mask_white = cv2.inRange(warp_img_hsv, self.bike_args.blue_low, self.bike_args.blue_upper)
blue_mask_white = cv2.bitwise_not(blue_mask_white)
kernel = np.ones((5, 5), np.uint8)
blue_mask_white = cv2.erode(blue_mask_white, kernel, iterations=1)
edges = cv2.Canny(self.warp_img, 50, 40, apertureSize=3)
edges = cv2.bitwise_and(edges, edges, mask=blue_mask_white)
kernel = np.ones((3, 3), np.uint8)
edges = cv2.dilate(edges, kernel, iterations=2)
edges_mask = np.zeros((self.img_size[1], self.img_size[0]), dtype=np.uint8)
cv2.rectangle(edges_mask, (160, 0), (480, 480), 255, thickness=cv2.FILLED)
self.edges = cv2.bitwise_and(edges, edges, mask=edges_mask)
def img_HoughLines(self):
self.line_points_x.clear()
lines = cv2.HoughLines(self.edges, 1, np.pi / 180, threshold=260)
if lines is not None:
for line in lines:
rho, theta = line[0]
theta_degree = np.degrees(theta)
if theta_degree > 90:
theta_degree = 180 - theta_degree
if np.abs(theta_degree) > 35:
continue
elif np.abs(theta) == 0:
b = rho
self.line_points_x.append(int(b))
else:
m = -1 / np.tan(theta)
b = rho / np.sin(theta)
self.line_points_x.append(int((self.y - b) / m))
def img_HoughLines_filter(self):
self.left_line_x.clear()
self.right_line_x.clear()
if len(self.line_points_x) != 0:
for point_x in self.line_points_x:
if point_x < self.dynamic_center_x and point_x > (self.dynamic_center_x - self.bike_args.find_current_x_delta_x):
self.left_line_x.append(point_x)
cv2.circle(self.warp_img, (point_x, self.y), radius=5, color=(255, 255, 255), thickness=-1)
elif point_x > self.dynamic_center_x and point_x < (self.dynamic_center_x + self.bike_args.find_current_x_delta_x):
self.right_line_x.append(point_x)
cv2.circle(self.warp_img, (point_x, self.y), radius=5, color=(255, 255, 255), thickness=-1)
if self.state == State.left or self.state == State.left_to_center or self.state == State.center:
if len(self.left_line_x) != 0:
self.leftx_mean = int(np.mean(self.left_line_x))
cv2.line(self.warp_img, (self.leftx_mean, 0), (self.leftx_mean, 480), (255, 0, 0), 3)
self.error_flag = 0
else:
self.error_flag = 1
if self.state == State.right or self.state == State.right_to_center or self.state == State.center:
if len(self.right_line_x) != 0:
self.rightx_mean = int(np.mean(self.right_line_x))
cv2.line(self.warp_img, (self.rightx_mean, 0), (self.rightx_mean, 480), (255, 0, 0), 3)
self.error_flag = 0
else:
self.error_flag = 1
else:
self.error_flag = 1
def img_swap_windows(self):
margin = 35
minpix = 25
try:
if self.error_flag != 1:
left_lane_inds = []
last_good_left_inds_len = 0
right_lane_inds = []
last_good_right_inds_len = 0
nwindows = 8
window_height = np.int32(self.img_size[1] / nwindows)
nonzero = self.edges.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
for window in range(nwindows):
win_y_low = self.img_size[1] - (window + 1) * window_height
win_y_high = self.img_size[1] - window * window_height
if self.state == State.left or self.state == State.center or self.state == State.left_to_center:
win_xleft_low = self.leftx_mean - margin
win_xleft_high = self.leftx_mean + margin
if self.state == State.right or self.state == State.center or self.state == State.right_to_center:
win_xright_low = self.rightx_mean - margin
win_xright_high = self.rightx_mean + margin
if self.state == State.left or self.state == State.center or self.state == State.left_to_center:
good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0]
left_lane_inds.append(good_left_inds)
last_good_left_inds_len = len(good_left_inds)
if self.state == State.right or self.state == State.center or self.state == State.right_to_center:
good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0]
right_lane_inds.append(good_right_inds)
last_good_right_inds_len = len(good_right_inds)
if self.state == State.left or self.state == State.center or self.state == State.left_to_center:
if last_good_left_inds_len > minpix:
self.leftx_mean = np.int32(np.mean(nonzerox[good_left_inds]))
if self.state == State.right or self.state == State.center or self.state == State.right_to_center:
if last_good_right_inds_len > minpix:
self.rightx_mean = np.int32(np.mean(nonzerox[good_right_inds]))
if self.state == State.left or self.state == State.center or self.state == State.left_to_center:
left_lane_inds = np.concatenate(left_lane_inds)
leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
if self.state == State.right or self.state == State.center or self.state == State.right_to_center:
right_lane_inds = np.concatenate(right_lane_inds)
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
if self.state == State.left or self.state == State.left_to_center:
self.left_fit = np.polyfit(lefty, leftx, 2)
self.left_fit[2] = self.left_fit[2] + 5
_x = self.left_fit[0] * self.y ** 2 + self.left_fit[1] * self.y + self.left_fit[2]
self.left_point_source = self.point_reverse_perspective((_x, self.y))
elif self.state == State.right or self.state == State.right_to_center:
self.right_fit = np.polyfit(righty, rightx, 2)
_x = self.right_fit[0] * self.y ** 2 + self.right_fit[1] * self.y + self.right_fit[2]
self.right_point_source = self.point_reverse_perspective((_x, self.y))
elif self.state == State.
阅读全文