Python OpenCV车道检测器“;无法解压缩不可编辑的numpy.uint8对象“;错误

Python OpenCV车道检测器“;无法解压缩不可编辑的numpy.uint8对象“;错误,python,numpy,opencv,Python,Numpy,Opencv,我一直在尝试使用网络摄像头进行输入,但它给了我一个错误。代码正在处理图片。我做错了什么 我尝试将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)

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

我尝试将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
将numpy作为np导入
def检测_边缘(帧):
gry=cv2.CVT颜色(边框,cv2.COLOR\u RGB2GRAY)
模糊=cv2.高斯模糊(gry,(5,5,0)
边缘=cv2.Canny(模糊,50,150)
返回边
def感兴趣区域(边缘):
高度、宽度=边。形状
掩码=np.类零(边)
多边形=np.数组([[
(0,高度*1/2),
(宽度、高度*1/2),
(宽度、高度),
(0,高度),
]],np.int32)
cv2.fillPoly(遮罩,多边形,255)
裁剪的_边=cv2.按位_和(边,遮罩)
返回裁剪的_边
def检测线段(裁剪边):
rho=1
角度=np.pi/180
最小阈值=10
直线段=cv2.HoughLinesP(裁剪的边缘、菱形、角度、最小阈值、,
np.array([]),minLineLength=8,maxLineGap=4)
返回直线段
def平均斜率截距(帧、线段):
车道线=[]
如果直线段为“无”:
打印(“无线段!”)
回程线
高度、宽度,u=frame.shape
左拟合=[]
右拟合=[]
边界=1/3
左_区域_边界=宽度*(1-边界)
右\区域\边界=宽度*边界
对于直线段中的直线段:
对于线_段中的x1、y1、x2、y2:
如果x1==x2:
打印(“跳过垂直线段?”)
持续
拟合=np.多边形拟合((x1,x2),(y1,y2),1)
斜率=拟合[0]
截距=拟合[1]
如果坡度<0:
如果x1<左_区域_边界,x2<左_区域_边界:
左拟合附加((斜率,截距))
其他:
如果x1>右\u区域\u边界和x2>右\u区域\u边界:
右拟合附加((斜率,截距))
左拟合平均值=np.平均值(左拟合,轴=0)
如果len(左_配合)>0:
车道线。附加(生成点(帧,左拟合平均值))
右拟合平均值=np.平均值(右拟合,轴=0)
如果len(右螺纹配合)>0:
车道线。附加(生成点(帧,右拟合平均值))
打印(“车道线:”,车道线)
回程线
def生成点(帧、线):
高度、宽度,u=frame.shape
斜率,截距=直线
y1=高度
y2=int(y1*1/2)
x1=最大值(-宽度,最小值(2*宽度,整数((y1-截距)/斜率)))
x2=最大值(-宽度,最小值(2*宽度,整数((y2-截距)/斜率)))
返回[[x1,y1,x2,y2]]
def检测通道(帧):
边=检测边(帧)
裁剪的边=感兴趣的区域(边)
直线段=检测直线段(裁剪边)
车道线=平均坡度截距(框架、直线段)
回程线
def显示线(帧、线、线颜色=(0、255、0)、线宽度=2):
线条图像=np.类零(帧)
如果行不是无:
对于行中的行:
对于直线中的x1、y1、x2、y2:
cv2.线条(线条图像,(int(x1),int(y1)),(int(x2),int(y2)),线条颜色,线条宽度)
line_image=cv2.addWeighted(帧,0.8,line_image,1,1)
返回线图像
cap=cv2.VideoCapture(“/dev/video0”)
while(cap.isOpened()):
_,frame=cap.read()
车道线=检测边缘(帧)
车道线图像=显示线(边框,车道线,线条颜色=(0,255,0),线条宽度=2)
cv2.imshow(“车道线”,车道线\u图像)
如果cv2.waitKey(1)==27:
self.cleanup()
打破
cv2.imshow()屏幕根本不显示,网络摄像头的指示灯只闪烁了一次,然后就吐出了错误

错误是

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
回溯(最近一次呼叫最后一次):
文件“WebLane.py”,第108行,在
车道线图像=显示线(边框,车道线,线条颜色=(0,255,0),线条宽度=2)
显示行中第98行的文件“WebLane.py”
对于直线中的x1、y1、x2、y2:
TypeError:无法解压缩不可编辑的numpy.uint8对象
编辑:将打印(行)放在前面,因为这是它吐出的内容

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 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 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 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 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 0 0 0 0
 0 0 0 0 0 0 0 0 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
root@kali:~/Desktop/Project OUT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 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 0 0 0 0 0 0 0 0 0]
回溯(最近一次呼叫最后一次):
文件“WebLane.py”,第109行,在
车道线图像=显示线(边框,车道线,线条颜色=(0,255,0),线条宽度=2)
显示行中第99行的文件“WebLane.py”
对于直线中的x1、y1、x2、y2:
TypeError:无法解包否