Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

data.py 2.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. import os
  2. import yaml
  3. import json
  4. from .yolo_converter import YOLOAnnotationConverter
  5. class DataFunctions():
  6. def __init__(self, dataset_dir, classes = [], to_name='image', from_name='label', label_type='bbox'):
  7. self.conv = YOLOAnnotationConverter(
  8. dataset_dir=dataset_dir,
  9. classes=classes,
  10. to_name=to_name,
  11. from_name=from_name,
  12. label_type=label_type)
  13. def create_yolo_v8_dataset_yaml(self, dataset):
  14. path = str(dataset.all().download_files(target_dir="."))
  15. for dp in dataset.all().get_blob_fields("annotation"):
  16. self.conv.from_de(dp)
  17. train = "data/images/train"
  18. val = "data/images/val"
  19. yaml_dict = {
  20. 'path': os.getcwd(),
  21. 'train': train,
  22. 'val': val,
  23. 'names': {i: name for i, name in enumerate(self.conv.classes)}
  24. }
  25. with open("custom_coco.yaml", "w") as file:
  26. file.write(yaml.dump(yaml_dict))
  27. def create_categories_COCO(self, annotations):
  28. categories = set()
  29. json_annotation = json.loads(annotations.decode())
  30. if 'annotations' in json_annotation:
  31. for annotation in json_annotation["annotations"]:
  32. for result in annotation['result']:
  33. categories.add(result['value'][result['type']][0])
  34. return ', '.join(str(item) for item in categories)
  35. def create_metadata(self, s):
  36. if s['path'].startswith(('val', 'test', 'train')):
  37. s["valid_datapoint"] = True
  38. s['year'] = 2017
  39. path = s['path'].split('/')
  40. s["split"] = path[0]
  41. # Add annotations where relevant
  42. if not s['path'].startswith('test'):
  43. if not ('annotation' in s and s['annotation']):
  44. s['annotation'] = self.conv.to_de(s)
  45. s['categories'] = self.create_categories_COCO(s["annotation"])
  46. else:
  47. print("Data paths must start with 'val'/'test'/'train'")
  48. return s
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...