-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
59 lines (46 loc) · 1.63 KB
/
task2.py
File metadata and controls
59 lines (46 loc) · 1.63 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
55
56
57
58
59
import os
import csv
import cv2
import pandas as pd
from main import save_as_csv
def scan_annotation(annotation_path: str) -> list[list[str]]:
"""func that scans annotation in given path and return data in matrix
Args:
annotation_path (str): path for annotation
Returns:
list[list[str]]: data as a matrix in return
"""
with open(annotation_path, 'r', newline='') as csvfile:
filereader = csv.reader(csvfile, delimiter=';', quotechar='|')
result = []
for row in filereader:
row.pop(0)
result.append(row)
return result
def copy_dataset(dataset: list[list[str]], copy_path: str) -> list[list[str]]:
"""func that copies dataset without columns
Args:
dataset (list[list[str]]): data in matrix w/ columns
copy_path (str): where to copy the data
Returns:
list[list[str]]: new dataset
"""
# relative or absolute
if not os.path.exists(copy_path):
os.mkdir(copy_path)
result = []
for row in dataset:
img_class = row[-1]
img_name = (row[1].split('\\'))[-1]
new_img_name = f'{img_class}_{img_name}'
img = cv2.imread(row[1])
cv2.imwrite(fr'{copy_path}\{new_img_name}', img)
result.append([os.path.abspath(fr'{copy_path}\{new_img_name}'), fr'{copy_path}\{new_img_name}', img_class])
print(row)
print("Saved successfully")
return result
if __name__ == "__main__":
data = scan_annotation('annotation.csv')
columns = data.pop(0)
new_data = copy_dataset(data, 'task2_dataset')
save_as_csv(new_data, columns, 'task2_annotation.csv')