Update kvasir-points_datasets_script.py
Browse files- kvasir-points_datasets_script.py +40 -17
kvasir-points_datasets_script.py
CHANGED
|
@@ -15,6 +15,7 @@
|
|
| 15 |
"""Dataset for filtered Kvasir-instrument and Hyper-Kvasir with bounding boxes."""
|
| 16 |
|
| 17 |
import os
|
|
|
|
| 18 |
import json
|
| 19 |
from PIL import Image
|
| 20 |
import datasets
|
|
@@ -22,17 +23,14 @@ import datasets
|
|
| 22 |
import os
|
| 23 |
import json
|
| 24 |
import pandas as pd
|
| 25 |
-
import hashlib
|
| 26 |
from collections import defaultdict
|
| 27 |
import numpy as np
|
| 28 |
|
| 29 |
|
| 30 |
-
def cal_mid(bx): return [[[float(box['xmin'] + box['xmax']) / 2,
|
| 31 |
-
float(box['ymin'] + box['ymax']) / 2] for box in bx]]
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"y": float(box['ymin'] + box['ymax']) / 2} for box in bx]
|
| 36 |
|
| 37 |
|
| 38 |
def cal_sha256(file_path): return hashlib.sha256(
|
|
@@ -51,6 +49,23 @@ def convert_to_json_format(file_path, image_width, image_height):
|
|
| 51 |
}
|
| 52 |
for line in file.readlines()
|
| 53 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
class_map = {"0": "normal", "1": "cluster", "2": "pinhead"}
|
|
@@ -111,7 +126,9 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
|
|
| 111 |
features = datasets.Features({
|
| 112 |
"image_data": datasets.Image(),
|
| 113 |
"image_sha256": datasets.Value("string"),
|
| 114 |
-
"
|
|
|
|
|
|
|
| 115 |
"count": datasets.Value("int64"),
|
| 116 |
"label": datasets.Value("string"),
|
| 117 |
"collection_method": datasets.Value("string"),
|
|
@@ -138,11 +155,14 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
|
|
| 138 |
def _generate_examples(self):
|
| 139 |
for key, entry in hyper_seg_imgs.items():
|
| 140 |
img_path = os.path.join(hyper_seg_img_base_path, f"{key}.jpg")
|
|
|
|
| 141 |
hyper_entry = hyper_df.loc[hyper_df['Video file'] == key].iloc[0]
|
| 142 |
yield key, {
|
| 143 |
-
"image_data":
|
| 144 |
"image_sha256": cal_sha256(img_path),
|
| 145 |
-
"
|
|
|
|
|
|
|
| 146 |
"count": len(entry['bbox']),
|
| 147 |
"label": hyper_entry.Finding,
|
| 148 |
"collection_method": 'counting',
|
|
@@ -152,10 +172,13 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
|
|
| 152 |
|
| 153 |
for key, entry in instr_seg_imgs.items():
|
| 154 |
img_path = os.path.join(instr_seg_img_base_path, f"{key}.jpg")
|
|
|
|
| 155 |
yield key, {
|
| 156 |
-
"image_data":
|
| 157 |
"image_sha256": cal_sha256(img_path),
|
| 158 |
-
"
|
|
|
|
|
|
|
| 159 |
"count": len(entry['bbox']),
|
| 160 |
"label": "instrument",
|
| 161 |
"collection_method": "counting",
|
|
@@ -166,24 +189,24 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
|
|
| 166 |
for folder in os.listdir(visem_root):
|
| 167 |
folder_path = os.path.join(visem_root, folder)
|
| 168 |
labels_all = os.listdir(folder_path+"/labels")
|
| 169 |
-
images = os.listdir(folder_path+"/images")
|
| 170 |
-
height, width = Image.open(os.path.join(
|
| 171 |
-
folder_path, "images", images[0])).size
|
| 172 |
labels = [labels_all[i] for i in np.linspace(
|
| 173 |
0, len(labels_all)-1, 250).astype(int)]
|
| 174 |
for label in labels:
|
| 175 |
label_path = os.path.join(folder_path, "labels", label)
|
| 176 |
image_path = label_path.replace(
|
| 177 |
"/labels/", "/images/").replace(".txt", ".jpg")
|
|
|
|
| 178 |
entry_bbox = convert_to_json_format(label_path, width, height)
|
| 179 |
label_dict = defaultdict(list)
|
| 180 |
for entry in entry_bbox:
|
| 181 |
label_dict[entry['label']].append(entry)
|
| 182 |
for label in label_dict:
|
| 183 |
yield cal_sha256(image_path)+label, {
|
| 184 |
-
"image_data":
|
| 185 |
"image_sha256": cal_sha256(image_path),
|
| 186 |
-
"
|
|
|
|
|
|
|
| 187 |
"count": len(label_dict[label]),
|
| 188 |
"label": class_map[label],
|
| 189 |
"collection_method": "counting",
|
|
@@ -193,5 +216,5 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
|
|
| 193 |
|
| 194 |
|
| 195 |
# rm -rf /home/sushant/.cache/huggingface/modules/datasets_modules/datasets/kvasir-points_datasets_script/ /home/sushant/.cache/huggingface/datasets/kvasir-points_datasets_script
|
| 196 |
-
# datasets-cli test /global/D1/projects/HOST/Datasets/hyper-kvasir/sushant-experiments/kvasir-points_datasets_script.py --save_info --all_configs --
|
| 197 |
# huggingface-cli upload kvasir-points . . --repo-type dataset
|
|
|
|
| 15 |
"""Dataset for filtered Kvasir-instrument and Hyper-Kvasir with bounding boxes."""
|
| 16 |
|
| 17 |
import os
|
| 18 |
+
import io
|
| 19 |
import json
|
| 20 |
from PIL import Image
|
| 21 |
import datasets
|
|
|
|
| 23 |
import os
|
| 24 |
import json
|
| 25 |
import pandas as pd
|
| 26 |
+
import hashlib
|
| 27 |
from collections import defaultdict
|
| 28 |
import numpy as np
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# def cal_mid_xy(bx): return [{"x": float(box['xmin'] + box['xmax']) / 2,
|
| 33 |
+
# "y": float(box['ymin'] + box['ymax']) / 2} for box in bx]
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def cal_sha256(file_path): return hashlib.sha256(
|
|
|
|
| 49 |
}
|
| 50 |
for line in file.readlines()
|
| 51 |
]
|
| 52 |
+
|
| 53 |
+
def get_image_bytes(img_path, max_width=700):
|
| 54 |
+
img = Image.open(img_path)
|
| 55 |
+
if img.width <= max_width:
|
| 56 |
+
return open(img_path, "rb").read(), img.width, img.height, 1.0
|
| 57 |
+
with io.BytesIO() as b:
|
| 58 |
+
img.resize((max_width, int(img.height * max_width / img.width))).save(b, "PNG")
|
| 59 |
+
return b.getvalue(), max_width, int(img.height * max_width / img.width), float(max_width) / img.width
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def get_bboxes(bx, ratio):
|
| 63 |
+
return [[box[k] * ratio for k in ('xmin', 'ymin', 'xmax', 'ymax')] for box in bx]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def cal_mid(bx, ratio):
|
| 67 |
+
return [[((box['xmin'] + box['xmax']) / 2) * ratio,
|
| 68 |
+
((box['ymin'] + box['ymax']) / 2) * ratio] for box in bx]
|
| 69 |
|
| 70 |
|
| 71 |
class_map = {"0": "normal", "1": "cluster", "2": "pinhead"}
|
|
|
|
| 126 |
features = datasets.Features({
|
| 127 |
"image_data": datasets.Image(),
|
| 128 |
"image_sha256": datasets.Value("string"),
|
| 129 |
+
"img_size": datasets.Sequence(datasets.Value("float32")),
|
| 130 |
+
"points": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
|
| 131 |
+
"bbox": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
|
| 132 |
"count": datasets.Value("int64"),
|
| 133 |
"label": datasets.Value("string"),
|
| 134 |
"collection_method": datasets.Value("string"),
|
|
|
|
| 155 |
def _generate_examples(self):
|
| 156 |
for key, entry in hyper_seg_imgs.items():
|
| 157 |
img_path = os.path.join(hyper_seg_img_base_path, f"{key}.jpg")
|
| 158 |
+
img, width, height, ratio = get_image_bytes(img_path)
|
| 159 |
hyper_entry = hyper_df.loc[hyper_df['Video file'] == key].iloc[0]
|
| 160 |
yield key, {
|
| 161 |
+
"image_data": img,
|
| 162 |
"image_sha256": cal_sha256(img_path),
|
| 163 |
+
"img_size": [width, height],
|
| 164 |
+
"points": cal_mid(entry['bbox'], ratio),
|
| 165 |
+
"bbox": get_bboxes(entry['bbox'], ratio),
|
| 166 |
"count": len(entry['bbox']),
|
| 167 |
"label": hyper_entry.Finding,
|
| 168 |
"collection_method": 'counting',
|
|
|
|
| 172 |
|
| 173 |
for key, entry in instr_seg_imgs.items():
|
| 174 |
img_path = os.path.join(instr_seg_img_base_path, f"{key}.jpg")
|
| 175 |
+
img, width, height, ratio = get_image_bytes(img_path)
|
| 176 |
yield key, {
|
| 177 |
+
"image_data": img,
|
| 178 |
"image_sha256": cal_sha256(img_path),
|
| 179 |
+
"img_size": [width, height],
|
| 180 |
+
"points": cal_mid(entry['bbox'], ratio),
|
| 181 |
+
"bbox": get_bboxes(entry['bbox'], ratio),
|
| 182 |
"count": len(entry['bbox']),
|
| 183 |
"label": "instrument",
|
| 184 |
"collection_method": "counting",
|
|
|
|
| 189 |
for folder in os.listdir(visem_root):
|
| 190 |
folder_path = os.path.join(visem_root, folder)
|
| 191 |
labels_all = os.listdir(folder_path+"/labels")
|
|
|
|
|
|
|
|
|
|
| 192 |
labels = [labels_all[i] for i in np.linspace(
|
| 193 |
0, len(labels_all)-1, 250).astype(int)]
|
| 194 |
for label in labels:
|
| 195 |
label_path = os.path.join(folder_path, "labels", label)
|
| 196 |
image_path = label_path.replace(
|
| 197 |
"/labels/", "/images/").replace(".txt", ".jpg")
|
| 198 |
+
img, width, height, ratio = get_image_bytes(image_path)
|
| 199 |
entry_bbox = convert_to_json_format(label_path, width, height)
|
| 200 |
label_dict = defaultdict(list)
|
| 201 |
for entry in entry_bbox:
|
| 202 |
label_dict[entry['label']].append(entry)
|
| 203 |
for label in label_dict:
|
| 204 |
yield cal_sha256(image_path)+label, {
|
| 205 |
+
"image_data": img,
|
| 206 |
"image_sha256": cal_sha256(image_path),
|
| 207 |
+
"img_size": [width, height],
|
| 208 |
+
"points": cal_mid(label_dict[label], ratio),
|
| 209 |
+
"bbox": get_bboxes(label_dict[label], ratio),
|
| 210 |
"count": len(label_dict[label]),
|
| 211 |
"label": class_map[label],
|
| 212 |
"collection_method": "counting",
|
|
|
|
| 216 |
|
| 217 |
|
| 218 |
# rm -rf /home/sushant/.cache/huggingface/modules/datasets_modules/datasets/kvasir-points_datasets_script/ /home/sushant/.cache/huggingface/datasets/kvasir-points_datasets_script
|
| 219 |
+
# datasets-cli test /global/D1/projects/HOST/Datasets/hyper-kvasir/sushant-experiments/kvasir-points_datasets_script.py --save_info --all_configs --trust_remote_code
|
| 220 |
# huggingface-cli upload kvasir-points . . --repo-type dataset
|