Update PlotQA_dataset.py
Browse files- PlotQA_dataset.py +56 -49
PlotQA_dataset.py
CHANGED
|
@@ -76,9 +76,9 @@ class PlotQA(datasets.GeneratorBasedBuilder):
|
|
| 76 |
"imgname": datasets.Value("string"),
|
| 77 |
"image": datasets.Image(),
|
| 78 |
"version": datasets.Value("string"),
|
| 79 |
-
"query": datasets.Value("string"),
|
| 80 |
"query_token": datasets.Sequence(datasets.Value("string")),
|
| 81 |
-
"label": datasets.Value("string"),
|
| 82 |
"img_ann": datasets.Value("string"),
|
| 83 |
###
|
| 84 |
"image_index": datasets.Value("string"),
|
|
@@ -139,6 +139,24 @@ class PlotQA(datasets.GeneratorBasedBuilder):
|
|
| 139 |
),
|
| 140 |
]
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def _generate_examples(self, annotation_v1_path:str, annotation_v2_path:str, img_anno_path:str ,images_path: str):
|
| 143 |
#Load image folder
|
| 144 |
# open file
|
|
@@ -146,52 +164,41 @@ class PlotQA(datasets.GeneratorBasedBuilder):
|
|
| 146 |
# extracting file
|
| 147 |
file.extractall('./imgs')
|
| 148 |
file.close()
|
| 149 |
-
|
| 150 |
-
|
| 151 |
idx = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
# file_name = os.path.basename(anno_path)
|
| 174 |
-
# #Table load
|
| 175 |
-
# df = pd.read_csv (os.path.join(table_path,item["imgname"].split('.')[0]+'.csv'))
|
| 176 |
-
# item["table"] = df.to_dict()
|
| 177 |
-
|
| 178 |
-
# file = os.path.splitext(file_name)
|
| 179 |
-
# if file == "test_augmented":
|
| 180 |
-
# item["human"] = False
|
| 181 |
-
# else:
|
| 182 |
-
# item["human"] = True
|
| 183 |
-
|
| 184 |
-
# img_anot_file = os.path.splitext(item["imgname"])[0]+'.json'
|
| 185 |
-
# img_anot = os.path.join(img_anno_path, img_anot_file)
|
| 186 |
-
# with open(img_anot) as f:
|
| 187 |
-
# item["img_ann"] = json.load(f)
|
| 188 |
-
"""
|
| 189 |
-
item['table'] = os.path.join(images_path,item["imgname"])
|
| 190 |
-
# annotation
|
| 191 |
-
item["img_anno"] = load json file...
|
| 192 |
-
t_path = os.path.join(table_path,item["table_name"])
|
| 193 |
-
table_data = load_dataset("csv", data_files=[t_path])
|
| 194 |
-
yield table_data
|
| 195 |
-
"""
|
| 196 |
-
yield idx, item
|
| 197 |
-
idx += 1
|
|
|
|
| 76 |
"imgname": datasets.Value("string"),
|
| 77 |
"image": datasets.Image(),
|
| 78 |
"version": datasets.Value("string"),
|
| 79 |
+
"query": datasets.Sequence(datasets.Value("string")),
|
| 80 |
"query_token": datasets.Sequence(datasets.Value("string")),
|
| 81 |
+
"label": datasets.Sequence(datasets.Value("string")),
|
| 82 |
"img_ann": datasets.Value("string"),
|
| 83 |
###
|
| 84 |
"image_index": datasets.Value("string"),
|
|
|
|
| 139 |
),
|
| 140 |
]
|
| 141 |
|
| 142 |
+
def find_qa(self, annotation_v1_path:str, annotation_v2_path:str, img_idx):
|
| 143 |
+
with open(annotation_v1_path, "r", encoding="utf-8") as v1:
|
| 144 |
+
data_v1 = json.load(v1)
|
| 145 |
+
|
| 146 |
+
with open(annotation_v2_path, "r", encoding="utf-8") as v2:
|
| 147 |
+
data_v2 = json.load(v2)
|
| 148 |
+
|
| 149 |
+
#1.
|
| 150 |
+
version = 1
|
| 151 |
+
_temp_item = []
|
| 152 |
+
for ele in data_v1:
|
| 153 |
+
if ele['image_index'] == img_idx:
|
| 154 |
+
_temp_item.append(ele)
|
| 155 |
+
for ele in data_v2:
|
| 156 |
+
if ele['image_index'] == img_idx:
|
| 157 |
+
_temp_item.append(ele)
|
| 158 |
+
return _temp_item
|
| 159 |
+
|
| 160 |
def _generate_examples(self, annotation_v1_path:str, annotation_v2_path:str, img_anno_path:str ,images_path: str):
|
| 161 |
#Load image folder
|
| 162 |
# open file
|
|
|
|
| 164 |
# extracting file
|
| 165 |
file.extractall('./imgs')
|
| 166 |
file.close()
|
| 167 |
+
_multi_anno = [annotation_v1_path, annotation_v2_path]
|
| 168 |
+
|
| 169 |
idx = 0
|
| 170 |
+
with open(img_anno_path, "r", encoding="utf-8") as a:
|
| 171 |
+
img_data = json.load(a)
|
| 172 |
+
for ele in img_data:
|
| 173 |
+
item = {}
|
| 174 |
+
item["img_ann"] = ele
|
| 175 |
+
item["imgname"] = str(ele['image_index'])+'.png'
|
| 176 |
+
item['image'] = os.path.join('./imgs',item['imgname'])
|
| 177 |
+
item["query_token"] = []
|
| 178 |
+
qa_returns = find_qa(annotation_v1_path,annotation_v2_path, item['image_index'])
|
| 179 |
+
_question = []
|
| 180 |
+
_label = []
|
| 181 |
+
for pair in qa_returns:
|
| 182 |
+
_question.append(pair['question_string'])
|
| 183 |
+
_label.append(pair["answer"])
|
| 184 |
|
| 185 |
+
# file = os.path.splitext(file_name)
|
| 186 |
+
# if file == "test_augmented":
|
| 187 |
+
# item["human"] = False
|
| 188 |
+
# else:
|
| 189 |
+
# item["human"] = True
|
| 190 |
+
|
| 191 |
+
# img_anot_file = os.path.splitext(item["imgname"])[0]+'.json'
|
| 192 |
+
# img_anot = os.path.join(img_anno_path, img_anot_file)
|
| 193 |
+
# with open(img_anot) as f:
|
| 194 |
+
# item["img_ann"] = json.load(f)
|
| 195 |
+
"""
|
| 196 |
+
item['table'] = os.path.join(images_path,item["imgname"])
|
| 197 |
+
# annotation
|
| 198 |
+
item["img_anno"] = load json file...
|
| 199 |
+
t_path = os.path.join(table_path,item["table_name"])
|
| 200 |
+
table_data = load_dataset("csv", data_files=[t_path])
|
| 201 |
+
yield table_data
|
| 202 |
+
"""
|
| 203 |
+
yield idx, item
|
| 204 |
+
idx += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|