Spaces:
Running
Running
File size: 5,637 Bytes
55d18b1 895e8d8 55d18b1 895e8d8 ba36d57 895e8d8 ba36d57 895e8d8 55d18b1 895e8d8 55d18b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import hashlib
from input.input_validator import generate_random_md5
# autogenerated class to hold the input data
class InputObservation:
"""
A class to hold an input observation and associated metadata
Attributes:
image (Any):
The image associated with the observation.
latitude (float):
The latitude where the observation was made.
longitude (float):
The longitude where the observation was made.
author_email (str):
The email of the author of the observation.
date (str):
The date when the observation was made.
time (str):
The time when the observation was made.
date_option (str):
Additional date option for the observation.
time_option (str):
Additional time option for the observation.
uploaded_filename (Any):
The uploaded filename associated with the observation.
Methods:
__str__():
Returns a string representation of the observation.
__repr__():
Returns a string representation of the observation.
__eq__(other):
Checks if two observations are equal.
__ne__(other):
Checks if two observations are not equal.
__hash__():
Returns the hash of the observation.
to_dict():
Converts the observation to a dictionary.
from_dict(data):
Creates an observation from a dictionary.
from_input(input):
Creates an observation from another input observation.
"""
_inst_count = 0
def __init__(self, image=None, latitude=None, longitude=None,
author_email=None, date=None, time=None, date_option=None, time_option=None,
uploaded_filename=None):
self.image = image
self.latitude = latitude
self.longitude = longitude
self.author_email = author_email
self.date = date
self.time = time
self.date_option = date_option
self.time_option = time_option
self.uploaded_filename = uploaded_filename
self._image_md5 = None
self._top_predictions = []
InputObservation._inst_count += 1
self._inst_id = InputObservation._inst_count
self.assign_image_md5()
def set_top_predictions(self, top_predictions:list):
self._top_predictions = top_predictions
# add a method to get the top predictions (property?)
@property
def top_predictions(self):
return self._top_predictions
# add a method to assign the image_md5 only once
def assign_image_md5(self):
if not self._image_md5:
self._image_md5 = hashlib.md5(self.uploaded_filename.read()).hexdigest() if self.uploaded_filename else generate_random_md5()
def __str__(self):
return f"Observation: {self.image}, {self.latitude}, {self.longitude}, {self.author_email}, {self.date}, {self.time}, {self.date_option}, {self.time_option}, {self.uploaded_filename}"
def __repr__(self):
return f"Observation: {self.image}, {self.latitude}, {self.longitude}, {self.author_email}, {self.date}, {self.time}, {self.date_option}, {self.time_option}, {self.uploaded_filename}"
def __eq__(self, other):
return (self.image == other.image and self.latitude == other.latitude and self.longitude == other.longitude and
self.author_email == other.author_email and self.date == other.date and self.time == other.time and
self.date_option == other.date_option and self.time_option == other.time_option and self.uploaded_filename == other.uploaded_filename)
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash((self.image, self.latitude, self.longitude, self.author_email, self.date, self.time, self.date_option, self.time_option, self.uploaded_filename))
def to_dict(self):
return {
#"image": self.image,
"image_filename": self.uploaded_filename.name if self.uploaded_filename else None,
"image_md5": self._image_md5,
#"image_md5": hashlib.md5(self.uploaded_filename.read()).hexdigest() if self.uploaded_filename else generate_random_md5(),
"latitude": self.latitude,
"longitude": self.longitude,
"author_email": self.author_email,
"date": self.date,
"time": self.time,
"date_option": str(self.date_option),
"time_option": str(self.time_option),
"uploaded_filename": self.uploaded_filename
}
@classmethod
def from_dict(cls, data):
return cls(data["image"], data["latitude"], data["longitude"], data["author_email"], data["date"], data["time"], data["date_option"], data["time_option"], data["uploaded_filename"])
@classmethod
def from_input(cls, input):
return cls(input.image, input.latitude, input.longitude, input.author_email, input.date, input.time, input.date_option, input.time_option, input.uploaded_filename)
@staticmethod
def from_input(input):
return InputObservation(input.image, input.latitude, input.longitude, input.author_email, input.date, input.time, input.date_option, input.time_option, input.uploaded_filename)
@staticmethod
def from_dict(data):
return InputObservation(data["image"], data["latitude"], data["longitude"], data["author_email"], data["date"], data["time"], data["date_option"], data["time_option"], data["uploaded_filename"])
|