-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
99 lines (60 loc) · 2.56 KB
/
split.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
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 5 16:17:07 2019
@author: Amir.Khan
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
from utils.image import read_image, draw_bbox
from utils.data import get_relevant_frames, get_boxes
labels = pd.read_csv("pool_labels.csv")
#labels = pd.read_csv("car_labels.csv")
paths = labels[["Frame", "Mask"]].as_matrix()
paths = paths[:, 0] + "!" + paths[:, 1]
paths = np.unique(paths)
def temp(img_path: str, mask_path: str) -> None:
"""Open IMAGE and MASK and plot"""
img = read_image(img_path)
mask = read_image(mask_path, gray=True)
result = cv2.bitwise_and(img, img, mask=mask)
plt.imshow(result)
# Select 10 random examples
idx_list = np.random.randint(low = 0, high = paths.shape[0], size=5 * 2)
plt.figure(1, figsize=(50, 50))
for i, idx in enumerate(idx_list):
plt.subplot(5, 2, i + 1)
temp(*paths[idx].split("!"))
def plot_bbox(image_path: str, df: pd.DataFrame) -> None:
image = read_image(image_path)
df = get_relevant_frames(image_path, df)
boxes = get_boxes(df)
for box in boxes:
image = draw_bbox(image, box.left_top, box.right_bot, color=(0, 255, 0), thickness=3)
plt.imshow(image)
# Select 10 random examples
idx_list = np.random.randint(low = 0, high = paths.shape[0], size=5 * 2)
plt.figure(1, figsize=(50, 50))
for i, idx in enumerate(idx_list):
plt.subplot(5, 2, i + 1)
plot_bbox(paths[idx].split("!")[0], labels)
# Extract
image_paths = labels["Frame"].unique()
np.random.shuffle(image_paths)
split_idx = int(image_paths.shape[0] * 0.80)
print("train index 0 ~ {} (size: {})".format(split_idx - 1, split_idx))
print("test index {} ~ {} (size: {})".format(split_idx,
image_paths.shape[0] - 1, image_paths.shape[0] - split_idx))
train_paths = image_paths[:split_idx]
test_paths = image_paths[split_idx:]
train_paths.shape, test_paths.shape
train_csv = labels[labels['Frame'].isin(train_paths)].reset_index(drop=True)
test_csv = labels[labels['Frame'].isin(test_paths)].reset_index(drop=True)
# only save these two columns
columns = ["Frame", "Mask"]
train_csv[columns].to_csv("pool_train.csv", index=False, header=False)
test_csv[columns].to_csv("pool_test.csv", index=False, header=False)
#train_csv[columns].to_csv("car_train.csv", index=False, header=False)
#test_csv[columns].to_csv("car_test.csv", index=False, header=False)