Jetson nano main function error

from jetbot import Robot
import cv2
import numpy as np

LINE_COLOR = [0, 255, 255]
LINE_THICKNESS = 30

def gstreamer_pipeline(
capture_width=1280,
capture_height=720,
display_width=1280,
display_height=720,
framerate=60,
flip_method=0,
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
“video/x-raw, format=(string)BGR ! appsink”
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)

def gray_scale(image):
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

def histogram_image(image):
return cv2.equalizeHist(image)

def binarization(image):
return cv2.threshold(image, 240, 255, cv2.THRESH_BINARY)

def low_pass_filter(image):
return cv2.medianBlur(image, 5)

def hough_transform(image):
rho = 1 #Distance resolution of the accumulator in pixels.
theta = np.pi/180 #Angle resolution of the accumulator in radians.
threshold = 10 #Only lines that are greater than threshold will be returned.
minLineLength = 10 #Line segments shorter than that are rejected.
maxLineGap = 0 #Maximum allowed gap between points on the same line to link them
return cv2.HoughLinesP(image, rho = rho, theta = theta, threshold = threshold,
minLineLength = minLineLength, maxLineGap = maxLineGap)

def mask_region(image):
mask_poly = np.array([[[0, 0], [0, 70], [80, 70], [80, 0]]])
cv2.fillPoly(image, mask_poly, (0, 0, 0))
return image

def pixel_points(y1, y2, line):
if line is None:
return None
slope, intercept = line
if slope == 0:
return None
x1 = int((y1 - intercept)/slope)
x2 = int((y2 - intercept)/slope)
y1 = int(y1)
y2 = int(y2)
return ((x1, y1), (x2, y2))

def average_slope_intercept(lines):
left_lines = #(slope, intercept)
left_weights = #(length,)
right_lines = #(slope, intercept)
right_weights = #(length,)

for line in lines:
    for x1, y1, x2, y2 in line:
        if x1 == x2:
            continue
        slope = (y2 - y1) / (x2 - x1)
        intercept = y1 - (slope * x1)
        length = np.sqrt(((y2 - y1) ** 2) + ((x2 - x1) ** 2))
        if slope < 0:
            left_lines.append((slope, intercept))
            left_weights.append((length))
        else:
            right_lines.append((slope, intercept))
            right_weights.append((length))
left_lane  = np.dot(left_weights,  left_lines) / np.sum(left_weights)  if len(left_weights) > 0 else None
right_lane = np.dot(right_weights, right_lines) / np.sum(right_weights) if len(right_weights) > 0 else None
return left_lane, right_lane

def lane_lines(image, lines):
left_lane, right_lane = average_slope_intercept(lines)
y1 = image.shape[0]
y2 = y1 * 0.5
left_line = pixel_points(y1, y2, left_lane)
right_line = pixel_points(y1, y2, right_lane)
return left_line, right_line

def draw_lane_lines(image, lines):
line_image = np.zeros_like(image)
for line in lines:
if line is not None:
cv2.line(line_image, *line, LINE_COLOR, LINE_THICKNESS)
return cv2.addWeighted(image, 1.0, line_image, 1.0, 0.0)

def findLane(src):
gray = gray_scale(src)
hist = histogram_image(gray)
ret, binary = binarization(hist)
median = low_pass_filter(binary)
hough = hough_transform(median)
region = mask_region(hough)
lane = None if (len(region) < 4) else lane_lines(src, region)
result_image = draw_lane_lines(src, lane)
return lane, result_image

ROBOT_MOVEMENT = False
SWAP_LR_MOTOR = True

def angle_between(p1, p2):
ang1 = np.arctan2(*p1[::-1])
ang2 = np.arctan2(*p2[::-1])
return np.rad2deg((ang1 - ang2) % (2 * np.pi))

def get_direction(image, lines):
x1 = y1 = x2 = y2 = 0
line_count = 0
for line in lines:
if line is not None:
x1 += line[0][0]
y1 += line[0][1]
x2 += line[1][0]
y2 += line[1][1]
line_count = line_count + 1
if line_count is not None:
x1 = x1 / line_count
x2 = x2 / line_count
y1 = y1 / line_count
y2 = y2 / line_count
else :
return None
angle = angle_between((x1 - x2, y1 - y2), (0, 1))
return angle

def calc_motor_PWR(direction, normal_speed=0.3):
if direction is None:
return 0, 0
fix = -(direction - 0.5)/8
if fix > 0:
right_PWR = normal_speed + fix
left_PWR = normal_speed
else:
right_PWR = normal_speed
left_PWR = normal_speed - fix
print([direction, fix, left_PWR, right_PWR])
return left_PWR, right_PWR

def robot_move(robot=None, left_motor_PWR=0, right_motor_PWR=0, adjust_gain = 0.03):
if robot is None:
return
if SWAP_LR_MOTOR:
robot.set_motors(-(right_motor_PWR * (1-adjust_gain)), -(left_motor_PWR * (1+adjust_gain)))
else:
robot.set_motors(left_motor_PWR * (1+adjust_gain), right_motor_PWR * (1-adjust_gain))

def robot_follow_direction(robot=None, direction=None, normal_speed=0.3):
if ROBOT_MOVEMENT:
left_PWR, right_PWR = calc_motor_PWR(direction, normal_speed)
robot_move(robot, left_PWR, right_PWR)

if name == ‘main’:
robot = Robot()
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)

if cap.isOpened():
    while True:
        rate, frame = cap.read()
        if rate:
            lane, result = findLane(frame)
            if lane is None:
                continue
            cv2.imshow("Lane Detect frame", result)
            cv2.waitKey(2500)
                      
            direction = get_direction(result, lane)
        
            robot.stop()
            robot_follow_direction(robot = robot, direction = direction) 
        else:                      
            break
else:
    print("can't open camera.")
cap.release()
cv2.destroyAllWindows()

this code, jeson nano has error. image don’t appear on the screen. and
infinite loop continue. I would like to operate jetson nano using opencv.
what is the problem? please know me.

1 Like