-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
54 lines (47 loc) · 1.32 KB
/
preprocessing.py
File metadata and controls
54 lines (47 loc) · 1.32 KB
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
52
53
54
import numpy as np
import random
import cv2
def grayscale(image):
"""
Grayscaling the image
"""
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
def resize(image):
"""
Resizing the image to 200,66
"""
return cv2.resize(image, (200, 66), interpolation=cv2.INTER_AREA)
def crop_image(image):
"""
Cropping the image
Cut off 43 pixels from the top and -24 from the bottom
"""
return image[43:-24,:]
def brightness(image):
"""
Returns an image with a random degree of brightness.
"""
image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
brightness = .25 + np.random.uniform()
image[:,:,2] = image[:,:,2] * brightness
image = cv2.cvtColor(image, cv2.COLOR_HSV2RGB)
return image
def preprocess(image):
"""
Returns an image after applying several preprocessing functions.
:param image: Image represented as a numpy array.
"""
image= grayscale(image)
image = brightness(image)
image = crop_image(image)
image = resize(image)
return np.array(image, dtype=np.float32)
def flipImg(image, angle):
"""
Returns an image after flipping it in 50% of the cases
:param image: Image represented as a numpy array.
"""
if random.randrange(2) == 1:
image = cv2.flip(image, 1)
angle = -angle
return image,angle