Skip to content

Instantly share code, notes, and snippets.

@archishou
Created June 20, 2019 20:56
Show Gist options
  • Save archishou/cd4c36727c40d884c169195b7a60ceff to your computer and use it in GitHub Desktop.
Save archishou/cd4c36727c40d884c169195b7a60ceff to your computer and use it in GitHub Desktop.
import cv2 as cv
import math
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from detectors.color_detector import ColorDetector
from processes.blurs import Blurs
import numpy as np
# load video
cap = cv.VideoCapture('testvideo.mp4')
# cap = cv.VideoCapture(0)
blurs = Blurs()
colorDetector = ColorDetector(lhsv = np.array([30, 60, 0]), uhsv = np.array([179, 255, 255]))
# colorDetector.enable_trackbars()
# Get shape of frame
width = cap.get(3)
height = cap.get(4)
# Percent of frame cropped for height and width
crop_height = 0.85
crop_width = 0
# 4 Points that define the point of intrest for the frame. The side walk should be near the center
# This array defines a trapazoid.
region_of_interest_vertices = [
(0, height * crop_height),
(width * crop_width, height * (1 - crop_height)),
(width * (1 - crop_width), height * (1 - crop_height)),
(width, height * crop_height),
]
# Draw lines given a frame and an array of line start/end coordinates
def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
line_img = np.zeros(
(
img.shape[0],
img.shape[1],
3
),
dtype=np.uint8
)
img = np.copy(img)
if lines is None:
return
for line in lines:
for x1, y1, x2, y2 in line:
cv.line(line_img, (int(x1), int(y1)), (int(x2), int(y2)), color, thickness)
img = cv.addWeighted(img, 0.8, line_img, 1.0, 0.0)
return img
# Crop frame
def crop(img, vertices):
# Define a blank matrix that matches the image height/width.
mask = np.zeros_like(img)
#defining a 3 channel or 1 channel color to fill the mask with depending on the input image
if len(img.shape) > 2:
channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
#filling pixels inside the polygon defined by "vertices" with the fill color
cv.fillPoly(mask, vertices, ignore_mask_color)
#returning the image only where mask pixels are nonzero
masked_image = cv.bitwise_and(img, mask)
return masked_image
# Main processing
while True:
_, init_frame = cap.read()
frame = blurs.average_blur(init_frame, 30)
cropped_frame = crop(
init_frame,
np.array([region_of_interest_vertices], np.int32),
)
mask = colorDetector.get_mask(cropped_frame)
mask = cv.bitwise_not(mask)
colored_frame = cv.bitwise_and(cropped_frame, cropped_frame, mask = mask)
edges = cv.Canny(mask, 200, 200)
cropped_edges = crop(
edges,
np.array([region_of_interest_vertices], np.int32),
)
lines = cv.HoughLinesP(
cropped_edges,
rho=6,
theta=np.pi / 60,
threshold=160,
lines=np.array([]),
minLineLength=100,
maxLineGap=25
)
# If no lines are detected in the frame
if np.shape(lines) != (): line_image = draw_lines(init_frame, lines)
else: line_image = init_frame
mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
cropped_edges = cv.cvtColor(cropped_edges, cv.COLOR_GRAY2BGR)
#out = np.concatenate((line_image, line_image2), axis = 1)
cv.imshow("out", line_image)
key = cv.waitKey(1)
if key == 27:
break
cap.release()
cv.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment