Skip to content

Commit

Permalink
changed getExampleDrawings websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasrberg committed Jul 17, 2024
1 parent 148b41d commit 4787484
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/webapp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def get_example_drawings(json_data):
if (lang == "NO"):
label = models.to_english(label)

example_drawings = storage.get_n_random_images_from_label(
number_of_images, label)
example_drawing_urls = models.get_n_random_example_images(label, number_of_images)
example_drawings = storage.get_images_from_relative_url(example_drawing_urls)
emit("getExampleDrawings", json.dumps(example_drawings), room=game_id)


Expand Down
58 changes: 58 additions & 0 deletions src/webapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class Difficulty(db.Model):
id = db.Column(db.Integer, primary_key=True)
difficulty = db.Column(db.String(32), nullable=False)

class ExampleImages(db.Model):
"""
Model for storing example image urls that the model has predicted correctly.
"""
image = db.Column(db.String(256), primary_key=True)
label = db.Column(db.String(32), db.ForeignKey("labels.english"))


# Functions to manipulate the tables above
def create_tables(app):
Expand Down Expand Up @@ -581,3 +588,54 @@ def get_translation_dict():
)
except Exception as e:
raise Exception("Could not read Labels table: " + str(e))

def insert_into_example_images(images, label):
"""
Insert values into ExampleImages table.
"""
if isinstance(images, list) and isinstance(label, str):
try:
for image in images:
example_image = ExampleImages(image=image, label=label)
db.session.add(example_image)
db.session.commit()
except Exception as e:
raise Exception(
"Could not insert into ExampleImages table: " + str(e))
else:
raise UserError("Images must be a list and label must be a string")


def get_n_random_example_images(label, number_of_images):
"""
Returns n random example images for the given label.
"""
try:
example_images = ExampleImages.query.filter_by(label=label).all()
selected_images = random.sample(
example_images, min(
number_of_images, len(example_images)))
images = [image.image for image in selected_images]
return images
except Exception as e:
raise Exception("Could not read ExampleImages table: " + str(e)
)


def populate_example_images(app):
"""
Function for populating example images table with exported csv data. Used so you dont need to
run the prediction job twice
"""
with app.app_context():
try:
# read all rows from safe_images.csv
with open("safe_images.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
for row in readCSV:
example_image = ExampleImages(image=row[0], label=row[1])
db.session.add(example_image)
db.session.commit()
except Exception as e:
raise Exception(
"Could not insert into ExampleImages table: " + str(e))
15 changes: 15 additions & 0 deletions src/webapp/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,18 @@ def image_to_data_url(image_data, content_type):
"""
base64_image = base64.b64encode(image_data).decode('utf-8')
return f"data:{content_type};base64,{base64_image}"

def get_images_from_relative_url(image_urls):
"""
Returns a list of images from a list of relative URLs.
"""
container_client = blob_connection(setup.CONTAINER_NAME_ORIGINAL)
images = []
for image in image_urls:
blob_client = container_client.get_blob_client(image)

image_data = blob_client.download_blob().readall()
decoded_image = image_to_data_url(
image_data, "application/octet-stream")
images.append(decoded_image)
return images

0 comments on commit 4787484

Please sign in to comment.