forked from sbft-cps-tool-competition/cps-tool-competition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_processing.py
40 lines (29 loc) · 1.03 KB
/
image_processing.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
import cv2
import numpy as np
IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS = 160, 320, 3
INPUT_SHAPE = (IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS)
def crop(image: np.ndarray) -> np.ndarray:
"""
Crop the image (removing the sky at the top and the car front at the bottom)
"""
return image[80:-30, :, :] # remove the sky and the car front
def resize(image: np.ndarray) -> np.ndarray:
"""
Resize the image to the input shape used by the network model
"""
return cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT), cv2.INTER_AREA)
def rgb2yuv(image: np.ndarray) -> np.ndarray:
"""
Convert the image from RGB to YUV (This is what the NVIDIA model does)
"""
return cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
def preprocess(image: np.ndarray, normalize: bool = False) -> np.ndarray:
"""
Combine all preprocess functions into one
"""
image = crop(image=image)
image = resize(image=image)
image = rgb2yuv(image=image)
if normalize:
image = (image / 127.5) - 1.0
return image