|
!pip install datasets |
|
|
|
from datasets import load_dataset, Dataset, Image as HFImage, concatenate_datasets |
|
import datasets |
|
from PIL import Image |
|
import pandas as pd |
|
import io |
|
|
|
def add_rows_to_dataset(dataset, new_rows): |
|
""" |
|
Adds new rows to a Hugging Face dataset. |
|
|
|
Args: |
|
dataset: The Hugging Face dataset to add rows to. |
|
new_rows: A list of dictionaries, where each dictionary represents a new row |
|
and has the keys 'image', 'image_id', and 'caption'. |
|
Returns: |
|
The updated dataset. |
|
""" |
|
|
|
|
|
|
|
for row in new_rows: |
|
|
|
image_bytes = io.BytesIO() |
|
row['image'].save(image_bytes, format='PNG') |
|
|
|
row['image'] = image_bytes.getvalue() |
|
|
|
new_dataset = Dataset.from_pandas(pd.DataFrame(new_rows)).cast_column("image", HFImage()) |
|
new_dataset.info.dataset_name = dataset.info.dataset_name |
|
new_dataset.info.description = dataset.info.description |
|
|
|
return concatenate_datasets([dataset,new_dataset]) |
|
|
|
|
|
|
|
dataset = load_dataset("mdwiratathya/ROCO-radiology", split="train") |
|
|
|
new_rows = [ |
|
{ |
|
"image": Image.open("radio/lux2.jpeg"), |
|
"image_id": "RONA_00001", |
|
"caption": "Right shoulder of a 50-year-old patient showing an anterior dislocated shoulder." |
|
}, |
|
{ |
|
"image": Image.open("radio/lux1.jpeg"), |
|
"image_id": "RONA_00002", |
|
"caption": " Right shoulder of a 50-year-old patient showing an anterior dislocated shoulder" |
|
}, |
|
{ |
|
"image": Image.open("radio/lux3.jpeg"), |
|
"image_id": "RONA_00003", |
|
"caption": "Right shoulder of a 50-year-old patient following a dislocated shoulder reduction" |
|
}, |
|
{ |
|
"image": Image.open("radio/lux4.jpeg"), |
|
"image_id": "RONA_00004", |
|
"caption": "Right shoulder of a 50-year-old patient following a dislocated shoulder reduction" |
|
}, |
|
] |
|
|
|
|
|
|
|
new_dataset = add_rows_to_dataset(dataset, new_rows) |
|
|
|
|
|
|
|
|
|
new_dataset.push_to_hub("eltorio/ROCO-radiology") |