1

I'm trying to copy the URL of my phone that is live streaming, and import it into my code so that it opens a new window with the live stream visible.

The URL I'm trying to import is https://192.168.1.69:8080

Here's my code:

import cv2
import numpy as np

def make_coordinates(image, line_parameters):
    try:
        slope, intercept = line_parameters
    except TypeError:
        slope, intercept = 1,1
    y1 = image.shape[0]
    y2 = int(y1*(3/4)) #Line length
    x1 = int((y1 - intercept)/slope)
    x2 = int((y2 - intercept)/slope)
    return np.array([x1, y1, x2, y2])

def averaged_slope_intercept(image, lines):
    left_fit = []
    right_fit = []
    for line in lines:
        x1, y1, x2, y2 = line.reshape(4)
        parameters = np.polyfit((x1, x2), (y1, y2), 1)
        print(parameters)
        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)
    left_line = make_coordinates(image, left_fit_average)
    right_line = make_coordinates(image, right_fit_average)
    return np.array([left_line, right_line])

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

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

def region_of_interest(image):
    height = image.shape[0]
    polygons = np.array([
    [(300, height), (1300, height), (500, 400)] #Dimensions of Polygon
    ])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, polygons, 255)
    masked_image = cv2.bitwise_and(image, mask)
    return masked_image

# image = cv2.imread('test_image.jpg')
# lane_image = np.copy(image)
# canny_image = canny(lane_image)
# cropped_image = region_of_interest(canny_image)
# lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
# averaged_lines = averaged_slope_intercept(lane_image, lines)
# line_image = display_lines(lane_image, averaged_lines)
# combo_image = cv2.addWeighted(lane_image, 0.8, line_image, 1, 1)
# cv2.imshow("result", combo_image)
# cv2.waitKey(0)

cap = cv2.VideoCapture("coast.mp4")
while(cap.isOpened()):
    _, frame = cap.read()
    canny_image = canny(frame)
    cropped_image = region_of_interest(canny_image)
    lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 250, np.array([]), minLineLength=40, maxLineGap=5) #Number of minimum line votes
    averaged_lines = averaged_slope_intercept(frame, lines)
    line_image = display_lines(frame, averaged_lines)
    combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
    cv2.imshow("Lane-Detection", combo_image)
    if cv2.waitKey(1) == ord('w'):
        break
cap.release()
cv2.destroyAllWindows()

I'm trying to place it where the "coast.mp4" is.

The code is for detecting lanes lines when driving, and I'm able to run the code with videos that are already downloaded on my computer (e.g. a video called "coast.mp4"), but I thought it would be interesting to see if I could implement a live stream so that the computer can detect lines in real-time. Is this possible?

Aliasgher Nooruddin
  • 534
  • 1
  • 6
  • 18

1 Answers1

0

If this is a streaming URL, you can simply do it like this

cv2.VideoCapture("https://192.168.1.69:8080/video")
Abdelsalam Hamdi
  • 465
  • 3
  • 13
  • I have tried that, and the code runs successfully, but there is no window that pops up to allow me to view it, and it finishes in ~2 seconds. – cheeseexpert Nov 30 '20 at 01:34
  • @cheeseexpert , you need to make sure ur url is a streaming url. you can also try this cv2.VideoCapture("https://192.168.1.69:8080/video") you can also refer to this link https://stackoverflow.com/questions/49978705/access-ip-camera-in-python-opencv – Abdelsalam Hamdi Nov 30 '20 at 03:46