|
|
import torch |
|
|
from lib.utils import is_parallel |
|
|
import numpy as np |
|
|
np.set_printoptions(threshold=np.inf) |
|
|
import cv2 |
|
|
from sklearn.cluster import DBSCAN |
|
|
|
|
|
|
|
|
def build_targets(cfg, predictions, targets, model): |
|
|
''' |
|
|
predictions |
|
|
[16, 3, 32, 32, 85] |
|
|
[16, 3, 16, 16, 85] |
|
|
[16, 3, 8, 8, 85] |
|
|
torch.tensor(predictions[i].shape)[[3, 2, 3, 2]] |
|
|
[32,32,32,32] |
|
|
[16,16,16,16] |
|
|
[8,8,8,8] |
|
|
targets[3,x,7] |
|
|
t [index, class, x, y, w, h, head_index] |
|
|
''' |
|
|
|
|
|
det = model.module.model[model.module.detector_index] if is_parallel(model) \ |
|
|
else model.model[model.detector_index] |
|
|
|
|
|
|
|
|
|
|
|
na, nt = det.na, targets.shape[0] |
|
|
tcls, tbox, indices, anch = [], [], [], [] |
|
|
gain = torch.ones(7, device=targets.device) |
|
|
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) |
|
|
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) |
|
|
|
|
|
g = 0.5 |
|
|
off = torch.tensor([[0, 0], |
|
|
[1, 0], [0, 1], [-1, 0], [0, -1], |
|
|
|
|
|
], device=targets.device).float() * g |
|
|
|
|
|
for i in range(det.nl): |
|
|
anchors = det.anchors[i] |
|
|
gain[2:6] = torch.tensor(predictions[i].shape)[[3, 2, 3, 2]] |
|
|
|
|
|
t = targets * gain |
|
|
|
|
|
if nt: |
|
|
|
|
|
r = t[:, :, 4:6] / anchors[:, None] |
|
|
j = torch.max(r, 1. / r).max(2)[0] < cfg.TRAIN.ANCHOR_THRESHOLD |
|
|
|
|
|
t = t[j] |
|
|
|
|
|
|
|
|
gxy = t[:, 2:4] |
|
|
gxi = gain[[2, 3]] - gxy |
|
|
j, k = ((gxy % 1. < g) & (gxy > 1.)).T |
|
|
l, m = ((gxi % 1. < g) & (gxi > 1.)).T |
|
|
j = torch.stack((torch.ones_like(j), j, k, l, m)) |
|
|
t = t.repeat((5, 1, 1))[j] |
|
|
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] |
|
|
else: |
|
|
t = targets[0] |
|
|
offsets = 0 |
|
|
|
|
|
|
|
|
b, c = t[:, :2].long().T |
|
|
gxy = t[:, 2:4] |
|
|
gwh = t[:, 4:6] |
|
|
gij = (gxy - offsets).long() |
|
|
gi, gj = gij.T |
|
|
|
|
|
|
|
|
a = t[:, 6].long() |
|
|
indices.append((b, a, gj.clamp_(0, int(gain[3]) - 1), gi.clamp_(0, int(gain[2]) - 1))) |
|
|
tbox.append(torch.cat((gxy - gij, gwh), 1)) |
|
|
anch.append(anchors[a]) |
|
|
tcls.append(c) |
|
|
|
|
|
return tcls, tbox, indices, anch |
|
|
|
|
|
def morphological_process(image, kernel_size=5, func_type=cv2.MORPH_CLOSE): |
|
|
""" |
|
|
morphological process to fill the hole in the binary segmentation result |
|
|
:param image: |
|
|
:param kernel_size: |
|
|
:return: |
|
|
""" |
|
|
if len(image.shape) == 3: |
|
|
raise ValueError('Binary segmentation result image should be a single channel image') |
|
|
|
|
|
if image.dtype is not np.uint8: |
|
|
image = np.array(image, np.uint8) |
|
|
|
|
|
kernel = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(kernel_size, kernel_size)) |
|
|
|
|
|
|
|
|
closing = cv2.morphologyEx(image, func_type, kernel, iterations=1) |
|
|
|
|
|
return closing |
|
|
|
|
|
def connect_components_analysis(image): |
|
|
""" |
|
|
connect components analysis to remove the small components |
|
|
:param image: |
|
|
:return: |
|
|
""" |
|
|
if len(image.shape) == 3: |
|
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
|
else: |
|
|
gray_image = image |
|
|
|
|
|
return cv2.connectedComponentsWithStats(gray_image, connectivity=8, ltype=cv2.CV_32S) |
|
|
|
|
|
def if_y(samples_x): |
|
|
for sample_x in samples_x: |
|
|
if len(sample_x): |
|
|
|
|
|
if sample_x[-1] == sample_x[0]: |
|
|
return False |
|
|
return True |
|
|
|
|
|
def fitlane(mask, sel_labels, labels, stats): |
|
|
H, W = mask.shape |
|
|
for label_group in sel_labels: |
|
|
states = [stats[k] for k in label_group] |
|
|
x, y, w, h, _ = states[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t = label_group[0] |
|
|
|
|
|
|
|
|
samples_y = np.linspace(y, y+h-1, 30) |
|
|
|
|
|
samples_x = [np.where(labels[int(sample_y)]==t)[0] for sample_y in samples_y] |
|
|
|
|
|
if if_y(samples_x): |
|
|
samples_x = [int(np.mean(sample_x)) if len(sample_x) else -1 for sample_x in samples_x] |
|
|
samples_x = np.array(samples_x) |
|
|
samples_y = np.array(samples_y) |
|
|
samples_y = samples_y[samples_x != -1] |
|
|
samples_x = samples_x[samples_x != -1] |
|
|
func = np.polyfit(samples_y, samples_x, 2) |
|
|
x_limits = np.polyval(func, H-1) |
|
|
|
|
|
if x_limits < 0 or x_limits > W: |
|
|
|
|
|
|
|
|
draw_y = np.linspace(y, y+h-1, h) |
|
|
else: |
|
|
|
|
|
draw_y = np.linspace(y, H-1, H-y) |
|
|
draw_x = np.polyval(func, draw_y) |
|
|
|
|
|
|
|
|
draw_points = (np.asarray([draw_x, draw_y]).T).astype(np.int32) |
|
|
cv2.polylines(mask, [draw_points], False, 1, thickness=15) |
|
|
else: |
|
|
|
|
|
samples_x = np.linspace(x, W-1, 30) |
|
|
|
|
|
|
|
|
samples_y = [np.where(labels[:, int(sample_x)]==t)[0] for sample_x in samples_x] |
|
|
samples_y = [int(np.mean(sample_y)) if len(sample_y) else -1 for sample_y in samples_y] |
|
|
samples_x = np.array(samples_x) |
|
|
samples_y = np.array(samples_y) |
|
|
samples_x = samples_x[samples_y != -1] |
|
|
samples_y = samples_y[samples_y != -1] |
|
|
try: |
|
|
func = np.polyfit(samples_x, samples_y, 2) |
|
|
except: |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
y_limits = np.polyval(func, 0) |
|
|
if y_limits >= H or y_limits < 0: |
|
|
draw_x = np.linspace(x, x+w-1, w+x-x) |
|
|
else: |
|
|
y_limits = np.polyval(func, W-1) |
|
|
if y_limits >= H or y_limits < 0: |
|
|
draw_x = np.linspace(x, x+w-1, w+x-x) |
|
|
|
|
|
|
|
|
else: |
|
|
draw_x = np.linspace(x, W-1, W-x) |
|
|
draw_y = np.polyval(func, draw_x) |
|
|
draw_points = (np.asarray([draw_x, draw_y]).T).astype(np.int32) |
|
|
cv2.polylines(mask, [draw_points], False, 1, thickness=15) |
|
|
return mask |
|
|
|
|
|
def connect_lane(image, shadow_height=0): |
|
|
if len(image.shape) == 3: |
|
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
|
else: |
|
|
gray_image = image |
|
|
if shadow_height: |
|
|
image[:shadow_height] = 0 |
|
|
mask = np.zeros((image.shape[0], image.shape[1]), np.uint8) |
|
|
|
|
|
num_labels, labels, stats, centers = cv2.connectedComponentsWithStats(gray_image, connectivity=8, ltype=cv2.CV_32S) |
|
|
|
|
|
selected_label = [] |
|
|
|
|
|
for t in range(1, num_labels, 1): |
|
|
_, _, _, _, area = stats[t] |
|
|
if area > 400: |
|
|
selected_label.append(t) |
|
|
if len(selected_label) == 0: |
|
|
return mask |
|
|
else: |
|
|
split_labels = [[label,] for label in selected_label] |
|
|
mask_post = fitlane(mask, split_labels, labels, stats) |
|
|
return mask_post |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|