-
Notifications
You must be signed in to change notification settings - Fork 21
/
preprocessing.py
88 lines (61 loc) · 2.04 KB
/
preprocessing.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import pandas as pd
import numpy as np
import os
from sklearn.preprocessing import LabelEncoder
def load_image_predict(image_path, image_h, image_w):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (image_h, image_w))
image = image/255
image = np.expand_dims(image, 0)
return image
def load_carla_data(path, labels):
le = LabelEncoder()
le.fit_transform(labels)
data = pd.read_csv(path, delimiter=",", header=None)
dataset = {}
objects_omitted = 0
red = 0
green = 0
for record in data[1:][data.columns[:7]].values:
tokens = record[5].split(",")
xmin, ymin, xmax, ymax = float(tokens[1].split(":")[1]), float(tokens[2].split(":")[1]),\
float(tokens[3].split(":")[1]), float(tokens[4].split(":")[1].replace("}", ""))
#omit small images
if xmax < 15:
objects_omitted += 1
continue
xmax += xmin
ymax += ymin
if "stop" in record[6]:
obj_class = "stop"
red += 1
else:
obj_class = "go"
green += 1
obj = {}
obj['xmin'], obj['ymin'], obj['xmax'], obj['ymax'], obj['class'] = xmin, ymin, xmax, ymax, obj_class
image_path = record[0]
#image_path = os.path.join("images", image_path)
if image_path in dataset:
dataset[image_path].append(obj)
else:
dataset[image_path] = [obj]
print("Objects omitted", objects_omitted)
print("Red light: ", red)
print("Green light: ", green)
instances = []
for key in dataset.keys():
inst = {}
inst['image_path'] = key
inst['object'] = dataset[key]
instances.append(inst)
return instances
def load_image(path):
img = cv2.imread(os.path.join(path))
if len(img.shape) == 3:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if len(img.shape) == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
return img