Spaces:
Sleeping
Sleeping
Merge pull request #40 from sdsc-ordes/fix/timezones
Browse files- src/input/input_handling.py +25 -6
- src/input/input_observation.py +15 -4
- src/input/input_validator.py +22 -0
- tests/test_input_observation.py +12 -6
src/input/input_handling.py
CHANGED
@@ -12,7 +12,7 @@ import cv2
|
|
12 |
import numpy as np
|
13 |
|
14 |
from input.input_observation import InputObservation
|
15 |
-
from input.input_validator import get_image_datetime, is_valid_email, is_valid_number, get_image_latlon
|
16 |
|
17 |
m_logger = logging.getLogger(__name__)
|
18 |
m_logger.setLevel(logging.INFO)
|
@@ -206,6 +206,7 @@ def metadata_inputs_one_file(file:UploadedFile, image_hash:str, dbg_ix:int=0) ->
|
|
206 |
author_email = st.session_state["input_author_email"]
|
207 |
filename = file.name
|
208 |
image_datetime_raw = get_image_datetime(file)
|
|
|
209 |
latitude0, longitude0 = get_image_latlon(file)
|
210 |
msg = f"[D] {filename}: lat, lon from image metadata: {latitude0}, {longitude0}"
|
211 |
m_logger.debug(msg)
|
@@ -247,19 +248,37 @@ def metadata_inputs_one_file(file:UploadedFile, image_hash:str, dbg_ix:int=0) ->
|
|
247 |
# 5. Date/time
|
248 |
## first from image metadata
|
249 |
if image_datetime_raw is not None:
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
else:
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
## either way, give user the option to enter manually (or correct, e.g. if camera has no rtc clock)
|
257 |
date = viewcontainer.date_input("Date for "+filename, value=date_value, key=f"input_date_{image_hash}")
|
258 |
time = viewcontainer.time_input("Time for "+filename, time_value, key=f"input_time_{image_hash}")
|
259 |
|
|
|
|
|
260 |
observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
|
261 |
author_email=author_email, image_datetime_raw=image_datetime_raw,
|
262 |
-
date=date, time=time,
|
263 |
uploaded_file=file, image_md5=image_hash
|
264 |
)
|
265 |
|
|
|
12 |
import numpy as np
|
13 |
|
14 |
from input.input_observation import InputObservation
|
15 |
+
from input.input_validator import get_image_datetime, is_valid_email, is_valid_number, get_image_latlon, get_image_timezone
|
16 |
|
17 |
m_logger = logging.getLogger(__name__)
|
18 |
m_logger.setLevel(logging.INFO)
|
|
|
206 |
author_email = st.session_state["input_author_email"]
|
207 |
filename = file.name
|
208 |
image_datetime_raw = get_image_datetime(file)
|
209 |
+
image_timezone_raw = get_image_timezone(file)
|
210 |
latitude0, longitude0 = get_image_latlon(file)
|
211 |
msg = f"[D] {filename}: lat, lon from image metadata: {latitude0}, {longitude0}"
|
212 |
m_logger.debug(msg)
|
|
|
248 |
# 5. Date/time
|
249 |
## first from image metadata
|
250 |
if image_datetime_raw is not None:
|
251 |
+
# if we have a timezone let's use it (but only if we also have datetime)
|
252 |
+
time_fmt = '%Y:%m:%d %H:%M:%S'
|
253 |
+
if image_timezone_raw is not None:
|
254 |
+
image_datetime_raw += f" {image_timezone_raw}"
|
255 |
+
time_fmt += ' %z'
|
256 |
+
#
|
257 |
+
dt = datetime.datetime.strptime(image_datetime_raw, time_fmt)
|
258 |
+
date_value = dt.date()
|
259 |
+
time_value = dt.time()
|
260 |
+
|
261 |
+
#time_value = datetime.datetime.strptime(image_datetime_raw, '%Y:%m:%d %H:%M:%S').time()
|
262 |
+
#date_value = datetime.datetime.strptime(image_datetime_raw, '%Y:%m:%d %H:%M:%S').date()
|
263 |
else:
|
264 |
+
# get current time, with user timezone (or is it server timezone?! TODO: test with different zones)
|
265 |
+
dt = datetime.datetime.now().astimezone().replace(microsecond=0)
|
266 |
+
time_value = dt.time()
|
267 |
+
date_value = dt.date()
|
268 |
+
|
269 |
+
#time_value = datetime.datetime.now().time() # Default to current time
|
270 |
+
#date_value = datetime.datetime.now().date()
|
271 |
+
|
272 |
|
273 |
## either way, give user the option to enter manually (or correct, e.g. if camera has no rtc clock)
|
274 |
date = viewcontainer.date_input("Date for "+filename, value=date_value, key=f"input_date_{image_hash}")
|
275 |
time = viewcontainer.time_input("Time for "+filename, time_value, key=f"input_time_{image_hash}")
|
276 |
|
277 |
+
tz_str = dt.strftime('%z') # this is numeric, otherwise the info isn't consistent.
|
278 |
+
|
279 |
observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
|
280 |
author_email=author_email, image_datetime_raw=image_datetime_raw,
|
281 |
+
date=date, time=time, timezone=tz_str,
|
282 |
uploaded_file=file, image_md5=image_hash
|
283 |
)
|
284 |
|
src/input/input_observation.py
CHANGED
@@ -26,6 +26,8 @@ class InputObservation:
|
|
26 |
Date of the observation
|
27 |
time (datetime.time):
|
28 |
Time of the observation
|
|
|
|
|
29 |
uploaded_file (UploadedFile):
|
30 |
The uploaded file associated with the observation.
|
31 |
image_md5 (str):
|
@@ -57,6 +59,7 @@ class InputObservation:
|
|
57 |
author_email:str=None, image_datetime_raw:str=None,
|
58 |
date:datetime.date=None,
|
59 |
time:datetime.time=None,
|
|
|
60 |
uploaded_file:UploadedFile=None, image_md5:str=None):
|
61 |
|
62 |
self.image = image
|
@@ -66,6 +69,7 @@ class InputObservation:
|
|
66 |
self.image_datetime_raw = image_datetime_raw
|
67 |
self.date = date
|
68 |
self.time = time
|
|
|
69 |
self.uploaded_file = uploaded_file
|
70 |
self.image_md5 = image_md5
|
71 |
# attributes that get set after predictions/processing
|
@@ -121,7 +125,7 @@ class InputObservation:
|
|
121 |
return (
|
122 |
f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
|
123 |
f"{self.author_email}, {self.image_datetime_raw}, {self.date}, "
|
124 |
-
f"{self.time}, {self.uploaded_file}, {self.image_md5}"
|
125 |
)
|
126 |
|
127 |
def __repr__(self):
|
@@ -135,6 +139,7 @@ class InputObservation:
|
|
135 |
f"raw timestamp: {self.image_datetime_raw}, "
|
136 |
f"Date: {self.date}, "
|
137 |
f"Time: {self.time}, "
|
|
|
138 |
f"Uploaded Filename: {self.uploaded_file}"
|
139 |
f"Image MD5 hash: {self.image_md5}"
|
140 |
)
|
@@ -158,6 +163,7 @@ class InputObservation:
|
|
158 |
self.date == other.date and
|
159 |
# temporarily skip time, it is followed by the clock and that is always differnt
|
160 |
#self.time == other.time and
|
|
|
161 |
self.uploaded_file == other.uploaded_file and
|
162 |
self.image_md5 == other.image_md5
|
163 |
)
|
@@ -167,7 +173,7 @@ class InputObservation:
|
|
167 |
# only highlight the differences, if element is the same don't show it
|
168 |
# have a summary at the top that shows if the observations are the same or not
|
169 |
|
170 |
-
def show_diff(self, other):
|
171 |
"""Show the differences between two observations"""
|
172 |
differences = []
|
173 |
if self.image is None or other.image is None:
|
@@ -189,6 +195,8 @@ class InputObservation:
|
|
189 |
differences.append(f" Date is different. (self: {self.date}, other: {other.date})")
|
190 |
if self.time != other.time:
|
191 |
differences.append(f" Time is different. (self: {self.time}, other: {other.time})")
|
|
|
|
|
192 |
if self.uploaded_file != other.uploaded_file:
|
193 |
differences.append(" Uploaded filename is different.")
|
194 |
if self.image_md5 != other.image_md5:
|
@@ -216,6 +224,7 @@ class InputObservation:
|
|
216 |
"image_datetime_raw": self.image_datetime_raw,
|
217 |
"date": str(self.date),
|
218 |
"time": str(self.time),
|
|
|
219 |
"selected_class": self._selected_class,
|
220 |
"top_prediction": self._top_predictions[0] if len(self._top_predictions) else None,
|
221 |
"class_overriden": self._class_overriden,
|
@@ -233,12 +242,13 @@ class InputObservation:
|
|
233 |
image_datetime_raw=data.get("image_datetime_raw"),
|
234 |
date=data.get("date"),
|
235 |
time=data.get("time"),
|
|
|
236 |
uploaded_file=data.get("uploaded_file"),
|
237 |
image_hash=data.get("image_md5")
|
238 |
)
|
239 |
|
240 |
@classmethod
|
241 |
-
def from_input(cls, input):
|
242 |
return cls(
|
243 |
image=input.image,
|
244 |
latitude=input.latitude,
|
@@ -247,8 +257,9 @@ class InputObservation:
|
|
247 |
image_datetime_raw=input.image_datetime_raw,
|
248 |
date=input.date,
|
249 |
time=input.time,
|
|
|
250 |
uploaded_file=input.uploaded_file,
|
251 |
-
|
252 |
)
|
253 |
|
254 |
|
|
|
26 |
Date of the observation
|
27 |
time (datetime.time):
|
28 |
Time of the observation
|
29 |
+
timezone (str):
|
30 |
+
Timezone of the observation (e.g. +0300)
|
31 |
uploaded_file (UploadedFile):
|
32 |
The uploaded file associated with the observation.
|
33 |
image_md5 (str):
|
|
|
59 |
author_email:str=None, image_datetime_raw:str=None,
|
60 |
date:datetime.date=None,
|
61 |
time:datetime.time=None,
|
62 |
+
timezone:str=None,
|
63 |
uploaded_file:UploadedFile=None, image_md5:str=None):
|
64 |
|
65 |
self.image = image
|
|
|
69 |
self.image_datetime_raw = image_datetime_raw
|
70 |
self.date = date
|
71 |
self.time = time
|
72 |
+
self.timezone = timezone
|
73 |
self.uploaded_file = uploaded_file
|
74 |
self.image_md5 = image_md5
|
75 |
# attributes that get set after predictions/processing
|
|
|
125 |
return (
|
126 |
f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
|
127 |
f"{self.author_email}, {self.image_datetime_raw}, {self.date}, "
|
128 |
+
f"{self.time}, {self.timezone}, {self.uploaded_file}, {self.image_md5}"
|
129 |
)
|
130 |
|
131 |
def __repr__(self):
|
|
|
139 |
f"raw timestamp: {self.image_datetime_raw}, "
|
140 |
f"Date: {self.date}, "
|
141 |
f"Time: {self.time}, "
|
142 |
+
f"Timezone: {self.timezone}, "
|
143 |
f"Uploaded Filename: {self.uploaded_file}"
|
144 |
f"Image MD5 hash: {self.image_md5}"
|
145 |
)
|
|
|
163 |
self.date == other.date and
|
164 |
# temporarily skip time, it is followed by the clock and that is always differnt
|
165 |
#self.time == other.time and
|
166 |
+
self.timezone == other.timezone and
|
167 |
self.uploaded_file == other.uploaded_file and
|
168 |
self.image_md5 == other.image_md5
|
169 |
)
|
|
|
173 |
# only highlight the differences, if element is the same don't show it
|
174 |
# have a summary at the top that shows if the observations are the same or not
|
175 |
|
176 |
+
def show_diff(self, other: 'InputObservation'):
|
177 |
"""Show the differences between two observations"""
|
178 |
differences = []
|
179 |
if self.image is None or other.image is None:
|
|
|
195 |
differences.append(f" Date is different. (self: {self.date}, other: {other.date})")
|
196 |
if self.time != other.time:
|
197 |
differences.append(f" Time is different. (self: {self.time}, other: {other.time})")
|
198 |
+
if self.timezone != other.timezone:
|
199 |
+
differences.append(f" Timezone is different. (self: {self.timezone}, other: {other.timezone})")
|
200 |
if self.uploaded_file != other.uploaded_file:
|
201 |
differences.append(" Uploaded filename is different.")
|
202 |
if self.image_md5 != other.image_md5:
|
|
|
224 |
"image_datetime_raw": self.image_datetime_raw,
|
225 |
"date": str(self.date),
|
226 |
"time": str(self.time),
|
227 |
+
"timezone": str(self.timezone),
|
228 |
"selected_class": self._selected_class,
|
229 |
"top_prediction": self._top_predictions[0] if len(self._top_predictions) else None,
|
230 |
"class_overriden": self._class_overriden,
|
|
|
242 |
image_datetime_raw=data.get("image_datetime_raw"),
|
243 |
date=data.get("date"),
|
244 |
time=data.get("time"),
|
245 |
+
timezone=data.get("timezone"),
|
246 |
uploaded_file=data.get("uploaded_file"),
|
247 |
image_hash=data.get("image_md5")
|
248 |
)
|
249 |
|
250 |
@classmethod
|
251 |
+
def from_input(cls, input: 'InputObservation'):
|
252 |
return cls(
|
253 |
image=input.image,
|
254 |
latitude=input.latitude,
|
|
|
257 |
image_datetime_raw=input.image_datetime_raw,
|
258 |
date=input.date,
|
259 |
time=input.time,
|
260 |
+
timezone=input.timezone,
|
261 |
uploaded_file=input.uploaded_file,
|
262 |
+
image_md5=input.image_md5
|
263 |
)
|
264 |
|
265 |
|
src/input/input_validator.py
CHANGED
@@ -84,6 +84,28 @@ def get_image_datetime(image_file:UploadedFile) -> Union[str, None]:
|
|
84 |
# TODO: add to logger
|
85 |
return None
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
def decimal_coords(coords:tuple, ref:str) -> Fraction:
|
89 |
"""
|
|
|
84 |
# TODO: add to logger
|
85 |
return None
|
86 |
|
87 |
+
# function to extract the timezone from image metadata
|
88 |
+
def get_image_timezone(image_file:UploadedFile) -> Union[str, None]:
|
89 |
+
"""
|
90 |
+
Extracts the timezone from the EXIF metadata of an uploaded image file.
|
91 |
+
|
92 |
+
Args:
|
93 |
+
image_file (UploadedFile): The uploaded image file from which to extract the timezone.
|
94 |
+
|
95 |
+
Returns:
|
96 |
+
str: The timezone as a string if available, otherwise None.
|
97 |
+
|
98 |
+
Raises:
|
99 |
+
Warning: If the timezone could not be extracted from the image metadata.
|
100 |
+
"""
|
101 |
+
try:
|
102 |
+
image = Image.open(image_file)
|
103 |
+
exif_data = image._getexif()
|
104 |
+
if exif_data is not None:
|
105 |
+
if ExifTags.Base.OffsetTimeOriginal in exif_data:
|
106 |
+
return exif_data.get(ExifTags.Base.OffsetTimeOriginal)
|
107 |
+
except Exception as e: # FIXME: what types of exception?
|
108 |
+
st.warning(f"Could not extract timezone from image metadata. (file: {image_file.name})")
|
109 |
|
110 |
def decimal_coords(coords:tuple, ref:str) -> Fraction:
|
111 |
"""
|
tests/test_input_observation.py
CHANGED
@@ -89,11 +89,12 @@ def test_input_observation_valid(mock_uploadedFile):
|
|
89 |
|
90 |
_date="2023-10-10"
|
91 |
_time="10:10:10"
|
92 |
-
|
93 |
-
|
|
|
94 |
date = dt.date()
|
95 |
time = dt.time()
|
96 |
-
|
97 |
## make a random image with dtype uint8 using np.random.randint
|
98 |
image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
99 |
image_md5 = 'd1d2515e6f6ac4c5ca6dd739d5143cd4' # 32 hex chars.
|
@@ -101,7 +102,7 @@ def test_input_observation_valid(mock_uploadedFile):
|
|
101 |
obs = InputObservation(
|
102 |
image=image,
|
103 |
latitude=12.34, longitude=56.78, author_email=author_email,
|
104 |
-
time=time, date=date,
|
105 |
uploaded_file=mock_file,
|
106 |
image_md5=image_md5,
|
107 |
)
|
@@ -116,6 +117,7 @@ def test_input_observation_valid(mock_uploadedFile):
|
|
116 |
assert isinstance(obs.time, datetime.time)
|
117 |
assert str(obs.date) == "2023-10-10"
|
118 |
assert str(obs.time) == "10:10:10"
|
|
|
119 |
|
120 |
assert obs.uploaded_file.name == image_name
|
121 |
assert obs.uploaded_file.size == 123456
|
@@ -274,16 +276,20 @@ def good_datadict_for_input_observation(mock_uploadedFile) -> dict:
|
|
274 |
# set up the good and bad inputs
|
275 |
_date="2023-10-10"
|
276 |
_time="10:10:10"
|
277 |
-
|
|
|
|
|
278 |
fname = "test_image.jpg"
|
279 |
image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
280 |
|
281 |
-
dt_ok = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S")
|
|
|
282 |
valid_inputs = {
|
283 |
"author_email": "[email protected]",
|
284 |
"uploaded_file": mock_uploadedFile(name=fname).get_data(),
|
285 |
"date": dt_ok.date(),
|
286 |
"time": dt_ok.time(),
|
|
|
287 |
"image": image,
|
288 |
"image_md5": 'd1d2515e6f6ac4c5ca6dd739d5143cd4', # 32 hex chars.
|
289 |
"image_datetime_raw": image_datetime_raw,
|
|
|
89 |
|
90 |
_date="2023-10-10"
|
91 |
_time="10:10:10"
|
92 |
+
_timezone = "+04:00"
|
93 |
+
image_datetime_raw = _date + " " + _time + " " + _timezone
|
94 |
+
dt = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S %z")
|
95 |
date = dt.date()
|
96 |
time = dt.time()
|
97 |
+
tz_str = dt.strftime('%z')
|
98 |
## make a random image with dtype uint8 using np.random.randint
|
99 |
image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
100 |
image_md5 = 'd1d2515e6f6ac4c5ca6dd739d5143cd4' # 32 hex chars.
|
|
|
102 |
obs = InputObservation(
|
103 |
image=image,
|
104 |
latitude=12.34, longitude=56.78, author_email=author_email,
|
105 |
+
time=time, date=date, timezone=tz_str,
|
106 |
uploaded_file=mock_file,
|
107 |
image_md5=image_md5,
|
108 |
)
|
|
|
117 |
assert isinstance(obs.time, datetime.time)
|
118 |
assert str(obs.date) == "2023-10-10"
|
119 |
assert str(obs.time) == "10:10:10"
|
120 |
+
assert obs.timezone == tz_str
|
121 |
|
122 |
assert obs.uploaded_file.name == image_name
|
123 |
assert obs.uploaded_file.size == 123456
|
|
|
276 |
# set up the good and bad inputs
|
277 |
_date="2023-10-10"
|
278 |
_time="10:10:10"
|
279 |
+
_timezone = "+04:00"
|
280 |
+
image_datetime_raw = _date + " " + _time + " " + _timezone
|
281 |
+
#dt = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S %z")
|
282 |
fname = "test_image.jpg"
|
283 |
image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
284 |
|
285 |
+
dt_ok = datetime.datetime.strptime(image_datetime_raw, "%Y-%m-%d %H:%M:%S %z")
|
286 |
+
tz_str = dt_ok.strftime('%z')
|
287 |
valid_inputs = {
|
288 |
"author_email": "[email protected]",
|
289 |
"uploaded_file": mock_uploadedFile(name=fname).get_data(),
|
290 |
"date": dt_ok.date(),
|
291 |
"time": dt_ok.time(),
|
292 |
+
"timezone": tz_str,
|
293 |
"image": image,
|
294 |
"image_md5": 'd1d2515e6f6ac4c5ca6dd739d5143cd4', # 32 hex chars.
|
295 |
"image_datetime_raw": image_datetime_raw,
|