Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
8f958ed
1
Parent(s):
5cb2d26
chore: cleaning up InputObservation (added type hints)
Browse filessee issue #27.
- added typehints (6) for all arguments, except (date, time) which
always seem to be none, and need investigation to resolve (see 2)
- updated the attribute uploaded_filename to uploaded_file, since it
is *not* a filename, but a BytesIO-like object, `UploadedFile`.
- src/input/input_handling.py +2 -2
- src/input/input_observation.py +27 -22
src/input/input_handling.py
CHANGED
|
@@ -155,7 +155,7 @@ def process_one_file(file:UploadedFile, ix:int=0) -> Tuple[np.ndarray, str, str,
|
|
| 155 |
observation = InputObservation(image=file, latitude=latitude, longitude=longitude,
|
| 156 |
author_email=author_email, date=image_datetime, time=None,
|
| 157 |
date_option=date_option, time_option=time_option,
|
| 158 |
-
|
| 159 |
)
|
| 160 |
|
| 161 |
#the_data = [] \
|
|
@@ -306,7 +306,7 @@ def metadata_inputs_one_file(file:UploadedFile, image_hash:str, dbg_ix:int=0) ->
|
|
| 306 |
observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
|
| 307 |
author_email=author_email, date=image_datetime, time=None,
|
| 308 |
date_option=date_option, time_option=time_option,
|
| 309 |
-
|
| 310 |
)
|
| 311 |
|
| 312 |
# TODO: pass in the hash to InputObservation, so it is done once only. (need to refactor the class a bit)
|
|
|
|
| 155 |
observation = InputObservation(image=file, latitude=latitude, longitude=longitude,
|
| 156 |
author_email=author_email, date=image_datetime, time=None,
|
| 157 |
date_option=date_option, time_option=time_option,
|
| 158 |
+
uploaded_file=file,
|
| 159 |
)
|
| 160 |
|
| 161 |
#the_data = [] \
|
|
|
|
| 306 |
observation = InputObservation(image=image, latitude=latitude, longitude=longitude,
|
| 307 |
author_email=author_email, date=image_datetime, time=None,
|
| 308 |
date_option=date_option, time_option=time_option,
|
| 309 |
+
uploaded_file=file, image_md5=image_hash
|
| 310 |
)
|
| 311 |
|
| 312 |
# TODO: pass in the hash to InputObservation, so it is done once only. (need to refactor the class a bit)
|
src/input/input_observation.py
CHANGED
|
@@ -7,7 +7,7 @@ class InputObservation:
|
|
| 7 |
A class to hold an input observation and associated metadata
|
| 8 |
|
| 9 |
Attributes:
|
| 10 |
-
image (
|
| 11 |
The image associated with the observation.
|
| 12 |
latitude (float):
|
| 13 |
The latitude where the observation was made.
|
|
@@ -15,16 +15,16 @@ class InputObservation:
|
|
| 15 |
The longitude where the observation was made.
|
| 16 |
author_email (str):
|
| 17 |
The email of the author of the observation.
|
| 18 |
-
date (
|
| 19 |
The date when the observation was made.
|
| 20 |
-
time (
|
| 21 |
The time when the observation was made.
|
| 22 |
-
date_option (
|
| 23 |
Additional date option for the observation.
|
| 24 |
-
time_option (
|
| 25 |
Additional time option for the observation.
|
| 26 |
-
|
| 27 |
-
The uploaded
|
| 28 |
image_md5 (str):
|
| 29 |
The MD5 hash of the image associated with the observation.
|
| 30 |
|
|
@@ -37,6 +37,8 @@ class InputObservation:
|
|
| 37 |
Checks if two observations are equal.
|
| 38 |
__ne__(other):
|
| 39 |
Checks if two observations are not equal.
|
|
|
|
|
|
|
| 40 |
to_dict():
|
| 41 |
Converts the observation to a dictionary.
|
| 42 |
from_dict(data):
|
|
@@ -47,9 +49,12 @@ class InputObservation:
|
|
| 47 |
|
| 48 |
_inst_count = 0
|
| 49 |
|
| 50 |
-
def __init__(
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
self.image = image
|
| 54 |
self.latitude = latitude
|
| 55 |
self.longitude = longitude
|
|
@@ -58,7 +63,7 @@ class InputObservation:
|
|
| 58 |
self.time = time
|
| 59 |
self.date_option = date_option
|
| 60 |
self.time_option = time_option
|
| 61 |
-
self.
|
| 62 |
self.image_md5 = image_md5
|
| 63 |
self._top_predictions = []
|
| 64 |
|
|
@@ -80,10 +85,10 @@ class InputObservation:
|
|
| 80 |
def assign_image_md5(self):
|
| 81 |
raise DeprecationWarning("This method is deprecated. hash is a required constructor argument.")
|
| 82 |
if not self.image_md5:
|
| 83 |
-
self.image_md5 = hashlib.md5(self.
|
| 84 |
|
| 85 |
# new comment / hybj hunk
|
| 86 |
-
self._cprint(f"[D] Assigned image md5: {self.image_md5} for {self.
|
| 87 |
|
| 88 |
def _cprint(self, msg:str, color:str=OKGREEN):
|
| 89 |
"""Print colored message"""
|
|
@@ -94,7 +99,7 @@ class InputObservation:
|
|
| 94 |
return (
|
| 95 |
f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
|
| 96 |
f"{self.author_email}, {self.date}, {self.time}, {self.date_option}, "
|
| 97 |
-
f"{self.time_option}, {self.
|
| 98 |
)
|
| 99 |
|
| 100 |
def __repr__(self):
|
|
@@ -109,7 +114,7 @@ class InputObservation:
|
|
| 109 |
f"Time: {self.time}, "
|
| 110 |
f"Date Option: {self.date_option}, "
|
| 111 |
f"Time Option: {self.time_option}, "
|
| 112 |
-
f"Uploaded Filename: {self.
|
| 113 |
f"Image MD5 hash: {self.image_md5}"
|
| 114 |
)
|
| 115 |
|
|
@@ -132,7 +137,7 @@ class InputObservation:
|
|
| 132 |
self.date_option == other.date_option and
|
| 133 |
# temporarily skip time_option, it is followed by the clock and that is always differnt
|
| 134 |
#self.time_option == other.time_option and
|
| 135 |
-
self.
|
| 136 |
self.image_md5 == other.image_md5
|
| 137 |
)
|
| 138 |
return equality
|
|
@@ -165,7 +170,7 @@ class InputObservation:
|
|
| 165 |
differences.append(f" Date option is different. (self: {self.date_option}, other: {other.date_option})")
|
| 166 |
if self.time_option != other.time_option:
|
| 167 |
differences.append(f" Time option is different. (self: {self.time_option}, other: {other.time_option})")
|
| 168 |
-
if self.
|
| 169 |
differences.append(" Uploaded filename is different.")
|
| 170 |
if self.image_md5 != other.image_md5:
|
| 171 |
differences.append(" Image MD5 hash is different.")
|
|
@@ -183,9 +188,9 @@ class InputObservation:
|
|
| 183 |
def to_dict(self):
|
| 184 |
return {
|
| 185 |
#"image": self.image,
|
| 186 |
-
"image_filename": self.
|
| 187 |
"image_md5": self.image_md5,
|
| 188 |
-
#"image_md5": hashlib.md5(self.
|
| 189 |
"latitude": self.latitude,
|
| 190 |
"longitude": self.longitude,
|
| 191 |
"author_email": self.author_email,
|
|
@@ -193,7 +198,7 @@ class InputObservation:
|
|
| 193 |
"time": self.time,
|
| 194 |
"date_option": str(self.date_option),
|
| 195 |
"time_option": str(self.time_option),
|
| 196 |
-
"
|
| 197 |
}
|
| 198 |
|
| 199 |
@classmethod
|
|
@@ -207,7 +212,7 @@ class InputObservation:
|
|
| 207 |
time=data.get("time"),
|
| 208 |
date_option=data.get("date_option"),
|
| 209 |
time_option=data.get("time_option"),
|
| 210 |
-
|
| 211 |
image_hash=data.get("image_md5")
|
| 212 |
)
|
| 213 |
|
|
@@ -222,7 +227,7 @@ class InputObservation:
|
|
| 222 |
time=input.time,
|
| 223 |
date_option=input.date_option,
|
| 224 |
time_option=input.time_option,
|
| 225 |
-
|
| 226 |
image_hash=input.image_hash
|
| 227 |
)
|
| 228 |
|
|
|
|
| 7 |
A class to hold an input observation and associated metadata
|
| 8 |
|
| 9 |
Attributes:
|
| 10 |
+
image (ndarray):
|
| 11 |
The image associated with the observation.
|
| 12 |
latitude (float):
|
| 13 |
The latitude where the observation was made.
|
|
|
|
| 15 |
The longitude where the observation was made.
|
| 16 |
author_email (str):
|
| 17 |
The email of the author of the observation.
|
| 18 |
+
date (Any): # FIXME: specify the type
|
| 19 |
The date when the observation was made.
|
| 20 |
+
time (Any): # FIXME: specify the type
|
| 21 |
The time when the observation was made.
|
| 22 |
+
date_option (datetime.date):
|
| 23 |
Additional date option for the observation.
|
| 24 |
+
time_option (datetime.time):
|
| 25 |
Additional time option for the observation.
|
| 26 |
+
uploaded_file (UploadedFile):
|
| 27 |
+
The uploaded file associated with the observation.
|
| 28 |
image_md5 (str):
|
| 29 |
The MD5 hash of the image associated with the observation.
|
| 30 |
|
|
|
|
| 37 |
Checks if two observations are equal.
|
| 38 |
__ne__(other):
|
| 39 |
Checks if two observations are not equal.
|
| 40 |
+
show_diff(other):
|
| 41 |
+
Shows the differences between two observations.
|
| 42 |
to_dict():
|
| 43 |
Converts the observation to a dictionary.
|
| 44 |
from_dict(data):
|
|
|
|
| 49 |
|
| 50 |
_inst_count = 0
|
| 51 |
|
| 52 |
+
def __init__(
|
| 53 |
+
self, image:ndarray=None, latitude:float=None, longitude:float=None,
|
| 54 |
+
author_email:str=None, date=None, time=None, date_option:datetime.date=None,
|
| 55 |
+
time_option:datetime.time=None,
|
| 56 |
+
uploaded_file:UploadedFile=None, image_md5:str=None):
|
| 57 |
+
|
| 58 |
self.image = image
|
| 59 |
self.latitude = latitude
|
| 60 |
self.longitude = longitude
|
|
|
|
| 63 |
self.time = time
|
| 64 |
self.date_option = date_option
|
| 65 |
self.time_option = time_option
|
| 66 |
+
self.uploaded_file = uploaded_file
|
| 67 |
self.image_md5 = image_md5
|
| 68 |
self._top_predictions = []
|
| 69 |
|
|
|
|
| 85 |
def assign_image_md5(self):
|
| 86 |
raise DeprecationWarning("This method is deprecated. hash is a required constructor argument.")
|
| 87 |
if not self.image_md5:
|
| 88 |
+
self.image_md5 = hashlib.md5(self.uploaded_file.read()).hexdigest() if self.uploaded_file else generate_random_md5()
|
| 89 |
|
| 90 |
# new comment / hybj hunk
|
| 91 |
+
self._cprint(f"[D] Assigned image md5: {self.image_md5} for {self.uploaded_file}")
|
| 92 |
|
| 93 |
def _cprint(self, msg:str, color:str=OKGREEN):
|
| 94 |
"""Print colored message"""
|
|
|
|
| 99 |
return (
|
| 100 |
f"Observation: {_im_str}, {self.latitude}, {self.longitude}, "
|
| 101 |
f"{self.author_email}, {self.date}, {self.time}, {self.date_option}, "
|
| 102 |
+
f"{self.time_option}, {self.uploaded_file}, {self.image_md5}"
|
| 103 |
)
|
| 104 |
|
| 105 |
def __repr__(self):
|
|
|
|
| 114 |
f"Time: {self.time}, "
|
| 115 |
f"Date Option: {self.date_option}, "
|
| 116 |
f"Time Option: {self.time_option}, "
|
| 117 |
+
f"Uploaded Filename: {self.uploaded_file}"
|
| 118 |
f"Image MD5 hash: {self.image_md5}"
|
| 119 |
)
|
| 120 |
|
|
|
|
| 137 |
self.date_option == other.date_option and
|
| 138 |
# temporarily skip time_option, it is followed by the clock and that is always differnt
|
| 139 |
#self.time_option == other.time_option and
|
| 140 |
+
self.uploaded_file == other.uploaded_file and
|
| 141 |
self.image_md5 == other.image_md5
|
| 142 |
)
|
| 143 |
return equality
|
|
|
|
| 170 |
differences.append(f" Date option is different. (self: {self.date_option}, other: {other.date_option})")
|
| 171 |
if self.time_option != other.time_option:
|
| 172 |
differences.append(f" Time option is different. (self: {self.time_option}, other: {other.time_option})")
|
| 173 |
+
if self.uploaded_file != other.uploaded_file:
|
| 174 |
differences.append(" Uploaded filename is different.")
|
| 175 |
if self.image_md5 != other.image_md5:
|
| 176 |
differences.append(" Image MD5 hash is different.")
|
|
|
|
| 188 |
def to_dict(self):
|
| 189 |
return {
|
| 190 |
#"image": self.image,
|
| 191 |
+
"image_filename": self.uploaded_file.name if self.uploaded_file else None,
|
| 192 |
"image_md5": self.image_md5,
|
| 193 |
+
#"image_md5": hashlib.md5(self.uploaded_file.read()).hexdigest() if self.uploaded_file else generate_random_md5(),
|
| 194 |
"latitude": self.latitude,
|
| 195 |
"longitude": self.longitude,
|
| 196 |
"author_email": self.author_email,
|
|
|
|
| 198 |
"time": self.time,
|
| 199 |
"date_option": str(self.date_option),
|
| 200 |
"time_option": str(self.time_option),
|
| 201 |
+
"uploaded_file": self.uploaded_file
|
| 202 |
}
|
| 203 |
|
| 204 |
@classmethod
|
|
|
|
| 212 |
time=data.get("time"),
|
| 213 |
date_option=data.get("date_option"),
|
| 214 |
time_option=data.get("time_option"),
|
| 215 |
+
uploaded_file=data.get("uploaded_file"),
|
| 216 |
image_hash=data.get("image_md5")
|
| 217 |
)
|
| 218 |
|
|
|
|
| 227 |
time=input.time,
|
| 228 |
date_option=input.date_option,
|
| 229 |
time_option=input.time_option,
|
| 230 |
+
uploaded_file=input.uploaded_file,
|
| 231 |
image_hash=input.image_hash
|
| 232 |
)
|
| 233 |
|