vancauwe commited on
Commit
a3786fb
·
1 Parent(s): cf0242a

feat: pictures as a gallery

Browse files
app/assets/config/{config_df.json → config_fields.json} RENAMED
@@ -1,6 +1,5 @@
1
  {
2
- "headers": [
3
- "image",
4
  "latitude",
5
  "longitude",
6
  "wounded",
@@ -15,6 +14,7 @@
15
  "physical_changes_body",
16
  "physical_changes_head",
17
  "physical_changes_feathers",
18
- "physical_changes_legs"
 
19
  ]
20
  }
 
1
  {
2
+ "fields": [
 
3
  "latitude",
4
  "longitude",
5
  "wounded",
 
14
  "physical_changes_body",
15
  "physical_changes_head",
16
  "physical_changes_feathers",
17
+ "physical_changes_legs",
18
+ "image"
19
  ]
20
  }
app/main_multianimal.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
  from gradio_modal import Modal
3
 
4
- from utils_df import get_headers
5
  from utils_json import *
6
- from utils_df import save_individual_to_df, get_headers, save_and_rest_df
7
  from maps import get_location
8
  from functools import partial
9
  from dead import show_section_dead
@@ -17,11 +16,16 @@ from style import *
17
  from theme import theme, css
18
 
19
  with gr.Blocks(theme=theme, css=css) as demo:
 
20
  with gr.Row():
21
  show_modal = gr.Button("Add an Animal", scale=3)
22
  submit_button = gr.Button("Submit All Animals", scale=1)
23
- df = gr.Dataframe(headers=get_headers(),
24
- visible=False)
 
 
 
 
25
  with Modal(visible=False) as modal:
26
  # ---------------------------------------------------------
27
  # Intro Text
@@ -33,9 +37,11 @@ with gr.Blocks(theme=theme, css=css) as demo:
33
  # ---------------------------------------------------------
34
  # Camera
35
  with gr.Row():
36
- #with gr.Column(scale=1):
 
 
37
  camera = gr.Image(elem_id="image")
38
-
39
  # ---------------------------------------------------------
40
  # Location
41
  #with gr.Row():
@@ -187,6 +193,7 @@ with gr.Blocks(theme=theme, css=css) as demo:
187
  button_df = gr.Button("Submit Animal Report", scale = 3)
