File size: 6,374 Bytes
f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb f19bc2d a3786fb |
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 136 137 138 139 140 141 142 143 144 |
import gradio as gr
import pandas as pd
import numpy as np
from dotenv import load_dotenv
from utils_config import load_config
from utils_json import get_json_one_individual, save_to_all_individuals
import os
load_dotenv()
PATH = os.getcwd() + "/"
PATH_ASSETS = os.getenv('PATH_ASSETS')
PATH_CONFIG = PATH + PATH_ASSETS + "config/"
def get_headers():
headers_config = load_config(PATH_CONFIG + "config_headers.json")
headers = headers_config["headers"]
return headers
def get_fields():
fields_config = load_config(PATH_CONFIG + "config_fields.json")
fields = fields_config["fields"]
return fields
def match_data_to_fields(fields, one_individual):
new_row = {}
for key in fields:
if key in one_individual:
if key=="image":
new_row[key] = one_individual[key]
elif type(one_individual[key])==list:
new_row[key] = ' , '.join(one_individual[key])
else:
new_row[key] = one_individual[key]
else:
new_row[key] = "NA"
return new_row
def process_animals(all_animals):
processed_animals = []
print(len(all_animals))
for _, animal in all_animals.items():
image = np.array(animal["image"])
caption = []
for key, val in animal.items():
if key!="image":
if key=="latitude":
caption.extend([
" | Latitude: " + str(animal["latitude"])])
elif key=="longitude":
caption.extend([
" | Longitude: " + str(animal["longitude"])])
elif key=="wounded" and val=="True":
caption.extend([" | Wounded: " + animal["wounded"]])
elif key=="dead" and val=="True":
caption.extend([" | Dead: " + animal["dead"]])
elif key=="circumstance":
caption.extend([" | Circumstances: " ,
animal["circumstance"],
animal["circumstance_dropdown_level1"],
animal["circumstance_dropdown_level2"],
animal["circumstance_openfield_level2"],
animal["circumstance_dropdown_extra_level2"]])
elif key=="behavior":
caption.extend([" | Behavior: ", animal[key]])
elif "physical_changes" in key:
if not(" | Physical Changes: " in caption) :
caption.extend([" | Physical Changes: ",
"Beak: " + animal["physical_changes_beak"],
"Body: " + animal["physical_changes_body"],
"Head: " + animal["physical_changes_head"],
"Feathers: " + animal["physical_changes_feathers"],
"Legs: " + animal["physical_changes_legs"]])
caption_str = " ".join(caption)
animal = (image, caption_str)
processed_animals.append(animal)
return processed_animals
def set_gallery_size(len_animals):
if len_animals < 10:
num_cols=5
num_rows=2
else:
num_cols = len_animals/2
num_rows = len_animals/(num_cols)
return num_cols, num_rows
def save_individual_to_gallery(gallery):
one_individual = get_json_one_individual()
fields = get_fields()
one_individual_matched = match_data_to_fields(fields, one_individual)
all_animals = save_to_all_individuals(one_individual_matched)
num_cols, num_rows = set_gallery_size(len(all_animals))
processed_animals = process_animals(all_animals)
gallery = gr.Gallery(
label="Gallery of Records", elem_id="gallery",
columns=[num_cols], rows=[num_rows],
value=processed_animals,
object_fit="contain", height="auto", interactive=False)
return gallery
# def save_individual_to_df(df):
# fields = get_fields()
# one_individual = get_json_one_individual()
# headers = get_headers()
# new_row = match_data_to_fields(fields, one_individual)
# new_row = format_row(new_row, headers)
# new_row_df = pd.DataFrame([new_row], columns=headers)
# df_new = pd.concat([df, new_row_df], ignore_index=True)
# df_gr = gr.DataFrame(visible=True,
# value=df_new,
# headers=headers)
# return df_gr
# def save_and_rest_df(df):
# save_all_animals(df)
# df = gr.Dataframe(fields=get_fields(),
# visible=False)
# return df
# def format_row(new_row, headers):
# formatted_row = {}
# #formatted_row["image"] = new_row["image"]
# for header in headers:
# if header=="location":
# formatted_row[header] = " | ".join(["Latitude: " + str(new_row["latitude"]),
# "Longitude: " + str(new_row["longitude"])])
# elif header=="state":
# formatted_row[header] = " | ".join(["Wounded: " + new_row["wounded"],
# "Dead: " + new_row["dead"]])
# elif header=="circumstance":
# formatted_row[header] = " | ".join([new_row["circumstance"],
# new_row["circumstance_dropdown_level1"],
# new_row["circumstance_dropdown_level2"],
# new_row["circumstance_openfield_level2"],
# new_row["circumstance_dropdown_extra_level2"]])
# elif header=="behavior":
# formatted_row[header] = new_row[header]
# elif header=="physical_changes":
# formatted_row[header] = " | ".join([new_row["physical_changes_beak"],
# new_row["physical_changes_body"],
# new_row["physical_changes_head"],
# new_row["physical_changes_feathers"],
# new_row["physical_changes_legs"]])
# return list(formatted_row.values())
|