Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Revert "fix bug with lowerhsv and apply precommit"
Browse files Browse the repository at this point in the history
This reverts commit 47b84ef.
  • Loading branch information
NLmeng committed Mar 4, 2023
1 parent 0c64b53 commit bc467bb
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 186 deletions.
15 changes: 5 additions & 10 deletions algorithms/CenterDownwards.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def __init__(self, config):

# filtering parameters
self.averaging_kernel_size = config.averaging_kernel_size
self.gauss_kernel_size = list(
map(int, config.gauss_kernel_size.split(',')))
self.gauss_kernel_size = list(map(int, config.gauss_kernel_size.split(',')))
self.dilate_kernel_size = config.dilate_kernel_size
self.sigma_x = config.sigma_x

Expand Down Expand Up @@ -122,8 +121,7 @@ def get_contours(self, binary_mask):
"""
frame = np.zeros((self.HEIGHT, self.WIDTH, 3))
ret, thresh = cv.threshold(binary_mask, 0, 254, 0)
contours, hierarchy = cv.findContours(
thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

return contours, frame

Expand Down Expand Up @@ -151,8 +149,7 @@ def ellipse_slopes(self, contours, black_frame):
rect = cv.minAreaRect(con)
box = cv.boxPoints(rect)
box = np.int0(box)
black_frame = cv.drawContours(
black_frame, [box], 0, (255, 255, 255), 2)
black_frame = cv.drawContours(black_frame, [box], 0, (255, 255, 255), 2)
ellipse = cv.fitEllipse(con)
self.contours.append(ellipse)
cv.ellipse(black_frame, ellipse, (255, 255, 255), 2)
Expand Down Expand Up @@ -186,13 +183,11 @@ def ellipse_slopes(self, contours, black_frame):
slopes.append(slope)
lefty = int((-x * vy / vx) + y)
righty = int(((cols - x) * vy / vx) + y)
black_frame = cv.line(
black_frame, (cols - 1, righty), (0, lefty), (255, 255, 0), 9)
black_frame = cv.line(black_frame, (cols - 1, righty), (0, lefty), (255, 255, 0), 9)
lines.append([cols - 1, righty, 0, lefty])

else:
black_frame = cv.line(black_frame, (int(x), 0),
(int(x), rows - 1), (255, 255, 0), 9)
black_frame = cv.line(black_frame, (int(x), 0), (int(x), rows - 1), (255, 255, 0), 9)
lines.append([int(x), 0, int(x), rows - 1])

return lines, slopes, black_frame
Expand Down
20 changes: 7 additions & 13 deletions algorithms/CenterRowAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def __init__(self, config):

# filtering parameters
self.averaging_kernel_size = config.averaging_kernel_size
self.gauss_kernel_size = list(
map(int, config.gauss_kernel_size.split(',')))
self.gauss_kernel_size = list(map(int, config.gauss_kernel_size.split(',')))
self.dilate_kernel_size = config.dilate_kernel_size
self.sigma_x = config.sigma_x

Expand Down Expand Up @@ -55,7 +54,7 @@ def update_lower_hsv(self, next):

def update_upper_hsv(self, next):
self.HIGH_GREEN = np.array(next)

def process_frame(self, frame, show):
"""Uses contouring to create contours around each crop row and uses these contours to find centroid lines,
row vanishing point, a center contour and the angle between the center contour and vanishing point\n
Expand All @@ -77,25 +76,22 @@ def process_frame(self, frame, show):
cv.drawContours(black_frame, contours, -1, self.contour_color, 3)
# fillPoly fills in the polygons in the frame
cv.fillPoly(black_frame, pts=contours, color=self.contour_color)
lines, slopes, ellipse_frame = self.ellipse_slopes(
contours, black_frame)
lines, slopes, ellipse_frame = self.ellipse_slopes(contours, black_frame)

if show:
Lines.draw_lines_on_frame(lines, black_frame)

intersections = Lines.get_intersections(lines)
x_points = [point[0] for point in intersections]
y_points = [point[1] for point in intersections]
vanishing_point = Lines.draw_vanishing_point(
ellipse_frame, x_points, y_points, show)
vanishing_point = Lines.draw_vanishing_point(ellipse_frame, x_points, y_points, show)

if vanishing_point:
center_contour, angle = self.find_center_contour(vanishing_point)
if show:
cv.ellipse(black_frame, center_contour, (0, 255, 0), 2)

angle = Lines.calculate_angle_from_v_point(
vanishing_point, self.WIDTH, self.HEIGHT)
angle = Lines.calculate_angle_from_v_point(vanishing_point, self.WIDTH, self.HEIGHT)

return black_frame, angle

