Datasets:
Upload PM25Vision.py with huggingface_hub
Browse files- PM25Vision.py +79 -0
PM25Vision.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import datasets
|
| 4 |
+
import zipfile
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class PM25Vision(datasets.GeneratorBasedBuilder):
|
| 8 |
+
def _info(self):
|
| 9 |
+
return datasets.DatasetInfo(
|
| 10 |
+
description="PM25Vision: Street-level imagery with PM2.5 annotations",
|
| 11 |
+
features=datasets.Features(
|
| 12 |
+
{
|
| 13 |
+
"image_id": datasets.Value("int64"),
|
| 14 |
+
"station_id": datasets.Value("int64"),
|
| 15 |
+
"captured_at": datasets.Value("string"),
|
| 16 |
+
"camera_angle": datasets.Value("float64"),
|
| 17 |
+
"longitude": datasets.Value("float64"),
|
| 18 |
+
"latitude": datasets.Value("float64"),
|
| 19 |
+
"quality_score": datasets.Value("float64"),
|
| 20 |
+
"downloaded_at": datasets.Value("string"),
|
| 21 |
+
"pm25": datasets.Value("float64"),
|
| 22 |
+
"image": datasets.Image(),
|
| 23 |
+
"quality": datasets.Value("string"),
|
| 24 |
+
"pm25_bin": datasets.Value("string"),
|
| 25 |
+
}
|
| 26 |
+
),
|
| 27 |
+
supervised_keys=None,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def _split_generators(self, dl_manager):
|
| 31 |
+
|
| 32 |
+
train_path = dl_manager.download("train.zip")
|
| 33 |
+
test_path = dl_manager.download("test.zip")
|
| 34 |
+
|
| 35 |
+
return [
|
| 36 |
+
datasets.SplitGenerator(
|
| 37 |
+
name=datasets.Split.TRAIN,
|
| 38 |
+
gen_kwargs={"archive_path": train_path},
|
| 39 |
+
),
|
| 40 |
+
datasets.SplitGenerator(
|
| 41 |
+
name=datasets.Split.TEST,
|
| 42 |
+
gen_kwargs={"archive_path": test_path},
|
| 43 |
+
),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
def _generate_examples(self, archive_path):
|
| 47 |
+
|
| 48 |
+
if self.config.streaming:
|
| 49 |
+
# Streaming
|
| 50 |
+
with zipfile.ZipFile(archive_path, "r") as z:
|
| 51 |
+
# find metadata.csv
|
| 52 |
+
meta_name = None
|
| 53 |
+
for name in z.namelist():
|
| 54 |
+
if name.endswith("metadata.csv"):
|
| 55 |
+
meta_name = name
|
| 56 |
+
break
|
| 57 |
+
if meta_name is None:
|
| 58 |
+
raise FileNotFoundError("metadata.csv not found in archive")
|
| 59 |
+
|
| 60 |
+
with z.open(meta_name) as f:
|
| 61 |
+
reader = csv.DictReader(line.decode("utf-8") for line in f)
|
| 62 |
+
for i, row in enumerate(reader):
|
| 63 |
+
img_name = f"images/{row['filename']}"
|
| 64 |
+
if img_name in z.namelist():
|
| 65 |
+
img_bytes = z.read(img_name)
|
| 66 |
+
row["image"] = {"bytes": img_bytes, "path": row["filename"]}
|
| 67 |
+
yield i, row
|
| 68 |
+
else:
|
| 69 |
+
# default
|
| 70 |
+
base_dir = datasets.filesystems.extract_archive(archive_path)
|
| 71 |
+
metadata_file = os.path.join(base_dir, "metadata.csv")
|
| 72 |
+
images_dir = os.path.join(base_dir, "images")
|
| 73 |
+
|
| 74 |
+
with open(metadata_file, encoding="utf-8") as f:
|
| 75 |
+
reader = csv.DictReader(f)
|
| 76 |
+
for i, row in enumerate(reader):
|
| 77 |
+
image_path = os.path.join(images_dir, row["filename"])
|
| 78 |
+
row["image"] = image_path
|
| 79 |
+
yield i, row
|