Alexandr Bolotin
commited on
Commit
·
ba84249
1
Parent(s):
0631a14
delete file
Browse files- parquet_push_to_hub.py +65 -0
parquet_push_to_hub.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import os
|
| 3 |
+
import datasets
|
| 4 |
+
import traceback
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from datasets import Features, Dataset
|
| 8 |
+
|
| 9 |
+
def create_features():
|
| 10 |
+
features = Features(
|
| 11 |
+
{
|
| 12 |
+
'image': datasets.Image(),
|
| 13 |
+
"type": datasets.Value("string"),
|
| 14 |
+
}
|
| 15 |
+
)
|
| 16 |
+
return features
|
| 17 |
+
|
| 18 |
+
def image_to_bytes(image_path):
|
| 19 |
+
image = Image.open(image_path)
|
| 20 |
+
|
| 21 |
+
image_data = io.BytesIO()
|
| 22 |
+
image.save(image_data, format='PNG')
|
| 23 |
+
|
| 24 |
+
img_data = image_data.getvalue()
|
| 25 |
+
|
| 26 |
+
return img_data
|
| 27 |
+
|
| 28 |
+
def main():
|
| 29 |
+
current_directory = os.path.dirname(os.path.realpath(__file__))
|
| 30 |
+
|
| 31 |
+
files_folder = 'files'
|
| 32 |
+
files_folder_path = os.path.join(current_directory, files_folder)
|
| 33 |
+
|
| 34 |
+
dataset_dict = {
|
| 35 |
+
'image': [],
|
| 36 |
+
"type": [],
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
for root, dirs, files in os.walk(files_folder_path):
|
| 40 |
+
for file in files:
|
| 41 |
+
|
| 42 |
+
if file.lower().endswith(('.jpg', '.jpeg')):
|
| 43 |
+
file_path = os.path.join(root, file)
|
| 44 |
+
img_data = image_to_bytes(file_path)
|
| 45 |
+
dataset_dict['image'].append(img_data)
|
| 46 |
+
dataset_dict['type'].append(os.path.basename(root))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# print('dataset_dict', dataset_dict)
|
| 50 |
+
# print('features', create_features())
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
dataset = Dataset.from_dict(dataset_dict, features=create_features())
|
| 54 |
+
except Exception as e:
|
| 55 |
+
traceback.print_exc()
|
| 56 |
+
# print(e)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
os.environ["HF_HOME"] = "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"
|
| 60 |
+
|
| 61 |
+
dataset.push_to_hub('TrainingDataPro/chest-x-rays')
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|