File size: 1,751 Bytes
8ee0874
 
 
 
 
9492b61
8ee0874
9492b61
8ee0874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b29d84
 
8ee0874
 
 
0b29d84
0ef5fab
0b29d84
 
 
 
 
 
 
 
8ee0874
0ef5fab
8ee0874
 
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
# Python script, Unicode text, UTF-8 text executable, with CRLF line terminators
import requests
import json
from pprint import pprint
import gradio as gr
import os

API_KEY = os.environ.get('PLANT_API_KEY')  # Your API_KEY from https://my.plantnet.org/account/settings
PROJECT = "all";  # try specific floras: "weurope", "canada"
api_endpoint = f"https://my-api.plantnet.org/v2/identify/{PROJECT}?api-key={API_KEY}&lang=zh"

def identify_plant(image_paths, organs):
    files = []
    for image_path in image_paths:
        image_data = open(image_path, 'rb')
        files.append(('images', (image_path, image_data)))
    data = {'organs': organs}
    req = requests.Request('POST', url=api_endpoint, files=files, data=data)
    prepared = req.prepare()
    s = requests.Session()
    response = s.send(prepared)
    json_result = json.loads(response.text)
    # Close the opened files
    for _, (_, image_data) in files:
        image_data.close()
    return response.status_code, json_result

def gradio_interface(image_path, organs):
    image_paths = [image_path]
    print(image_paths)
    status_code, json_result = identify_plant(image_paths, organs)
    return json_result.get("bestMatch",None), json_result

with gr.Blocks(title="Clay&Tree PlantyAI") as demo:
    image = gr.Image(type="filepath", label = "Plant Image")
    identify_btn = gr.Button("Identify")
    
    organs_input = gr.CheckboxGroup(choices=["flower", "leaf", "fruit", "bark", "habit"],label="Organs", info="What are the organs?")
    best_match_text = gr.Textbox(label="Scientific Name")
    json_text = gr.JSON(label="Raw Json String")
    
    identify_btn.click(gradio_interface, inputs=[image,organs_input], outputs = [best_match_text,json_text])

demo.launch()