Expand Down Expand Up @@ -125,8 +121,7 @@ def get_contours(self, binary_mask):
"""
frame = np.zeros((self.HEIGHT, self.WIDTH, 3))
ret, thresh = cv.threshold(binary_mask, 0, 254, 0)
contours, hierarchy = cv.findContours(
thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(frame, contours, -1, (0, 255, 0), 2)
cv.fillPoly(frame, pts=contours, color=(0, 255, 0))

Expand All @@ -148,8 +143,7 @@ def ellipse_slopes(self, contours, black_frame):
rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
black_frame = cv.drawContours(
black_frame, [box], 0, (255, 255, 255), 2)
black_frame = cv.drawContours(black_frame, [box], 0, (255, 255, 255), 2)
ellipse = cv.fitEllipse(cnt)
self.contours.append(ellipse)
cv.ellipse(black_frame, ellipse, (255, 255, 255), 2)
Expand Down
2 changes: 1 addition & 1 deletion algorithms/CheckRowEnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def update_lower_hsv(self, next):

def update_upper_hsv(self, next):
self.HIGH_GREEN = np.array(next)

def process_frame(self, frame, show):
"""Averages values in each row in a mask of the frame. If the number of rows with an average value
of zero is greater than req_rows_empty, then frame is row end\n
Expand Down
11 changes: 4 additions & 7 deletions algorithms/HoughAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def update_lower_hsv(self, next):

def update_upper_hsv(self, next):
self.HIGH_GREEN = np.array(next)

# processFrame function that is called to process a frame of a video
# takes in frame mat object obtained from cv2 video.read()
def process_frame(self, frame, show=True):
Expand Down Expand Up @@ -92,15 +92,12 @@ def process_frame(self, frame, show=True):
y_points = [point[1] for point in intersections]

if show:
vanishing_point = Lines.draw_vanishing_point(
line_img, x_points, y_points, show)
vanishing_point = Lines.draw_vanishing_point(line_img, x_points, y_points, show)
else:
vanishing_point = Lines.draw_vanishing_point(
frame, x_points, y_points, show)
vanishing_point = Lines.draw_vanishing_point(frame, x_points, y_points, show)

# Calculating angle from vanishing point to (self.WIDTH // 2, 0)
angle = Lines.calculate_angle_from_v_point(
vanishing_point, self.WIDTH, self.HEIGHT)
angle = Lines.calculate_angle_from_v_point(vanishing_point, self.WIDTH, self.HEIGHT)

if show:
return line_img, angle
Expand Down
29 changes: 10 additions & 19 deletions algorithms/MiniContoursAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def get_centroids(self, mask, num_strips):

