-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathcodes_for_lane_detection_utils.py
51 lines (46 loc) · 1.62 KB
/
codes_for_lane_detection_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import cv2
import sys
sys.path.append('../../util')
import webcamera_utils
from math_utils import softmax
def crop_and_resize(raw_img,WIDTH,HEIGHT,arch,resize):
if resize=="padding":
#add padding
frame,resized_img = webcamera_utils.adjust_frame_size(raw_img, HEIGHT, WIDTH)
return resized_img
elif resize=="crop_center" or resize=="crop_bottom":
#crop bottom
scale_x = (WIDTH / raw_img.shape[1])
crop_y = raw_img.shape[0] * scale_x - HEIGHT
crop_y = int(crop_y / scale_x)
if resize=="crop_center":
crop_y = int(crop_y/2)
img = raw_img[crop_y:, :, :] #keep aspect
if arch=="erfnet":
img = cv2.resize(img, (WIDTH, HEIGHT), interpolation = cv2.INTER_LINEAR)
elif arch=="scnn":
img = cv2.resize(img, (WIDTH, HEIGHT), interpolation = cv2.INTER_CUBIC)
return img
return None
def preprocess(img,arch):
if arch=="erfnet":
#channel first
mean = [103.939, 116.779, 123.68] #bgr
std = [1, 1, 1]
img = np.expand_dims(img, 0)
img = img - np.array(mean)[np.newaxis, np.newaxis, ...]
img = img / np.array(std)[np.newaxis, np.newaxis, ...]
img = np.array(img).transpose(0, 3, 1, 2)
elif arch=="scnn":
#channel last
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
x = img[np.newaxis, :, :, :]
img = x.astype(np.float32)
return img
def postprocess(output,arch):
if arch=="erfnet":
output = softmax(output, axis=1)
elif arch=="scnn":
output = output.transpose((0, 3, 1, 2))
return output