ClayTreeClay commited on
Commit
0b29d84
·
1 Parent(s): 9492b61

added labels

Browse files
Files changed (1) hide show
  1. app.py +11 -16
app.py CHANGED
@@ -5,7 +5,6 @@ from pprint import pprint
5
  import gradio as gr
6
  import os
7
 
8
-
9
  API_KEY = os.environ.get('PLANT_API_KEY') # Your API_KEY from https://my.plantnet.org/account/settings
10
  PROJECT = "all"; # try specific floras: "weurope", "canada"
11
  api_endpoint = f"https://my-api.plantnet.org/v2/identify/{PROJECT}?api-key={API_KEY}&lang=zh"
@@ -15,37 +14,33 @@ def identify_plant(image_paths, organs):
15
  for image_path in image_paths:
16
  image_data = open(image_path, 'rb')
17
  files.append(('images', (image_path, image_data)))
18
-
19
  data = {'organs': organs}
20
  req = requests.Request('POST', url=api_endpoint, files=files, data=data)
21
  prepared = req.prepare()
22
-
23
  s = requests.Session()
24
  response = s.send(prepared)
25
  json_result = json.loads(response.text)
26
-
27
  # Close the opened files
28
  for _, (_, image_data) in files:
29
  image_data.close()
30
-
31
  return response.status_code, json_result
32
 
33
-
34
- def gradio_interface(files, organs):
35
- image_paths = [file.name for file in files]
36
  print(image_paths)
37
  status_code, json_result = identify_plant(image_paths, organs)
38
-
39
  return json_result.get("bestMatch",None), json_result
 
40
  with gr.Blocks() as demo:
41
- file_output = gr.File()
42
- upload_button = gr.UploadButton("Click to Upload a File", file_types=["image", "video"], file_count="multiple")
43
- organs_input = gr.CheckboxGroup(choices=["flower", "leaf", "fruit", "bark", "habit"])
44
- best_match_text = gr.Textbox()
45
- json_text = gr.JSON()
46
- upload_button.upload(gradio_interface, inputs=[upload_button,organs_input], outputs = [best_match_text,json_text])
 
 
47
 
48
- #gr.Interface(fn=gradio_interface, inputs=[image_input, organs_input], outputs=outputs).launch()
49
  demo.launch()
50
 
51
 
 
5
  import gradio as gr
6
  import os
7
 
 
8
  API_KEY = os.environ.get('PLANT_API_KEY') # Your API_KEY from https://my.plantnet.org/account/settings
9
  PROJECT = "all"; # try specific floras: "weurope", "canada"
10
  api_endpoint = f"https://my-api.plantnet.org/v2/identify/{PROJECT}?api-key={API_KEY}&lang=zh"
 
14
  for image_path in image_paths:
15
  image_data = open(image_path, 'rb')
16
  files.append(('images', (image_path, image_data)))
 
17
  data = {'organs': organs}
18
  req = requests.Request('POST', url=api_endpoint, files=files, data=data)
19
  prepared = req.prepare()
 
20
  s = requests.Session()
21
  response = s.send(prepared)
22
  json_result = json.loads(response.text)
 
23
  # Close the opened files
24
  for _, (_, image_data) in files:
25
  image_data.close()
 
26
  return response.status_code, json_result
27
 
28
+ def gradio_interface(image_path, organs):
29
+ image_paths = [image_path]
 
30
  print(image_paths)
31
  status_code, json_result = identify_plant(image_paths, organs)
 
32
  return json_result.get("bestMatch",None), json_result
33
+
34
  with gr.Blocks() as demo:
35
+ image = gr.Image(type="filepath", label = "Plant Image")
36
+ identify_btn = gr.Button("Identify")
37
+
38
+ organs_input = gr.CheckboxGroup(choices=["flower", "leaf", "fruit", "bark", "habit"],label="Organs", info="What are the organs?")
39
+ best_match_text = gr.Textbox(label="Scientific Name")
40
+ json_text = gr.JSON(label="Raw Json String")
41
+
42
+ identify_btn.click(gradio_interface, inputs=[image,organs_input], outputs = [best_match_text,json_text])
43
 
 
44
  demo.launch()
45
 
46