skylord commited on
Commit
ec1de2c
·
1 Parent(s): bdcb1bf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from io import StringIO
3
+ import pandas as pd
4
+ import requests
5
+ from tabulate import tabulate
6
+ from requests.compat import urljoin as urlj
7
+ import json
8
+ from IPython.display import Image, display
9
+ import cv2
10
+ import ast
11
+ import gradio as gr
12
+ from PIL import Image
13
+ import os
14
+ import requests
15
+
16
+
17
+ API_TOKEN_FROM_YOUR_PROFILE = '0953ed57513ab57a7dccbc6859472657383dd56b'
18
+ token = API_TOKEN_FROM_YOUR_PROFILE
19
+
20
+ def request_data(url):
21
+ auth_header = {"Authorization": f"Token {token}"}
22
+
23
+
24
+ r = requests.get(url, headers=auth_header)
25
+ req = json.loads(r.text)
26
+ return req
27
+
28
+ projects_url = "https://sweden.trapper-project.org/media_classification/api/projects"
29
+ projects = request_data(projects_url)
30
+
31
+ p13_df = pd.read_csv('observations_0_13.csv')
32
+ p6_df = pd.read_csv('observations_0_6.csv')
33
+ p2_df = pd.read_csv('observations_0_2.csv')
34
+
35
+ def display_image(file_path, width=1000, height=None):
36
+ print(file_path)
37
+ if os.path.exists(file_path):
38
+ display(Image(file_path, width=width, height=height))
39
+ else:
40
+ print('File not in dir (yet)')
41
+
42
+ def draw_bboxs(img_path, coordinates, out_path=None, display_now=False):
43
+ image = cv2.imread(img_path)
44
+ height, width, channels = image.shape
45
+ for c in coordinates:
46
+ start_point = (int(c[0]*width), int(c[1]*height))
47
+ #end_point = (int(c[2]*width), int(c[3]*height))
48
+ end_point = (int(c[0]*width)+ int(c[2]*width), int(c[1]*height) + int(c[3]*height))
49
+ cv2.rectangle(image, start_point, end_point, color=(0,255,0), thickness=2)
50
+ if not out_path:
51
+ out_path = img_path.replace('.', '_bboxes.')
52
+
53
+ cv2.imwrite(out_path, image)
54
+
55
+ if display_now:
56
+ display_image(out_path)
57
+ return out_path
58
+
59
+ def select_by_species(df, species):
60
+ return df[df['commonName'] == species]
61
+
62
+ def show_random_species(df, species):
63
+ display_now = True
64
+ sdf = select_by_species(df, species)
65
+ if sdf.empty:
66
+ print('Species not in available photo set')
67
+ row = sdf.sample(n=None)
68
+ print(row.to_markdown())
69
+ row = row.squeeze()
70
+ photo_path = download_image_url(row.filePath, f'{row.fileName}.png', display_now=display_now)
71
+ row['local_path'] = photo_path
72
+ row['bboxes'] = ast.literal_eval(row['bboxes'])
73
+ return row
74
+
75
+ def download_image_url(url, file_name, file_path='photos', display_now=False):
76
+ photo_path = os.path.join(file_path, file_name)
77
+ auth_header = {"Authorization": f"Token {token}"}
78
+ r = requests.get(url, headers=auth_header)
79
+
80
+ if r.status_code == 200:
81
+ os.makedirs(file_path, exist_ok=True)
82
+ with open(photo_path, 'wb') as out_file:
83
+ out_file.write(r.content)
84
+ else:
85
+ print(f"ERROR code: {r.status_code} on URL: {url}")
86
+ return None
87
+
88
+ if display_now:
89
+ display_image(photo_path)
90
+
91
+ return photo_path
92
+
93
+ def display_image(photo_path):
94
+ image = Image.open(photo_path)
95
+ image.show()
96
+ return image
97
+
98
+ def outputImg(df_name, species):
99
+ print("df_name", df_name)
100
+ if df_name == "p13_df":
101
+ df = p13_df
102
+ elif df_name == "p6_df":
103
+ df = p6_df
104
+ elif df_name == "p2_df":
105
+ df = p2_df
106
+ else:
107
+ print("Invalid DataFrame selected")
108
+ return None
109
+ image_info = show_random_species(df, species)
110
+ print(image_info['local_path'], image_info.to_string())
111
+ details_table = tabulate(pd.DataFrame(image_info).transpose(), headers='keys', tablefmt='pipe')
112
+ print("details_table",details_table)
113
+ bbimg = draw_bboxs(image_info['local_path'], image_info.bboxes)#image_info['local_path'], image_info.to_string()
114
+ return display_image(bbimg), details_table#image_info.to_string()
115
+
116
+ iface = gr.Interface(
117
+ fn=outputImg,
118
+ inputs=[
119
+ gr.inputs.Dropdown(label="df", choices=["p13_df", "p6_df", "p2_df"]),
120
+ gr.inputs.Dropdown(label="species", choices=list(pd.concat([p13_df['commonName'], p6_df['commonName'], p2_df['commonName']]).unique())),
121
+ #gr.inputs.Textbox(label="DataFrame")
122
+ ],
123
+ outputs=[
124
+ gr.outputs.Image(type="pil", label="Random Image"),
125
+ gr.outputs.Textbox(label="Details", type="text")
126
+ ]
127
+ )
128
+
129
+ iface.launch()