centroids = []
for i, strip in enumerate(strips):
contours, hierarchy = cv2.findContours(
strip, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv2.findContours(strip, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
strip_centroids = []
for contour in contours:
M = cv2.moments(contour)
Expand Down Expand Up @@ -107,8 +106,7 @@ def get_center_hough_lines(
# lines: list of [[votes, rho, theta]] of all lines
# point_lines: list of [votes, pt1, pt2] of all lines

mask = cv2.inRange(cv2.cvtColor(
frame, cv2.COLOR_BGR2HSV), self.low_green, self.high_green)
mask = cv2.inRange(cv2.cvtColor(frame, cv2.COLOR_BGR2HSV), self.low_green, self.high_green)
mask = cv2.medianBlur(mask, 9)
# mask = cv2.GaussianBlur(mask, (9,9), 10)
# mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, self.morphology_kernel)
Expand Down Expand Up @@ -136,8 +134,7 @@ def get_center_hough_lines(
if show:

cv2.line(frame, (0, cut_off_height), (width // 2, 0), self.color_2)
cv2.line(frame, (width, cut_off_height),
(width // 2, 0), self.color_2)
cv2.line(frame, (width, cut_off_height), (width // 2, 0), self.color_2)

for i, strip_centroid in enumerate(centroids):
if i > int(0.3 * len(centroids)):
Expand All @@ -152,16 +149,12 @@ def get_center_hough_lines(
segmented_points[idx].append([int(x), int(y)])

if show:
cv2.circle(frame, (int(centroid[0]), int(
centroid[1])), 3, self.color_1, -1)
cv2.circle(mask, (int(centroid[0]), int(
centroid[1])), 3, self.color_1, -1)
points_vector.append(
[int(centroid[0]), int(centroid[1])])
cv2.circle(frame, (int(centroid[0]), int(centroid[1])), 3, self.color_1, -1)
cv2.circle(mask, (int(centroid[0]), int(centroid[1])), 3, self.color_1, -1)
points_vector.append([int(centroid[0]), int(centroid[1])])
else:
if show:
cv2.circle(frame, (int(centroid[0]), int(
centroid[1])), 3, self.color_3, -1)
cv2.circle(frame, (int(centroid[0]), int(centroid[1])), 3, self.color_3, -1)

c_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

Expand Down Expand Up @@ -217,7 +210,7 @@ def update_lower_hsv(self, next):

def update_upper_hsv(self, next):
self.HIGH_GREEN = np.array(next)

def process_frame(self, original_frame, num_strips=60, show=False):

# original_frame: BGR frame
Expand All @@ -243,11 +236,9 @@ def process_frame(self, original_frame, num_strips=60, show=False):

x_points = [point[0] for point in intersections]
y_points = [point[1] for point in intersections]
vanishing_point = Lines.draw_vanishing_point(
frame, x_points, y_points, show)
vanishing_point = Lines.draw_vanishing_point(frame, x_points, y_points, show)

# Calculating angle from vanishing point to (self.WIDTH // 2, 0)
angle = Lines.calculate_angle_from_v_point(
vanishing_point, self.WIDTH, self.HEIGHT)
angle = Lines.calculate_angle_from_v_point(vanishing_point, self.WIDTH, self.HEIGHT)

return frame, angle
26 changes: 9 additions & 17 deletions algorithms/MiniContoursDownwards.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ def __init__(self, config):
self.high_green = np.array(config.high_green)

# random colors for drawing lines
# teal (for contour points that are within bounds)
self.color_1 = (255, 255, 0)
self.color_1 = (255, 255, 0) # teal (for contour points that are within bounds)
# pink (for the line of best fit through all the contour points that are within bounds)
self.color_2 = (200, 0, 255)
# red (for contour points that are out of bounds)
self.color_3 = (0, 0, 255)
# orange (for a reference line vertically down center of frame)
self.midline = (0, 129, 255)
self.color_3 = (0, 0, 255) # red (for contour points that are out of bounds)
self.midline = (0, 129, 255) # orange (for a reference line vertically down center of frame)

# parameters for calculating centroids and drawing the best fit line among them
self.num_strips = self.config.num_strips
Expand Down Expand Up @@ -68,8 +65,7 @@ def get_centroids(self, mask, num_strips, show):

centroids = []
for i, strip in enumerate(strips):
contours, hierarchy = cv2.findContours(
strip, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv2.findContours(strip, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
strip_centroids = []
for contour in contours:
M = cv2.moments(contour)
Expand All @@ -93,8 +89,7 @@ def get_best_fit_line(self, frame, show, num_strips=60, dist_type=cv2.DIST_L2, p
-line: A vector [vx, vy, x, y] representing the line of best fit through all the centroids. [vx, vy] is a vector that describes the direction of the line, where (x, y) when taken together is a point on the line
"""""

mask = cv2.inRange(cv2.cvtColor(
frame, cv2.COLOR_BGR2HSV), self.low_green, self.high_green)
mask = cv2.inRange(cv2.cvtColor(frame, cv2.COLOR_BGR2HSV), self.low_green, self.high_green)
mask = cv2.medianBlur(mask, 9)
# mask = cv2.GaussianBlur(mask, (9,9), 10)
# mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, self.morphology_kernel)
Expand All @@ -116,10 +111,8 @@ def get_best_fit_line(self, frame, show, num_strips=60, dist_type=cv2.DIST_L2, p
idx = int(x / width * split_factor)
segmented_points[idx].append([int(x), int(y)])
if show:
cv2.circle(frame, (int(centroid[0]), int(
centroid[1])), 3, self.color_1, -1)
cv2.circle(mask, (int(centroid[0]), int(
centroid[1])), 3, self.color_1, -1)
cv2.circle(frame, (int(centroid[0]), int(centroid[1])), 3, self.color_1, -1)
cv2.circle(mask, (int(centroid[0]), int(centroid[1])), 3, self.color_1, -1)
points_vector.append([int(centroid[0]), int(centroid[1])])

c_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
Expand Down Expand Up @@ -168,7 +161,7 @@ def update_lower_hsv(self, next):

def update_upper_hsv(self, next):
self.HIGH_GREEN = np.array(next)

def process_frame(self, original_frame, num_strips=60, show=False):
"""""
parameters:
Expand All @@ -191,8 +184,7 @@ def process_frame(self, original_frame, num_strips=60, show=False):

if line is not None and line[0] is not None:
angle = round(math.degrees(math.atan(-line[0] / line[1])), 2)
cv2.line(frame, (int(self.WIDTH / 2), 0),
(int(self.WIDTH / 2), int(self.HEIGHT)), self.midline, 2)
cv2.line(frame, (int(self.WIDTH / 2), 0), (int(self.WIDTH / 2), int(self.HEIGHT)), self.midline, 2)
print(angle)
return frame, angle
else:
Expand Down
35 changes: 12 additions & 23 deletions algorithms/ScanningAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def __init__(self, config):
self.right_x_bound = int(self.WIDTH * (1 - self.config.bounding_box_x))
self.upper_y_bound = int(self.HEIGHT * self.config.bounding_box_y)
self.lower_y_bound = self.HEIGHT - 1
self.mid_y = self.lower_y_bound - \
(self.lower_y_bound - self.upper_y_bound) // 2
self.mid_y = self.lower_y_bound - (self.lower_y_bound - self.upper_y_bound) // 2

self.kernel = np.ones((5, 5), np.uint8)

Expand All @@ -38,16 +37,14 @@ def __init__(self, config):
# creates lines from the top of the horizon to the bottom
for top_x in range(0, self.WIDTH, self.pixel_gap):
for bottom_x in range(0, self.WIDTH, self.pixel_gap):
line = self.create_line(
top_x, self.upper_y_bound, bottom_x, self.lower_y_bound)
line = self.create_line(top_x, self.upper_y_bound, bottom_x, self.lower_y_bound)
self.lines.append(line)

# creates lines from horizon to the right side
for top_x in range(0, self.WIDTH, self.pixel_gap):
for right_y in range(self.mid_y,
self.lower_y_bound, self.pixel_gap):
line1 = self.create_line(
top_x, self.upper_y_bound, self.WIDTH - 1, right_y)
line1 = self.create_line(top_x, self.upper_y_bound, self.WIDTH - 1, right_y)
self.lines.append(line1)

# creates lines from horizon to the left side
Expand Down Expand Up @@ -86,7 +83,7 @@ def update_lower_hsv(self, next):

def update_upper_hsv(self, next):
self.HIGH_GREEN = np.array(next)

def process_frame(self, frame, show):

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
Expand All @@ -101,8 +98,7 @@ def process_frame(self, frame, show):
return mask, None

self.upper_y_bound = white_pixels[0][0]
self.mid_y = self.lower_y_bound - \
(self.lower_y_bound - self.upper_y_bound) // 2
self.mid_y = self.lower_y_bound - (self.lower_y_bound - self.upper_y_bound) // 2

# use this to invert the mask
# mask = ~mask
Expand Down Expand Up @@ -135,8 +131,7 @@ def process_frame(self, frame, show):
most_prominent_pos_lines = pos_array[:, 1][largest_pos_indices]
most_prominent_neg_lines = neg_array[:, 1][largest_neg_indices]

most_prominent_lines = numpy.concatenate(
(most_prominent_pos_lines, most_prominent_neg_lines), axis=None)
most_prominent_lines = numpy.concatenate((most_prominent_pos_lines, most_prominent_neg_lines), axis=None)

# convert to lines as defined in Lines.py
converted_lines = []
Expand All @@ -151,33 +146,27 @@ def process_frame(self, frame, show):

x_points = [point[0] for point in intersections]
y_points = [point[1] for point in intersections]
vanishing_point = Lines.draw_vanishing_point(
frame, x_points, y_points, show)
vanishing_point = Lines.draw_vanishing_point(frame, x_points, y_points, show)

if show:
for line in converted_lines:
frame = cv2.line(
frame, (line[0], line[1]), (line[2], line[3]), (255, 255, 255), 1)
frame = cv2.line(frame, (line[0], line[1]), (line[2], line[3]), (255, 255, 255), 1)

# line for the middle of frame
frame = cv2.line(frame, (self.WIDTH // 2, self.HEIGHT),
(self.WIDTH // 2, 0), (0, 0, 255), 1)
frame = cv2.line(frame, (self.WIDTH // 2, self.HEIGHT), (self.WIDTH // 2, 0), (0, 0, 255), 1)

# point with x coordinate of the vanishing point and y coordinate of the end of the crop row
frame = cv2.circle(
frame, (vanishing_point[0], self.upper_y_bound), 5, (0, 255, 0), -1)
frame = cv2.circle(frame, (vanishing_point[0], self.upper_y_bound), 5, (0, 255, 0), -1)

# point in the middle of frame at midpoint between the horizon and bottom of the screen
frame = cv2.circle(
frame, (self.WIDTH // 2, self.mid_y), 5, (0, 255, 0), -1)
frame = cv2.circle(frame, (self.WIDTH // 2, self.mid_y), 5, (0, 255, 0), -1)

# line between the two points above
frame = cv2.line(frame, (self.WIDTH // 2, self.mid_y),
(vanishing_point[0], self.upper_y_bound), (0, 255, 0), 2)

# Calculating angle from vanishing point to (self.WIDTH // 2, 0)
angle = Lines.calculate_angle_from_v_point(
vanishing_point, self.WIDTH, self.HEIGHT)
angle = Lines.calculate_angle_from_v_point(vanishing_point, self.WIDTH, self.HEIGHT)

return frame, angle

Expand Down
Loading

0 comments on commit bc467bb

Please sign in to comment.