SushantGautam commited on
Commit
c58c4fe
·
verified ·
1 Parent(s): eeb748f

Update kvasir-points_datasets_script.py

Browse files
Files changed (1) hide show
  1. kvasir-points_datasets_script.py +58 -4
kvasir-points_datasets_script.py CHANGED
@@ -23,15 +23,38 @@ import os
23
  import json
24
  import pandas as pd
25
  import hashlib
 
 
26
 
27
 
28
- cal_mid = lambda bx: [[[float(box['xmin'] + box['xmax']) / 2, float(box['ymin'] + box['ymax']) / 2] for box in bx]]
 
 
 
 
 
29
 
30
 
31
  def cal_sha256(file_path): return hashlib.sha256(
32
  open(file_path, 'rb').read()).hexdigest()
33
 
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  hyper_label_img_path = '/global/D1/projects/HOST/Datasets/hyper-kvasir/labeled-images/image-labels.csv'
36
 
37
  hyper_df = pd.read_csv(hyper_label_img_path)
@@ -45,6 +68,8 @@ instr_seg_img_base_path = '/global/D1/projects/HOST/Datasets/kvasir-instrument/i
45
  hyper_seg_imgs = json.load(open(hyper_seg_img_path))
46
  instr_seg_imgs = json.load(open(instr_seg_img_path))
47
 
 
 
48
  _CITATION = """\
49
  @article{kvasir,
50
  title={Kvasir-instrument and Hyper-Kvasir datasets for bounding box annotations},
@@ -122,12 +147,11 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
122
  "label": hyper_entry.Finding,
123
  "collection_method": 'counting',
124
  "classification": hyper_entry.Classification,
125
- "organ": hyper_entry.Organ
126
  }
127
 
128
  for key, entry in instr_seg_imgs.items():
129
  img_path = os.path.join(instr_seg_img_base_path, f"{key}.jpg")
130
- assert len(cal_mid(entry['bbox'])) > 0
131
  yield key, {
132
  "image_data": open(img_path, 'rb').read(),
133
  "image_sha256": cal_sha256(img_path),
@@ -139,5 +163,35 @@ class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
139
  "organ": "instrument"
140
  }
141
 
142
- #datasets-cli test /global/D1/projects/HOST/Datasets/hyper-kvasir/sushant-experiments/kvasir-points_datasets_script.py --save_info --all_configs --trust_remote_code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  # huggingface-cli upload kvasir-points . . --repo-type dataset
 
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
+ def cal_mid_xy(bx): return [{"x": float(box['xmin'] + box['xmax']) / 2,
35
+ "y": float(box['ymin'] + box['ymax']) / 2} for box in bx]
36
 
37
 
38
  def cal_sha256(file_path): return hashlib.sha256(
39
  open(file_path, 'rb').read()).hexdigest()
40
 
41
 
42
+ def convert_to_json_format(file_path, image_width, image_height):
43
+ with open(file_path, 'r') as file:
44
+ return [
45
+ {
46
+ "label": line.split()[0],
47
+ "xmin": int((float(line.split()[1]) - float(line.split()[3]) / 2) * image_width),
48
+ "ymin": int((float(line.split()[2]) - float(line.split()[4]) / 2) * image_height),
49
+ "xmax": int((float(line.split()[1]) + float(line.split()[3]) / 2) * image_width),
50
+ "ymax": int((float(line.split()[2]) + float(line.split()[4]) / 2) * image_height),
51
+ }
52
+ for line in file.readlines()
53
+ ]
54
+
55
+
56
+ class_map = {"0": "normal", "1": "cluster", "2": "pinhead"}
57
+
58
  hyper_label_img_path = '/global/D1/projects/HOST/Datasets/hyper-kvasir/labeled-images/image-labels.csv'
59
 
60
  hyper_df = pd.read_csv(hyper_label_img_path)
 
68
  hyper_seg_imgs = json.load(open(hyper_seg_img_path))
69
  instr_seg_imgs = json.load(open(instr_seg_img_path))
70
 
71
+ visem_root = "/global/D1/projects/HOST/Datasets/visem-tracking"
72
+
73
  _CITATION = """\
74
  @article{kvasir,
75
  title={Kvasir-instrument and Hyper-Kvasir datasets for bounding box annotations},
 
147
  "label": hyper_entry.Finding,
148
  "collection_method": 'counting',
149
  "classification": hyper_entry.Classification,
150
+ "organ": hyper_entry.Organ
151
  }
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": open(img_path, 'rb').read(),
157
  "image_sha256": cal_sha256(img_path),
 
163
  "organ": "instrument"
164
  }
165
 
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": open(image_path, 'rb').read(),
185
+ "image_sha256": cal_sha256(image_path),
186
+ "points": cal_mid(label_dict[label]),
187
+ "count": len(label_dict[label]),
188
+ "label": class_map[label],
189
+ "collection_method": "counting",
190
+ "classification": "sperm",
191
+ "organ": "visem dataset"
192
+ }
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 --trust_remote_cod
197
  # huggingface-cli upload kvasir-points . . --repo-type dataset