nielsr HF staff commited on
Commit
562c3b7
1 Parent(s): 73ba5e0

Update XFUN.py

Browse files
Files changed (1) hide show
  1. XFUN.py +10 -7
XFUN.py CHANGED
@@ -10,14 +10,15 @@ import numpy as np
10
 
11
  from transformers import AutoTokenizer
12
 
13
- def load_image(image_path):
14
  image = Image.open(image_path).convert("RGB")
15
  w, h = image.size
16
- # resize image to 224x224
17
- image = image.resize((224, 224))
18
- image = np.asarray(image)
19
- image = image[:, :, ::-1] # flip color channels from RGB to BGR
20
- image = image.transpose(2, 0, 1) # move channels to first dimension
 
21
  return image, (w, h)
22
 
23
 
@@ -143,7 +144,8 @@ class XFUN(datasets.GeneratorBasedBuilder):
143
 
144
  for doc in data["documents"]:
145
  doc["img"]["fpath"] = os.path.join(filepath[1], doc["img"]["fname"])
146
- image, size = load_image(doc["img"]["fpath"])
 
147
  document = doc["document"]
148
  tokenized_doc = {"input_ids": [], "bbox": [], "labels": []}
149
  entities = []
@@ -272,6 +274,7 @@ class XFUN(datasets.GeneratorBasedBuilder):
272
  {
273
  "id": f"{doc['id']}_{chunk_id}",
274
  "image": image,
 
275
  "entities": entities_in_this_span,
276
  "relations": relations_in_this_span,
277
  }
 
10
 
11
  from transformers import AutoTokenizer
12
 
13
+ def load_image(image_path, size):
14
  image = Image.open(image_path).convert("RGB")
15
  w, h = image.size
16
+ if size is not None:
17
+ # resize image
18
+ image = image.resize((size, size))
19
+ image = np.asarray(image)
20
+ image = image[:, :, ::-1] # flip color channels from RGB to BGR
21
+ image = image.transpose(2, 0, 1) # move channels to first dimension
22
  return image, (w, h)
23
 
24
 
 
144
 
145
  for doc in data["documents"]:
146
  doc["img"]["fpath"] = os.path.join(filepath[1], doc["img"]["fname"])
147
+ image, size = load_image(doc["img"]["fpath"], size=224)
148
+ original_image, _ = load_image(doc["img"]["fpath"])
149
  document = doc["document"]
150
  tokenized_doc = {"input_ids": [], "bbox": [], "labels": []}
151
  entities = []
 
274
  {
275
  "id": f"{doc['id']}_{chunk_id}",
276
  "image": image,
277
+ "original_image": original_image,
278
  "entities": entities_in_this_span,
279
  "relations": relations_in_this_span,
280
  }