Create add_rows.py
Browse files- add_rows.py +70 -0
add_rows.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install datasets
|
2 |
+
|
3 |
+
from datasets import load_dataset, Dataset, Image as HFImage, concatenate_datasets
|
4 |
+
import datasets
|
5 |
+
from PIL import Image
|
6 |
+
import pandas as pd
|
7 |
+
import io
|
8 |
+
|
9 |
+
def add_rows_to_dataset(dataset, new_rows):
|
10 |
+
"""
|
11 |
+
Adds new rows to a Hugging Face dataset.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
dataset: The Hugging Face dataset to add rows to.
|
15 |
+
new_rows: A list of dictionaries, where each dictionary represents a new row
|
16 |
+
and has the keys 'image', 'image_id', and 'caption'.
|
17 |
+
Returns:
|
18 |
+
The updated dataset.
|
19 |
+
"""
|
20 |
+
# Convert PIL.Image.Image objects to bytes and then back to PNG
|
21 |
+
# This will allow them to be stored within the Huggingface dataset
|
22 |
+
# and ensures the type is PIL.PngImagePlugin.PngImageFile.
|
23 |
+
for row in new_rows:
|
24 |
+
# Store the image as bytes in a buffer
|
25 |
+
image_bytes = io.BytesIO()
|
26 |
+
row['image'].save(image_bytes, format='PNG') # Save as PNG
|
27 |
+
# Replace PIL image with a new PNG image created from the bytes
|
28 |
+
row['image'] = image_bytes.getvalue()
|
29 |
+
|
30 |
+
new_dataset = Dataset.from_pandas(pd.DataFrame(new_rows)).cast_column("image", HFImage())
|
31 |
+
new_dataset.info.dataset_name = dataset.info.dataset_name
|
32 |
+
new_dataset.info.description = dataset.info.description
|
33 |
+
|
34 |
+
return concatenate_datasets([dataset,new_dataset])
|
35 |
+
|
36 |
+
|
37 |
+
# Load the dataset (replace with the correct dataset name if needed)
|
38 |
+
dataset = load_dataset("mdwiratathya/ROCO-radiology", split="train")
|
39 |
+
|
40 |
+
new_rows = [
|
41 |
+
{
|
42 |
+
"image": Image.open("radio/lux2.jpeg"),
|
43 |
+
"image_id": "RONA_00001",
|
44 |
+
"caption": "Right shoulder of a 50-year-old patient showing an anterior dislocated shoulder."
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"image": Image.open("radio/lux1.jpeg"),
|
48 |
+
"image_id": "RONA_00002",
|
49 |
+
"caption": " Right shoulder of a 50-year-old patient showing an anterior dislocated shoulder"
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"image": Image.open("radio/lux3.jpeg"),
|
53 |
+
"image_id": "RONA_00003",
|
54 |
+
"caption": "Right shoulder of a 50-year-old patient following a dislocated shoulder reduction"
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"image": Image.open("radio/lux4.jpeg"),
|
58 |
+
"image_id": "RONA_00004",
|
59 |
+
"caption": "Right shoulder of a 50-year-old patient following a dislocated shoulder reduction"
|
60 |
+
},
|
61 |
+
]
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
new_dataset = add_rows_to_dataset(dataset, new_rows)
|
66 |
+
|
67 |
+
# print(f"Number of rows in the updated dataset: {len(new_dataset)}")
|
68 |
+
# print(f"Dataset information: {new_dataset.info}")
|
69 |
+
# Print some info about the updated dataset
|
70 |
+
new_dataset.push_to_hub("eltorio/ROCO-radiology")
|