188
  button_clear = gr.ClearButton(scale = 1,
189
  components=[
 
190
  location, identified_location,
191
  button_collision_dead, button_deliberate_destruction_dead, button_indirect_destruction_dead, button_natural_cause_dead,
192
  dropdown_dead, dropdown_level2_dead, openfield_level2_dead, dropdown_extra_level2_dead,
@@ -201,17 +208,20 @@ with gr.Blocks(theme=theme, css=css) as demo:
201
  button_clear.click()
202
  button_clear.click(hide_physical,
203
  outputs=[checkbox_beak, text_beak, checkbox_body, text_body, checkbox_feathers, text_feathers, checkbox_head, text_head, checkbox_legs, text_legs])
204
- button_df.click(save_individual_to_df,
205
- inputs=[df],
206
- outputs=[df])
 
 
 
207
  button_df.click(lambda: Modal(visible=False), None, modal)
208
 
209
  # ---------------------------------------------------------
210
  # Event Functions of the landing page buttons
211
  show_modal.click(lambda: Modal(visible=True), None, modal)
212
- show_modal.click(create_json)
213
- submit_button.click(save_and_rest_df, inputs=[df], outputs=[df])
214
-
215
 
216
 
217
 
 
1
  import gradio as gr
2
  from gradio_modal import Modal
3
 
 
4
  from utils_json import *
5
+ from utils_df import save_individual_to_gallery
6
  from maps import get_location
7
  from functools import partial
8
  from dead import show_section_dead
 
16
  from theme import theme, css
17
 
18
  with gr.Blocks(theme=theme, css=css) as demo:
19
+ create_json_all_individuals()
20
  with gr.Row():
21
  show_modal = gr.Button("Add an Animal", scale=3)
22
  submit_button = gr.Button("Submit All Animals", scale=1)
23
+ # df = gr.Dataframe(headers=get_headers(),
24
+ # visible=False)
25
+ gallery = gr.Gallery(
26
+ label="Gallery of Records", elem_id="gallery",
27
+ columns=[1], rows=[1],
28
+ object_fit="contain", height="auto", interactive=False)
29
  with Modal(visible=False) as modal:
30
  # ---------------------------------------------------------
31
  # Intro Text
 
37
  # ---------------------------------------------------------
38
  # Camera
39
  with gr.Row():
40
+ def save_image(camera):
41
+ add_data_to_individual("image", camera.tolist())
42
+
43
  camera = gr.Image(elem_id="image")
44
+ camera.input(save_image, inputs=[camera])
45
  # ---------------------------------------------------------
46
  # Location
47
  #with gr.Row():
 
193
  button_df = gr.Button("Submit Animal Report", scale = 3)
194
  button_clear = gr.ClearButton(scale = 1,
195
  components=[
196
+ camera,
197
  location, identified_location,
198
  button_collision_dead, button_deliberate_destruction_dead, button_indirect_destruction_dead, button_natural_cause_dead,
199
  dropdown_dead, dropdown_level2_dead, openfield_level2_dead, dropdown_extra_level2_dead,
 
208
  button_clear.click()
209
  button_clear.click(hide_physical,
210
  outputs=[checkbox_beak, text_beak, checkbox_body, text_body, checkbox_feathers, text_feathers, checkbox_head, text_head, checkbox_legs, text_legs])
211
+ # button_df.click(save_individual_to_df,
212
+ # inputs=[df],
213
+ # outputs=[df])
214
+ button_df.click(save_individual_to_gallery,
215
+ inputs=[gallery],
216
+ outputs=[gallery])
217
  button_df.click(lambda: Modal(visible=False), None, modal)
218
 
219
  # ---------------------------------------------------------
220
  # Event Functions of the landing page buttons
221
  show_modal.click(lambda: Modal(visible=True), None, modal)
222
+ show_modal.click(create_json_one_individual)
223
+ #submit_button.click(save_and_rest_df, inputs=[df], outputs=[df])
224
+ #submit_button.click(save_individual_to_gallery)
225
 
226
 
227
 
app/utils_df.py CHANGED
@@ -1,8 +1,9 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
  from dotenv import load_dotenv
4
  from utils_config import load_config
5
- from utils_json import get_json_one_individual, save_all_animals
6
  import os
7
  load_dotenv()
8
  PATH = os.getcwd() + "/"
@@ -10,35 +11,133 @@ PATH_ASSETS = os.getenv('PATH_ASSETS')
10
  PATH_CONFIG = PATH + PATH_ASSETS + "config/"
11
 
12
  def get_headers():
13
- headers_config = load_config(PATH_CONFIG + "config_df.json")
14
  headers = headers_config["headers"]
15
  return headers
16
 
17
- def match_data_to_headers(headers, one_individual):
 
 
 
 
 
18
  new_row = {}
19
- for key in headers:
20
  if key in one_individual:
21
- if type(one_individual[key])==list:
 
 
22
  new_row[key] = ' , '.join(one_individual[key])
23
  else:
24
  new_row[key] = one_individual[key]
25
  else:
26
  new_row[key] = "NA"
27
- return list(new_row.values())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- def save_individual_to_df(df):
30
- headers = get_headers()
 
 
 
 
 
 
 
 
31
  one_individual = get_json_one_individual()
32
- new_row = match_data_to_headers(headers, one_individual)
33
- new_row_df = pd.DataFrame([new_row], columns=headers)
34
- df_new = pd.concat([df, new_row_df], ignore_index=True)
35
- df_gr = gr.DataFrame(visible=True,
36
- value=df_new,
37
- headers=headers)
38
- return df_gr
39
-
40
- def save_and_rest_df(df):
41
- save_all_animals(df)
42
- df = gr.Dataframe(headers=get_headers(),
43
- visible=False)
44
- return df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import numpy as np
4
  from dotenv import load_dotenv
5
  from utils_config import load_config
6
+ from utils_json import get_json_one_individual, save_to_all_individuals
7
  import os
8
  load_dotenv()
9
  PATH = os.getcwd() + "/"
 
11
  PATH_CONFIG = PATH + PATH_ASSETS + "config/"
12
 
13
  def get_headers():
14
+ headers_config = load_config(PATH_CONFIG + "config_headers.json")
15
  headers = headers_config["headers"]
16
  return headers
17
 
18
+ def get_fields():
19
+ fields_config = load_config(PATH_CONFIG + "config_fields.json")
20
+ fields = fields_config["fields"]
21
+ return fields
22
+
23
+ def match_data_to_fields(fields, one_individual):
24
  new_row = {}
25
+ for key in fields:
26
  if key in one_individual:
27
+ if key=="image":
28
+ new_row[key] = one_individual[key]
29
+ elif type(one_individual[key])==list:
30
  new_row[key] = ' , '.join(one_individual[key])
31
  else:
32
  new_row[key] = one_individual[key]
33
  else:
34
  new_row[key] = "NA"
35
+ return new_row
36
+
37
+ def process_animals(all_animals):
38
+ processed_animals = []
39
+ print(len(all_animals))
40
+ for _, animal in all_animals.items():
41
+ image = np.array(animal["image"])
42
+ caption = []
43
+ for key, val in animal.items():
44
+ if key!="image":
45
+ if key=="latitude":
46
+ caption.extend([
47
+ " | Latitude: " + str(animal["latitude"])])
48
+ elif key=="longitude":
49
+ caption.extend([
50
+ " | Longitude: " + str(animal["longitude"])])
51
+ elif key=="wounded" and val=="True":
52
+ caption.extend([" | Wounded: " + animal["wounded"]])
53
+ elif key=="dead" and val=="True":
54
+ caption.extend([" | Dead: " + animal["dead"]])
55
+ elif key=="circumstance":
56
+ caption.extend([" | Circumstances: " ,
57
+ animal["circumstance"],
58
+ animal["circumstance_dropdown_level1"],
59
+ animal["circumstance_dropdown_level2"],
60
+ animal["circumstance_openfield_level2"],
61
+ animal["circumstance_dropdown_extra_level2"]])
62
+ elif key=="behavior":
63
+ caption.extend([" | Behavior: ", animal[key]])
64
+ elif "physical_changes" in key:
65
+ if not(" | Physical Changes: " in caption) :
66
+ caption.extend([" | Physical Changes: ",
67
+ "Beak: " + animal["physical_changes_beak"],
68
+ "Body: " + animal["physical_changes_body"],
69
+ "Head: " + animal["physical_changes_head"],
70
+ "Feathers: " + animal["physical_changes_feathers"],
71
+ "Legs: " + animal["physical_changes_legs"]])
72
+ caption_str = " ".join(caption)
73
+ animal = (image, caption_str)
74
+ processed_animals.append(animal)
75
+ return processed_animals
76
 
77
+ def set_gallery_size(len_animals):
78
+ if len_animals < 10:
79
+ num_cols=5
80
+ num_rows=2
81
+ else:
82
+ num_cols = len_animals/2
83
+ num_rows = len_animals/(num_cols)
84
+ return num_cols, num_rows
85
+
86
+ def save_individual_to_gallery(gallery):
87
  one_individual = get_json_one_individual()
88
+ fields = get_fields()
89
+ one_individual_matched = match_data_to_fields(fields, one_individual)
90
+ all_animals = save_to_all_individuals(one_individual_matched)
91
+ num_cols, num_rows = set_gallery_size(len(all_animals))
92
+ processed_animals = process_animals(all_animals)
93
+ gallery = gr.Gallery(
94
+ label="Gallery of Records", elem_id="gallery",
95
+ columns=[num_cols], rows=[num_rows],
96
+ value=processed_animals,
97
+ object_fit="contain", height="auto", interactive=False)
98
+ return gallery
99
+
100
+ # def save_individual_to_df(df):
101
+ # fields = get_fields()
102
+ # one_individual = get_json_one_individual()
103
+ # headers = get_headers()
104
+ # new_row = match_data_to_fields(fields, one_individual)
105
+ # new_row = format_row(new_row, headers)
106
+ # new_row_df = pd.DataFrame([new_row], columns=headers)
107
+ # df_new = pd.concat([df, new_row_df], ignore_index=True)
108
+ # df_gr = gr.DataFrame(visible=True,
109
+ # value=df_new,
110
+ # headers=headers)
111
+ # return df_gr
112
+
113
+ # def save_and_rest_df(df):
114
+ # save_all_animals(df)
115
+ # df = gr.Dataframe(fields=get_fields(),
116
+ # visible=False)
117
+ # return df
118
+
119
+ # def format_row(new_row, headers):
120
+ # formatted_row = {}
121
+ # #formatted_row["image"] = new_row["image"]
122
+ # for header in headers:
123
+ # if header=="location":
124
+ # formatted_row[header] = " | ".join(["Latitude: " + str(new_row["latitude"]),
125
+ # "Longitude: " + str(new_row["longitude"])])
126
+ # elif header=="state":
127
+ # formatted_row[header] = " | ".join(["Wounded: " + new_row["wounded"],
128
+ # "Dead: " + new_row["dead"]])
129
+ # elif header=="circumstance":
130
+ # formatted_row[header] = " | ".join([new_row["circumstance"],
131
+ # new_row["circumstance_dropdown_level1"],
132
+ # new_row["circumstance_dropdown_level2"],
133
+ # new_row["circumstance_openfield_level2"],
134
+ # new_row["circumstance_dropdown_extra_level2"]])
135
+ # elif header=="behavior":
136
+ # formatted_row[header] = new_row[header]
137
+ # elif header=="physical_changes":
138
+ # formatted_row[header] = " | ".join([new_row["physical_changes_beak"],
139
+ # new_row["physical_changes_body"],
140
+ # new_row["physical_changes_head"],
141
+ # new_row["physical_changes_feathers"],
142
+ # new_row["physical_changes_legs"]])
143
+ # return list(formatted_row.values())
app/utils_json.py CHANGED
@@ -1,26 +1,42 @@
1
  import json
2
  import gradio as gr
3
 
4
- def create_json(one_individual={}):
5
  # Serializing json
6
  one_individual = json.dumps(one_individual)
7
  with open("data/one_individual.json", "w") as outfile:
8
  outfile.write(one_individual)
 
 
 
 
9
 
10
  def add_data_to_individual(key, value):
11
  with open("data/one_individual.json", 'r') as openfile:
12
  one_individual = json.load(openfile)
13
  one_individual[key] = value
14
- create_json(one_individual)
15
 
16
  def get_json_one_individual():
17
  with open("data/one_individual.json", 'r') as openfile:
18
  one_individual = json.load(openfile)
19
  return one_individual
20
 
 
 
 
 
21
 
22
- def save_all_animals(df):
23
- all_animals = df.to_json(orient="records")
24
- with open("data/all_animals.json", "w") as outfile:
25
- outfile.write(all_animals)
 
 
 
 
 
 
 
 
26
 
 
1
  import json
2
  import gradio as gr
3
 
4
+ def create_json_one_individual(one_individual={}):
5
  # Serializing json
6
  one_individual = json.dumps(one_individual)
7
  with open("data/one_individual.json", "w") as outfile:
8
  outfile.write(one_individual)
9
+ def create_json_all_individuals(all_individuals={}):
10
+ all_individuals = json.dumps(all_individuals)
11
+ with open("data/all_individuals.json", "w") as outfile:
12
+ outfile.write(all_individuals)
13
 
14
  def add_data_to_individual(key, value):
15
  with open("data/one_individual.json", 'r') as openfile:
16
  one_individual = json.load(openfile)
17
  one_individual[key] = value
18
+ create_json_one_individual(one_individual)
19
 
20
  def get_json_one_individual():
21
  with open("data/one_individual.json", 'r') as openfile:
22
  one_individual = json.load(openfile)
23
  return one_individual
24
 
25
+ def get_json_all_individuals():
26
+ with open("data/all_individuals.json", "r") as openfile:
27
+ all_individuals = json.load(openfile)
28
+ return all_individuals
29
 
30
+ def save_to_all_individuals(one_individual):
31
+ all_individuals = get_json_all_individuals()
32
+ all_individuals[str(len(all_individuals))] = one_individual
33
+ all_individuals_for_json = json.dumps(all_individuals)
34
+ with open("data/all_individuals.json", "w") as outfile:
35
+ outfile.write(all_individuals_for_json)
36
+ return all_individuals
37
+
38
+ # def save_all_individuals(df):
39
+ # all_individuals = df.to_json(orient="records")
40
+ # with open("data/all_individuals.json", "w") as outfile:
41
+ # outfile.write(all_individuals)
42