Views
No views yet
[{
"images": image_path1,
"objects": {
"ref": [class_label1, class_label2, ...],
"bbox": [bbox1, bbox2, ...]
}
},
...
]1import json
2from data_juicer.core.data import NestedDataset as Dataset
3from data_juicer.ops.mapper.image_detection_yolo_mapper import ImageDetectionYoloMapper
4from data_juicer.utils.constant import Fields, MetaKeys
5
6if __name__ == "__main__":
7
8 image_path1 = "test1.jpg"
9 image_path2 = "test2.jpg"
10 image_path3 = "test3.jpg"
11
12 source_list = [{
13 'images': [image_path1, image_path2, image_path3]
14 }]
15
16 class_names =['水稻白叶枯病Bacterial_Leaf_Blight', '水稻胡麻斑病Brown_Spot', '健康水稻HealthyLeaf', '稻瘟病Leaf_Blast', '水稻叶鞘腐病Leaf_Scald', '水稻窄褐斑病Narrow_Brown_Leaf_Spot', '水稻穗颈瘟Neck_Blast', '稻飞虱Rice_Hispa']
17
18 op = ImageDetectionYoloMapper(
19 imgsz=640, conf=0.05, iou=0.5, model_path='Path_to_YOLO11L-Rice-Disease-Detection.pt')
20
21
22 dataset = Dataset.from_list(source_list)
23 if Fields.meta not in dataset.features:
24 dataset = dataset.add_column(name=Fields.meta,
25 column=[{}] * dataset.num_rows)
26 dataset = dataset.map(op.process, num_proc=1, with_rank=True)
27 res_list = dataset.to_list()[0]
28
29 new_data = []
30 for temp_image_name, temp_bbox_lists, class_name_lists in zip(res_list["images"], res_list["__dj__meta__"]["__dj__bbox__"], res_list["__dj__meta__"]["__dj__class_label__"]):
31 temp_json = {}
32 temp_json["images"] = temp_image_name
33 temp_json["objects"] = {"ref": [], "bbox":temp_bbox_lists}
34
35 for temp_object_label in class_name_lists:
36 temp_json["objects"]["ref"].append(class_names[int(temp_object_label)])
37
38 new_data.append(temp_json)
39
40 with open("./output.json", "w") as f:
41 json.dump(new_data, f)