-
Notifications
You must be signed in to change notification settings - Fork 1
/
yolov5_to_supervisely.py
126 lines (103 loc) · 3.58 KB
/
yolov5_to_supervisely.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
import json
import os
import shutil
from pathlib import Path
import yolov5
from PIL import Image
from sahi.utils.file import list_files
from tqdm import tqdm
from yolov5.utils.dataloaders import IMG_FORMATS
WEIGHTS = "yolov5s.pt"
DEVICE = "cuda:0"
SOURCE_DIR = "images"
SAVE_DIR = "results/"
IMAGE_SIZE = 640
CONF_THRESHOLD = 0.2
IOU_THRESHOLD = 0.45
META_JSON_PATH = "meta.json"
def get_class_title_to_id_from_meta_json(meta_json):
class_title_to_id = {
class_["title"]: class_["id"] for class_ in meta_json["classes"]
}
return class_title_to_id
def create_dir(newpath):
if not os.path.exists(newpath):
os.makedirs(newpath)
print(f"folder created at {newpath}")
else:
print(f"{newpath} folder already exist")
def predict_and_export(
supervisely_meta_json_path,
weights="best.pt",
device="cpu",
source_dir="test_images/",
save_dir="results/",
img_size=640,
conf_thres=0.1,
iou_thres=0.45,
):
with open(supervisely_meta_json_path) as json_file:
meta_json = json.load(json_file)
meta_export_path = Path(save_dir) / "meta.json"
save_dir = Path(save_dir) / "dataset"
# Initialize directories
save_dir_img = Path(save_dir) / "img"
create_dir(save_dir_img)
save_dir_ann = Path(save_dir) / "ann"
create_dir(save_dir_ann)
class_title_to_id = get_class_title_to_id_from_meta_json(meta_json)
model = yolov5.load(model_path=weights, device=device)
model.conf = conf_thres
model.iou = iou_thres
image_files = list_files(source_dir, contains=IMG_FORMATS)
for frame_path in tqdm(image_files):
width, height = Image.open(frame_path).size
json_frame = {}
json_frame["description"] = ""
json_frame["tags"] = []
json_frame["size"] = {"height": height, "width": width}
json_frame["objects"] = []
pred = model(frame_path, augment=False, size=img_size)
for ind in range(len(pred.xyxy[0])):
xyxy = pred.xyxy[0][ind][:4]
frame_id = Path(frame_path).stem
class_title = pred.names[ind]
class_id = class_title_to_id[class_title]
json_frame["objects"].append(
{
"id": f"{frame_id}{ind}",
"classId": class_id,
"description": "",
"geometryType": "rectangle",
"labelerLogin": "[email protected]",
"createdAt": "2021-09-14T13:35:58.348Z",
"updatedAt": "2021-09-14T13:36:20.612Z",
"tags": [],
"classTitle": class_title,
"points": {
"exterior": [
[int(xyxy[0]), int(xyxy[1])],
[int(xyxy[2]), int(xyxy[3])],
],
"interior": [],
},
}
)
# save image
shutil.copyfile(frame_path, save_dir_img / Path(frame_path).name)
# save json ann
with open(f"{save_dir_ann}/{Path(frame_path).stem}.json", "w") as fp:
json.dump(json_frame, fp, indent=4)
with open(meta_export_path, "w") as fp:
json.dump(meta_json, fp, indent=4)
if __name__ == "__main__":
predict_and_export(
META_JSON_PATH,
WEIGHTS,
DEVICE,
SOURCE_DIR,
SAVE_DIR,
IMAGE_SIZE,
CONF_THRESHOLD,
IOU_THRESHOLD,
)