Datasets:
Sayali9141
commited on
Commit
•
6e5700c
1
Parent(s):
ff3e14e
created the code file
Browse files- signals.py +118 -0
signals.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from typing import List
|
5 |
+
import datasets
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# TODO: Add BibTeX citation
|
9 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
10 |
+
_CITATION = """\
|
11 |
+
@InProceedings{huggingface:dataset,
|
12 |
+
title = {A great new dataset},
|
13 |
+
author={huggingface, Inc.
|
14 |
+
},
|
15 |
+
year={2020}
|
16 |
+
}
|
17 |
+
"""
|
18 |
+
|
19 |
+
# TODO: Add description of the dataset here
|
20 |
+
# You can copy an official description
|
21 |
+
_DESCRIPTION = """\
|
22 |
+
This dataset contains traffic images from traffic signal cameras of singapore. The images are captured at 1.5 minute interval from 6 pm to 7 pm everyday for the month of January 2024.
|
23 |
+
"""
|
24 |
+
|
25 |
+
# TODO: Add a link to an official homepage for the dataset here
|
26 |
+
_HOMEPAGE = "https://beta.data.gov.sg/collections/354/view"
|
27 |
+
|
28 |
+
# TODO: Add the licence for the dataset here if you can find it
|
29 |
+
_LICENSE = ""
|
30 |
+
|
31 |
+
# TODO: Add link to the official dataset URLs here
|
32 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
33 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
34 |
+
_URL = "https://github.com/Sayali-pingle/HuggingFace--Traffic-Image-Dataset/blob/main/camera_data.csv"
|
35 |
+
_URLS = {
|
36 |
+
"train": _URL + "train-v1.1.json",
|
37 |
+
"dev": _URL + "dev-v1.1.json",
|
38 |
+
}
|
39 |
+
|
40 |
+
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
41 |
+
class TrafficImages(datasets.GeneratorBasedBuilder):
|
42 |
+
"""TODO: Short description of my dataset."""
|
43 |
+
|
44 |
+
_URLS = _URLS
|
45 |
+
VERSION = datasets.Version("1.1.0")
|
46 |
+
|
47 |
+
def _info(self):
|
48 |
+
return datasets.DatasetInfo(
|
49 |
+
description=_DESCRIPTION,
|
50 |
+
features=datasets.Features(
|
51 |
+
{
|
52 |
+
"timestamp": datasets.Value("string"),
|
53 |
+
"camera_id": datasets.Value("string"),
|
54 |
+
"latitude": datasets.Value("float"),
|
55 |
+
"longitude": datasets.Value("float"),
|
56 |
+
"image_url": datasets.Value("string"),
|
57 |
+
"image_metadata": datasets.Value("string")
|
58 |
+
}
|
59 |
+
),
|
60 |
+
homepage=_HOMEPAGE,
|
61 |
+
citation=_CITATION,
|
62 |
+
)
|
63 |
+
|
64 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
65 |
+
urls_to_download = self._URLS
|
66 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
67 |
+
|
68 |
+
return [
|
69 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
70 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
|
71 |
+
]
|
72 |
+
|
73 |
+
def _generate_examples(self, file_path):
|
74 |
+
# This method will yield examples from your dataset
|
75 |
+
start_date = datetime(2024, 1, 1, 18, 0, 0)
|
76 |
+
end_date = datetime(2024, 1, 31, 19, 0, 0)
|
77 |
+
interval_seconds = 240
|
78 |
+
|
79 |
+
date_time_strings = [
|
80 |
+
(current_date + timedelta(seconds=seconds)).strftime('%Y-%m-%dT%H:%M:%S+08:00')
|
81 |
+
for current_date in pd.date_range(start=start_date, end=end_date, freq='D')
|
82 |
+
for seconds in range(0, 3600, interval_seconds)
|
83 |
+
]
|
84 |
+
|
85 |
+
url = 'https://api.data.gov.sg/v1/transport/traffic-images'
|
86 |
+
camera_data = []
|
87 |
+
|
88 |
+
for date_time in date_time_strings:
|
89 |
+
params = {'date_time': date_time}
|
90 |
+
response = requests.get(url, params=params)
|
91 |
+
|
92 |
+
if response.status_code == 200:
|
93 |
+
data = response.json()
|
94 |
+
camera_data.extend([
|
95 |
+
{
|
96 |
+
'timestamp': item['timestamp'],
|
97 |
+
'camera_id': camera['camera_id'],
|
98 |
+
'latitude': camera['location']['latitude'],
|
99 |
+
'longitude': camera['location']['longitude'],
|
100 |
+
'image_url': camera['image'],
|
101 |
+
'image_metadata': camera['image_metadata']
|
102 |
+
}
|
103 |
+
for item in data['items']
|
104 |
+
for camera in item['cameras']
|
105 |
+
])
|
106 |
+
else:
|
107 |
+
print(f"Error: {response.status_code}")
|
108 |
+
|
109 |
+
for idx, example in enumerate(camera_data):
|
110 |
+
yield idx, {
|
111 |
+
"timestamp": example["timestamp"],
|
112 |
+
"camera_id": example["camera_id"],
|
113 |
+
"latitude": example["latitude"],
|
114 |
+
"longitude": example["longitude"],
|
115 |
+
"image_url": example["image_url"],
|
116 |
+
"image_metadata": example["image_metadata"]
|
117 |
+
}
|
118 |
+
|