-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.py
More file actions
48 lines (36 loc) · 1.31 KB
/
task3.py
File metadata and controls
48 lines (36 loc) · 1.31 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
import os
import random
import cv2
import pandas as pd
from task2 import scan_annotation
from main import save_as_csv
def randomized_dataset_copy(dataset: list[list[str]], copy_path: str) -> list[list[str]]:
"""func that copies the dataset of pics with renaming them with random numbers
Args:
dataset (list[list[str]]): matrix of data without columns
copy_path (str): path to copy
Returns:
list[list[str]]: new dataset
"""
# relative or absolute
if not os.path.exists(copy_path):
os.mkdir(copy_path)
img_names = set()
result = []
for row in dataset:
img_class = row[-1]
img_name = str(random.randint(0, 10000)) + '.jpg'
while img_name in img_names:
img_name = str(random.randint(0, 10000)) + '.jpg'
img_names.add(img_name)
img = cv2.imread(row[1])
cv2.imwrite(fr'{copy_path}\{img_name}', img)
print(row)
print("Saved successfully")
result.append([os.path.abspath(fr'{copy_path}\{img_name}'), fr'{copy_path}\{img_name}', img_class])
return result
if __name__ == "__main__":
data = scan_annotation('annotation.csv')
columns = data.pop(0)
new_data = randomized_dataset_copy(data, 'task3_dataset')
save_as_csv(new_data, columns, 'task3_annotation.csv')