web-dev-qa-db-ja.com

反復不可能なnumpy.float64オブジェクトpython3 opencvを解凍できません

このエラーが発生し、問題が発生している理由を理解できません。以下はコードとエラーになります。

最後の印刷可能なトレーニングの結果

_[-8.54582258e-01  9.83741381e+02] left
[   0.776281243  -160.77584028] right
_

コードエラーは_make_coordinates_で発生し、行は

_slope, intercept = line_parameters
_

完全なコードは次のとおりです。

_import cv2
import numpy as np

vid = cv2.VideoCapture('carDriving.mp4')

def processImage(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    canny = cv2.Canny(blur, 50, 150)
    return canny

def region_of_interest(image):
    height = image.shape[0]
    polygons = np.array([
    [(200,height), (1200,height), (750,300)]
    ])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, polygons, 255)
    masked_image = cv2.bitwise_and(image, mask) 
    return masked_image

def display_lines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            cv2.line(line_image, (x1, y1), (x2, y2), (255,0,0), 10)
    return line_image

def average_slope_intercept(image, lines):
    left_fit = []
    right_fit = []
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            parameters = np.polyfit((x1, x2), (y1, y2), 1)
            slope = parameters[0]
            intercept = parameters[1]
            if slope < 0:
                left_fit.append((slope, intercept))
            else:
                right_fit.append((slope, intercept))
        left_fit_average = np.average(left_fit, axis=0)
        right_fit_average = np.average(right_fit, axis=0)
        print(left_fit_average, 'left')
        print(right_fit_average, 'right')
        left_line = make_coordinates(image, left_fit_average)
        right_line = make_coordinates(image, right_fit_average)
        #return np.array([left_line, right_line])

def make_coordinates(image, line_parameters):
    slope, intercept = line_parameters
    y1 = image.shape[0]
    y2 = int(y1*3/5)
    x1 = int(y1 - intercept)/slope
    x1 = int(y2 - intercept)/slope
    return np.array([x1, y1, x2, y2])

while True:
    ret, frame = vid.read()
    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    processed_image = processImage(frame)
    cropped_image = region_of_interest(processed_image)
    lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
    averaged_lines = average_slope_intercept(grayFrame, lines)
    line_image = display_lines(cropped_image,lines) 
    combo_image = cv2.addWeighted(grayFrame, .6, line_image, 1, 1)
    cv2.imshow('result', combo_image)
    print(lines)
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

vid.release()
cv2.destroyAllWindows()
_

そして完全なエラーメッセージ:

_Message=cannot unpack non-iterable numpy.float64 object
Source=C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py
  StackTrace:
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 52, in make_coordinates
    slope, intercept = line_parameters
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 47, in average_slope_intercept
    left_line = make_coordinates(image, left_fit_average)
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 65, in <module>
    averaged_lines = average_slope_intercept(grayFrame, lines)
_

27行目の別のエラーを受け取りました、最初のエラーが修正されました

_Message=integer argument expected, got float
  Source=C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py
  StackTrace:
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 27, in display_lines
    cv2.line(line_image, (x1, y1), (x2, y2), (255,0,0), 10)
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 76, in <module>
    line_image = display_lines(cropped_image,averaged_lines)
_

27行目をcv2.line(line_image, int(x1, y1), int(x2, y2), (255,0,0), 10)に変更すると、次のエラーが発生します

_  Message='numpy.float64' object cannot be interpreted as an integer
  Source=C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py
  StackTrace:
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 27, in display_lines
    cv2.line(line_image, int(x1, y1), int(x2, y2), (255,0,0), 10)
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 76, in <module>
    line_image = display_lines(cropped_image,averaged_lines)
_
5

@telの回答に従って、私はいくつかを追加したいのですが、

try:
    slope, intercept = line_parameters
except TypeError:
    slope, intercept = 0.001, 0 // It will minimize the error detecting the lane (putting 0, give you a math error)

繰り返しますが、車線間の距離が非常に大きい場合は、maxLineGapの値を増やして車線をキャッチできます。

0

私は解決策を見つけました、あなたのコードには間違ったインデントがあります:あなたのコードの代わりに:

_def average_slope_intercept(image, lines):
    left_fit = []
    right_fit = []
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            parameters = np.polyfit((x1, x2), (y1, y2), 1)
            slope = parameters[0]
            intercept = parameters[1]
            if slope < 0:
                left_fit.append((slope, intercept))
            else:
                right_fit.append((slope, intercept))
        **left_fit_average = np.average(left_fit, axis=0)
        right_fit_average = np.average(right_fit, axis=0)
        print(left_fit_average, 'left')
        print(right_fit_average, 'right')
        left_line = make_coordinates(image, left_fit_average)
        right_line = make_coordinates(image, right_fit_average)
        #return np.array([left_line, right_line])**

_

right_fit.append((slope, intercept))の後、関数の最後までインデントを1つ減らす必要があります。

したがって、コードは次のようにする必要があります。

_def average_slope_intercept(image, lines):
    left_fit = []
    right_fit = []
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            parameters = np.polyfit((x1, x2), (y1, y2), 1)
            slope = parameters[0]
            intercept = parameters[1]
            if slope < 0:
                left_fit.append((slope, intercept))
            else:
                right_fit.append((slope, intercept))
    left_fit_average = np.average(left_fit, axis=0)
    right_fit_average = np.average(right_fit, axis=0)
    print(left_fit_average, 'left')
    print(right_fit_average, 'right')
    left_line = make_coordinates(image, left_fit_average)
    right_line = make_coordinates(image, right_fit_average)
    return np.array([left_line, right_line])
_
0
Vasily Poupkine