forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_data_stark.py
157 lines (124 loc) Β· 5.55 KB
/
scrape_data_stark.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import httpx
import json
import os
import shutil
from google.cloud import storage
from urllib.parse import urlparse
from loguru import logger
label_dictionary = {
"Table": 0,
"Table head": 1,
"Table line": 2,
"Table column": 3,
"Table footer": 4,
"Comments": 5,
"Table totals": 6,
"Delivery address": 7,
"Vendor address": 8,
"Company address": 9,
"Invoice address": 10,
"In footer comments": 11,
"Between line comments": 12,
"In line comments": 13,
"End of table comments": 14,
"Above table comments": 15,
"Below table footer comments": 16,
}
def json_to_yolo(input_json, annotation_name):
with open('preprocess_stark/labels/' + annotation_name, 'w') as f:
for annotation in input_json['result']:
try:
x_top = annotation['value']['x']
y_top = annotation['value']['y']
width = annotation['value']['width']
height = annotation['value']['height']
label = annotation['value']['rectanglelabels'][0]
yolo_x_middle = (x_top + width/2)/100
yolo_y_middle = (y_top + height/2)/100
yolo_width = width/100
yolo_height = height/100
yolo_label_num = label_dictionary[label]
final_string = f"{yolo_label_num} {yolo_x_middle} {yolo_y_middle} {yolo_width} {yolo_height}"
f.write(final_string)
f.write('\n')
except Exception as e:
pass
def delete_content_of_folder(path_to_folder: str) -> None:
for filename in os.listdir(path_to_folder):
try:
file_path = os.path.join(path_to_folder, filename)
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def decode_gcs_url(url: str):
p = urlparse(url)
path = p.path[1:].split('/', 1)
bucket, file_path = path[0], path[1]
return bucket, file_path
def download_blob(url: str) -> dict:
annotation = {}
if url:
storage_client = storage.Client()
bucket, file_path = decode_gcs_url(url)
bucket = storage_client.bucket(bucket)
blob = bucket.blob(file_path)
annotation = json.loads(blob.download_as_text())
return annotation
def download_annotated_data_from_bucket(data_paths, needs_to_be_validated):
annotations = {}
for num, url in enumerate(data_paths):
if (num + 1) % 100 == 0:
print(f'--- Already downloaded {num + 1} files out of {len(data_paths)} ---')
json_file = download_blob(url)
if json_file:
try:
task_id = json_file['task']['id']
nature_of_annotation = [obj['value']['choices'][0] for obj in json_file['result']
if obj['from_name'] == 'Validation']
# Remove annotations which were skipped over the time
if task_id in annotations.keys() and not json_file['task']['is_labeled']:
del annotations[task_id]
# and add only the ones which were annotated and wasn't deleted
elif (json_file['task']['is_labeled'] and not json_file['was_cancelled']
and 'Deleted' not in nature_of_annotation):
if needs_to_be_validated:
if 'Validated' in nature_of_annotation:
annotations[task_id] = json_file
else:
annotations[task_id] = json_file
except Exception as e:
logger.info(f'Annotation was not downloaded from gcp with an error: {e}')
return annotations
def download_label_studio_data(annotated_data_in_jsons):
# Instantiates a client
storage_client = storage.Client()
# Get GCS bucket
bucket = storage_client.get_bucket('ga_vision_images')
for num, json_annotation in enumerate(annotated_data_in_jsons.values()):
if (num + 1) % 100 == 0:
print(f'--- Already processed {num + 1} annotations out of {len(annotated_data_in_jsons.values())} ---')
image_path = json_annotation['task']['data']['image'].replace('gs://ga_vision_images/', '')
image_name = '/'.join(image_path.split('/')[1:])
annotation_name = ''.join(image_name.split('.')[:-1]) + '.txt'
json_to_yolo(json_annotation, annotation_name)
try:
blob = bucket.blob(image_path)
blob.download_to_filename('preprocess_stark/images/' + image_name)
except Exception as e:
print(f'--- Failed to download {image_name} with {e} ---')
def get_bucket_data_urls(specific_org: str = ''):
storage_client = storage.Client()
folder_in_bucket = storage_client.list_blobs('ga_vision_annotations', prefix=specific_org)
return [blob.public_url for blob in folder_in_bucket if blob.name != 'Stark/']
def get_training_data(needs_to_be_validated: bool = False, specific_org: str = '') -> None:
delete_content_of_folder('preprocess_stark/images')
delete_content_of_folder('preprocess_stark/labels')
# get label studio data
urls = get_bucket_data_urls(specific_org)
annotated_data_in_jsons = download_annotated_data_from_bucket(urls, needs_to_be_validated)
download_label_studio_data(annotated_data_in_jsons)
if __name__ == '__main__':
get_training_data(needs_to_be_validated=True, specific_org='Stark')