-
Notifications
You must be signed in to change notification settings - Fork 0
/
creating-files-data-and-name.py
104 lines (80 loc) · 2.46 KB
/
creating-files-data-and-name.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
100
101
102
103
104
"""
Course: Training YOLO v3 for Objects Detection with Custom Data
Section-3
Labelling new Dataset in YOLO format
File: creating-files-data-and-name.py
"""
# Creating files labelled_data.data and classes.names
# for training in Darknet framework
#
# Algorithm:
# Setting up full paths --> Reading file classes.txt -->
# --> Creating file classes.names -->
# --> Creating file labelled_data.data
#
# Result:
# Files classes.names and labelled_data.data needed to train
# in Darknet framework
"""
Start of:
Setting up full path to directory with labelled images
"""
# Full or absolute path to the folder with images
# Find it with Py file getting-full-path.py
# Pay attention! If you're using Windows, yours path might looks like:
# r'C:\Users\my_name\Downloads\video-to-annotate'
# or:
# 'C:\\Users\\my_name\\Downloads\\video-to-annotate'
full_path_to_images = 'annotated'
"""
End of:
Setting up full path to directory with labelled images
"""
"""
Start of:
Creating file classes.names
"""
# Defining counter for classes
c = 0
# Creating file classes.names from existing one classes.txt
# Pay attention! If you're using Windows, it might need to change
# this: + '/' +
# to this: + '\' +
# or to this: + '\\' +
with open(full_path_to_images + '/' + 'classes.names', 'w') as names, \
open(full_path_to_images + '/' + 'classes.txt', 'r') as txt:
# Going through all lines in txt file and writing them into names file
for line in txt:
names.write(line) # Copying all info from file txt to names
# Increasing counter
c += 1
"""
End of:
Creating file classes.names
"""
"""
Start of:
Creating file labelled_data.data
"""
# Creating file labelled_data.data
# Pay attention! If you're using Windows, it might need to change
# this: + '/' +
# to this: + '\' +
# or to this: + '\\' +
with open(full_path_to_images + '/' + 'labelled_data.data', 'w') as data:
# Writing needed 5 lines
# Number of classes
# By using '\n' we move to the next line
data.write('classes = ' + str(c) + '\n')
# Location of the train.txt file
data.write('train = ' + full_path_to_images + '/' + 'train.txt' + '\n')
# Location of the test.txt file
data.write('valid = ' + full_path_to_images + '/' + 'test.txt' + '\n')
# Location of the classes.names file
data.write('names = ' + full_path_to_images + '/' + 'classes.names' + '\n')
# Location where to save weights
data.write('backup = backup')
"""
End of:
Creating file labelled_data.data
"""