Python OpenCV Lane Detector "cannot unpack non-iterable numpy.uint8 object" Error



我一直在尝试使用网络摄像头进行输入,但它给了我错误。代码正在处理图片。我做错了什么

我尝试将 x1、x2、y1、y2 设置为整数值,并在 make_points 函数上尝试了 NP.ARRAY

,这是代码
import cv2
import numpy as np
def detect_edges(frame):
gry = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gry, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)
return edges
def region_of_interest(edges):
height, width = edges.shape
mask = np.zeros_like(edges)
polygon = np.array([[
(0, height * 1 / 2),
(width, height * 1 / 2),
(width, height),
(0, height),
]], np.int32)
cv2.fillPoly(mask, polygon, 255)
cropped_edges = cv2.bitwise_and(edges, mask)
return cropped_edges
def detect_line_segments(cropped_edges):
rho = 1
angle = np.pi / 180
min_threshold = 10
line_segments = cv2.HoughLinesP(cropped_edges, rho, angle, min_threshold,
np.array([]), minLineLength=8, maxLineGap=4)
return line_segments
def average_slope_intercept(frame, line_segments):
lane_lines = []
if line_segments is None:
print("No LineSegments!")
return lane_lines
height, width, _ = frame.shape
left_fit = []
right_fit = []
boundary = 1/3
left_region_boundary = width * (1 - boundary)
right_region_boundary = width * boundary
for line_segment in line_segments:
for x1, y1, x2, y2 in line_segment:
if x1 == x2:
print("Skipping Vertical Line Segment?")
continue
fit = np.polyfit((x1, x2), (y1, y2), 1)
slope = fit[0]
intercept = fit[1]
if slope < 0:
if x1 < left_region_boundary and x2 < left_region_boundary:
left_fit.append((slope, intercept))
else:
if x1 > right_region_boundary and x2 > right_region_boundary:
right_fit.append((slope, intercept))
left_fit_average = np.average(left_fit, axis=0)
if len(left_fit) > 0:
lane_lines.append(make_points(frame, left_fit_average))
right_fit_average = np.average(right_fit, axis=0)
if len(right_fit) > 0:
lane_lines.append(make_points(frame, right_fit_average))
print("Lane_Lines :", lane_lines)
return lane_lines
def make_points(frame, line):
height, width, _ = frame.shape
slope, intercept = line
y1 = height
y2 = int(y1 * 1 / 2)
x1 = max(-width, min(2 * width, int((y1 - intercept) / slope)))
x2 = max(-width, min(2 * width, int((y2 - intercept) / slope)))
return [[x1, y1, x2, y2]]
def detect_lane(frame):
edges = detect_edges(frame)
cropped_edges = region_of_interest(edges)
line_segments = detect_line_segments(cropped_edges)
lane_lines = average_slope_intercept(frame, line_segments)
return lane_lines
def display_lines(frame, lines, line_color=(0, 255, 0), line_width=2):
line_image = np.zeros_like(frame)
if lines is not None:
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(line_image, (int(x1), int(y1)), (int(x2), int(y2)), line_color, line_width)
line_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
return line_image
cap = cv2.VideoCapture("/dev/video0")
while (cap.isOpened()):
_, frame = cap.read()
lane_lines = detect_edges(frame)
lane_lines_image = display_lines(frame, lane_lines, line_color=(0, 255, 0), line_width=2)
cv2.imshow("lane lines", lane_lines_image)
if cv2.waitKey(1) == 27:
self.cleanup()
break

cv2.imshow(( 屏幕根本没有显示,网络摄像头指示灯只是闪烁 1 次,然后吐出错误

错误是

root@kali:~/Desktop/Project-OUTO# python3 WebLane.py 
Traceback (most recent call last):
File "WebLane.py", line 108, in <module>
lane_lines_image = display_lines(frame, lane_lines, line_color=(0, 255, 0), line_width=2)
File "WebLane.py", line 98, in display_lines
for x1, y1, x2, y2 in line:
TypeError: cannot unpack non-iterable numpy.uint8 object

编辑:在打印(行(之前放置打印(行(,因为这是它吐出的内容

root@kali:~/Desktop/Project-OUTO# python3 WebLane.py 
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0]
Traceback (most recent call last):
File "WebLane.py", line 109, in <module>
lane_lines_image = display_lines(frame, lane_lines, line_color=(0, 255, 0), line_width=2)
File "WebLane.py", line 99, in display_lines
for x1, y1, x2, y2 in line:
TypeError: cannot unpack non-iterable numpy.uint8 object

由于用户 furas 而解决 我不小心写了lane_lines = detect_edges(( 意味着lane_lines = detect_lane(( 这为我解决了这个问题,谢谢你的愤怒:D

最新更新