Skip to content

Commit f199fa3

Browse files
committed
update apiRequest after update of PlantNet API
1 parent 7384c8f commit f199fa3

File tree

2 files changed

+297
-2
lines changed

2 files changed

+297
-2
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ jupyter notebook apiRequests.ipynb
5454
* Password
5555
* For the other APIs:
5656
* Access token
57-
* Filename of csv, where for each image the organ for each image is specified. Organ could be one of: leaf, flower, fruit, bark (For Pl@ntNet only)
5857
* The result file will have following result:
5958
* fileName of image, first prediction, score of first prediction, second prediction, score of second prediction, ...
6059

apiRequests.ipynb

Lines changed: 297 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,297 @@
1-
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Copy of apiRequests.ipynb","provenance":[{"file_id":"1DeqHHIVPyq050iqMdRp6fApulrSw6-Pv","timestamp":1639148424775}],"collapsed_sections":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.4"}},"cells":[{"cell_type":"markdown","metadata":{"id":"aDrbTiS5DtVu"},"source":["# Requests to Computer Vision APIs (plant.id, PlantNet, iNaturalist, NIA)"]},{"cell_type":"code","metadata":{"id":"uJ39A3lpD_rL"},"source":["# import of needed packages\n","import base64\n","import requests\n","import getpass\n","import csv\n","import os\n","import json\n","from pprint import pprint\n","import time"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"m0dq5HDmDo9Z"},"source":["# function to request the Plant.id API\n","def plantID (image, token, dir):\n"," image_data = readImage(dir + \"/\" + image) \n"," json_data = {\n"," \"images\": image_data,\n"," \"plant_details\": [\"common_names\", \"url\", \"taxonomy\"]\n"," }\n"," response = requests.post(\n"," \"https://api.plant.id/v2/identify\",\n"," json=json_data,\n"," headers={\n"," \"Content-Type\": \"application/json\",\n"," \"Api-Key\": token\n"," }).json()\n"," result= [image]\n"," for suggestion in response[\"suggestions\"]:\n"," result.append(suggestion[\"plant_name\"])\n"," result.append(str(suggestion[\"probability\"]))\n"," return result"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"zguR5RkqMdS-"},"source":["# function to request the plantNet API\n","def plantNet (image, token, dir): \n"," image_data = open(dir + \"/\" + image[0], 'rb')\n"," api_endpoint = f\"https://my-api.plantnet.org/v2/identify/all?api-key={token}\"\n","\n","\n"," files = [\n"," ('images', (image[0], image_data, ), )\n"," ]\n"," \n"," data = {\n"," 'organs': [image[1].lstrip()]\n"," }\n","\n"," req = requests.Request('POST', url=api_endpoint, files=files, data=data)\n"," prepared = req.prepare()\n","\n"," s = requests.Session()\n"," response = s.send(prepared)\n"," json_result = json.loads(response.text)\n","\n"," pprint(response.status_code)\n"," pprint(json_result)\n"," result = [image[0]]\n"," if(response.status_code==404):\n"," result.append(\"not found\")\n"," elif(response.status_code==200): \n"," for suggestion in json_result[\"results\"]:\n"," result.append(suggestion[\"species\"][\"scientificNameWithoutAuthor\"]) \n"," result.append(str(suggestion[\"score\"]))\n"," return result"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"m-i9gldEbbAe"},"source":["# function to request an access token for the NIA (Nature Identification API)\n","def NiaRetrieveAccess (clientId, email, password): \n"," data = {\n"," 'client_id': clientId,\n"," 'grant_type': 'password',\n"," 'email': email,\n"," 'password': password\n"," }\n","\n"," response = requests.post('https://waarneming.nl/api/v1/oauth2/token/', data=data)\n","\n"," response_text_json = json.loads(response.text) \n"," acces_token = response_text_json[\"access_token\"]\n","\n"," return acces_token"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"HLrPrFVsbbAi"},"source":["# function to request the NIA API\n","def NIA(image, token, dir):\n"," image_dir = dir + \"/\" + image \n"," \n"," headers = {\n"," 'Authorization': 'Bearer ' + token,\n"," }\n","\n"," params = (\n"," ('app_name', 'uni-muenster'),\n"," )\n","\n"," files = {\n"," 'image': (image_dir, open(image_dir, 'rb')),\n"," }\n"," \n"," response = requests.post('https://waarneming.nl/api/identify-proxy/v1/', headers=headers, params=params, files=files)\n"," \n"," response_text_json = json.loads(response.text)\n"," \n"," result = [image]\n"," \n"," for prediction in response_text_json[\"predictions\"]:\n"," result.append(prediction[\"taxon\"][\"name\"])\n"," result.append(str(prediction[\"probability\"])) \n"," \n"," return result\n"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"tRr2pNuDb5qL"},"source":["# function to request the iNaturalist API\n","def iNaturalist(image, token, dir):\n"," api_endpoint = \"https://visionapi.p.rapidapi.com/v1/rapidapi/score_image\"\n"," image_data = open(dir + \"/\" + image, 'rb')\n","\n"," headers = {\n"," 'x-rapidapi-host': \"visionapi.p.rapidapi.com\",\n"," 'x-rapidapi-key': token\n"," }\n","\n"," files = [\n"," ('image', (image, image_data))\n"," ]\n","\n"," req = requests.Request('POST', headers=headers, url=url, files=files)\n"," prepared = req.prepare()\n"," s = requests.Session()\n"," response = s.send(prepared)\n"," json_result = json.loads(response.text)\n"," result = [image]\n"," if(response.status_code==200): \n"," for suggestion in json_result[\"results\"]:\n"," result.append(suggestion[\"taxon\"][\"name\"]) \n"," result.append(\"n/a\")\n"," return result"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"rJahqD-uFEkJ"},"source":["# encode image to base64\n","def readImage (image_name):\n"," with open(image_name, \"rb\") as file:\n"," image = [base64.b64encode(file.read()).decode(\"ascii\")]\n"," return image"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"e1uoIZGqIwTJ"},"source":["# function to write the results to csv\n","def writeDataToCSV (data, filename):\n"," with open(filename,'a+', encoding='UTF8', newline='') as file:\n"," writer = csv.writer(file)\n"," writer.writerow(data)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"oLordk4TFZU0","scrolled":true},"source":["# final excecution script\n","dir = input('Directory with test images: ') \n","filename = input('Filename of result CSV: ') \n","api = input('API (\"plantNet\",\"iNaturalist\", \"plantID\", \"NIA\"): ') \n","if (api == \"plantID\" or api == \"iNaturalist\" or api==\"NIA\"): \n"," if (api == \"NIA\"): \n"," clientId= getpass.getpass(prompt='Client Id: ', stream=None) \n"," email= input('Email: ') \n"," password= getpass.getpass(prompt='Password: ', stream=None) \n"," token = NiaRetrieveAccess(clientId, email, password)\n"," else:\n"," token= getpass.getpass(prompt='Api Key: ', stream=None) \n","\n","\n"," # get all images from the test directory \n"," images = os.listdir(dir)\n","\n"," # requesting the API for each image in the test directory\n"," for image in images:\n"," response = eval(api + \"(image, token, dir)\")\n"," writeDataToCSV(response, filename)\n"," if (api == \"iNaturalist\"):\n"," time.sleep(1) # limitation that just 60 requests per minute are allowed\n","\n","elif (api == \"plantNet\"): \n"," # plantNet requires an organ for each image. Organ could be one of: leaf, flower, fruit, bark\n"," species_list = input('Name of the CSV where corresponding organ is added to the imagefile') #csv should look like : imageName.jpg, organ \\n\n"," with open(species_list, mode ='r')as file:\n"," \n"," # reading the CSV file\n"," csv_file = csv.reader(file)\n","\n"," # requesting the API for each line of the CSV file\n"," for lines in csv_file:\n"," response= plantNet(lines, api_key, dir)\n"," writeDataToCSV(response, filename)\n","else:\n"," print('API must be one of \"plantNet\",\"iNaturalist\", \"plantID\", \"NIA\"')\n","\n"," \n"," "],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"xnT0UdvIbbA3"},"source":[""],"execution_count":null,"outputs":[]}]}
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "aDrbTiS5DtVu"
7+
},
8+
"source": [
9+
"# Requests to Computer Vision APIs (plant.id, PlantNet, iNaturalist, NIA)"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"id": "uJ39A3lpD_rL"
17+
},
18+
"outputs": [],
19+
"source": [
20+
"# import of needed packages\n",
21+
"import base64\n",
22+
"import requests\n",
23+
"import getpass\n",
24+
"import csv\n",
25+
"import os\n",
26+
"import json\n",
27+
"from pprint import pprint\n",
28+
"import time"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {
35+
"id": "m0dq5HDmDo9Z"
36+
},
37+
"outputs": [],
38+
"source": [
39+
"# function to request the Plant.id API\n",
40+
"def plantID (image, token, dir):\n",
41+
" image_data = readImage(dir + \"/\" + image) \n",
42+
" json_data = {\n",
43+
" \"images\": image_data,\n",
44+
" \"plant_details\": [\"common_names\", \"url\", \"taxonomy\"]\n",
45+
" }\n",
46+
" response = requests.post(\n",
47+
" \"https://api.plant.id/v2/identify\",\n",
48+
" json=json_data,\n",
49+
" headers={\n",
50+
" \"Content-Type\": \"application/json\",\n",
51+
" \"Api-Key\": token\n",
52+
" }).json()\n",
53+
" result= [image]\n",
54+
" for suggestion in response[\"suggestions\"]:\n",
55+
" result.append(suggestion[\"plant_name\"])\n",
56+
" result.append(str(suggestion[\"probability\"]))\n",
57+
" return result"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {
64+
"id": "zguR5RkqMdS-"
65+
},
66+
"outputs": [],
67+
"source": [
68+
"# function to request the plantNet API\n",
69+
"def plantNet (image, token, dir): \n",
70+
" image_data = open(dir + \"/\" + image, 'rb')\n",
71+
" api_endpoint = f\"https://my-api.plantnet.org/v2/identify/the-plant-list?api-key={token}\"\n",
72+
"\n",
73+
"\n",
74+
" files = [\n",
75+
" ('images', (image, image_data, ) )\n",
76+
" ]\n",
77+
" \n",
78+
"\n",
79+
"\n",
80+
" req = requests.Request('POST', url=api_endpoint, files=files)\n",
81+
" prepared = req.prepare()\n",
82+
"\n",
83+
" s = requests.Session()\n",
84+
" response = s.send(prepared)\n",
85+
" json_result = json.loads(response.text)\n",
86+
"\n",
87+
" pprint(response.status_code)\n",
88+
" pprint(json_result)\n",
89+
" result = [image]\n",
90+
" if(response.status_code==404):\n",
91+
" result.append(\"not found\")\n",
92+
" elif(response.status_code==200): \n",
93+
" for suggestion in json_result[\"results\"]:\n",
94+
" result.append(suggestion[\"species\"][\"scientificNameWithoutAuthor\"]) \n",
95+
" result.append(str(suggestion[\"score\"]))\n",
96+
" return result"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"metadata": {
103+
"id": "m-i9gldEbbAe"
104+
},
105+
"outputs": [],
106+
"source": [
107+
"# function to request an access token for the NIA (Nature Identification API)\n",
108+
"def NiaRetrieveAccess (clientId, email, password): \n",
109+
" data = {\n",
110+
" 'client_id': clientId,\n",
111+
" 'grant_type': 'password',\n",
112+
" 'email': email,\n",
113+
" 'password': password\n",
114+
" }\n",
115+
"\n",
116+
" response = requests.post('https://waarneming.nl/api/v1/oauth2/token/', data=data)\n",
117+
"\n",
118+
" response_text_json = json.loads(response.text) \n",
119+
" acces_token = response_text_json[\"access_token\"]\n",
120+
"\n",
121+
" return acces_token"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {
128+
"id": "HLrPrFVsbbAi"
129+
},
130+
"outputs": [],
131+
"source": [
132+
"# function to request the NIA API\n",
133+
"def NIA(image, token, dir):\n",
134+
" image_dir = dir + \"/\" + image \n",
135+
" \n",
136+
" headers = {\n",
137+
" 'Authorization': 'Bearer ' + token,\n",
138+
" }\n",
139+
"\n",
140+
" params = (\n",
141+
" ('app_name', 'uni-muenster'),\n",
142+
" )\n",
143+
"\n",
144+
" files = {\n",
145+
" 'image': (image_dir, open(image_dir, 'rb')),\n",
146+
" }\n",
147+
" \n",
148+
" response = requests.post('https://waarneming.nl/api/identify-proxy/v1/', headers=headers, params=params, files=files)\n",
149+
" \n",
150+
" response_text_json = json.loads(response.text)\n",
151+
" \n",
152+
" result = [image]\n",
153+
" \n",
154+
" for prediction in response_text_json[\"predictions\"]:\n",
155+
" result.append(prediction[\"taxon\"][\"name\"])\n",
156+
" result.append(str(prediction[\"probability\"])) \n",
157+
" \n",
158+
" return result\n"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {
165+
"id": "tRr2pNuDb5qL"
166+
},
167+
"outputs": [],
168+
"source": [
169+
"# function to request the iNaturalist API\n",
170+
"def iNaturalist(image, token, dir):\n",
171+
" api_endpoint = \"https://visionapi.p.rapidapi.com/v1/rapidapi/score_image\"\n",
172+
" image_data = open(dir + \"/\" + image, 'rb')\n",
173+
"\n",
174+
" headers = {\n",
175+
" 'x-rapidapi-host': \"visionapi.p.rapidapi.com\",\n",
176+
" 'x-rapidapi-key': token\n",
177+
" }\n",
178+
"\n",
179+
" files = [\n",
180+
" ('image', (image, image_data))\n",
181+
" ]\n",
182+
"\n",
183+
" req = requests.Request('POST', headers=headers, url=url, files=files)\n",
184+
" prepared = req.prepare()\n",
185+
" s = requests.Session()\n",
186+
" response = s.send(prepared)\n",
187+
" json_result = json.loads(response.text)\n",
188+
" result = [image]\n",
189+
" if(response.status_code==200): \n",
190+
" for suggestion in json_result[\"results\"]:\n",
191+
" result.append(suggestion[\"taxon\"][\"name\"]) \n",
192+
" result.append(\"n/a\")\n",
193+
" return result"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": null,
199+
"metadata": {
200+
"id": "rJahqD-uFEkJ"
201+
},
202+
"outputs": [],
203+
"source": [
204+
"# encode image to base64\n",
205+
"def readImage (image_name):\n",
206+
" with open(image_name, \"rb\") as file:\n",
207+
" image = [base64.b64encode(file.read()).decode(\"ascii\")]\n",
208+
" return image"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": null,
214+
"metadata": {
215+
"id": "e1uoIZGqIwTJ"
216+
},
217+
"outputs": [],
218+
"source": [
219+
"# function to write the results to csv\n",
220+
"def writeDataToCSV (data, filename):\n",
221+
" with open(filename,'a+', encoding='UTF8', newline='') as file:\n",
222+
" writer = csv.writer(file)\n",
223+
" writer.writerow(data)"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": null,
229+
"metadata": {
230+
"colab": {
231+
"base_uri": "https://localhost:8080/"
232+
},
233+
"id": "oLordk4TFZU0",
234+
"outputId": "aa92fc39-244d-4dbe-ac48-78fd5d272dbf",
235+
"scrolled": true
236+
},
237+
"outputs": [],
238+
"source": [
239+
"## final excecution script\n",
240+
"dir = input('Directory with test images: ') \n",
241+
"filename = input('Filename of result CSV: ') \n",
242+
"api = input('API (\"plantNet\",\"iNaturalist\", \"plantID\", \"NIA\"): ') \n",
243+
"if (api == \"plantID\" or api == \"iNaturalist\" or api==\"NIA\" or api == \"plantNet\"): \n",
244+
" if (api == \"NIA\"): \n",
245+
" clientId= getpass.getpass(prompt='Client Id: ', stream=None) \n",
246+
" email= input('Email: ') \n",
247+
" password= getpass.getpass(prompt='Password: ', stream=None) \n",
248+
" token = NiaRetrieveAccess(clientId, email, password)\n",
249+
" else:\n",
250+
" token= getpass.getpass(prompt='Api Key: ', stream=None) \n",
251+
"\n",
252+
"\n",
253+
" # get all images from the test directory \n",
254+
" images = os.listdir(dir)\n",
255+
"\n",
256+
" # requesting the API for each image in the test directory\n",
257+
" for image in images:\n",
258+
" response = eval(api + \"(image, token, dir)\")\n",
259+
" writeDataToCSV(response, filename)\n",
260+
" if (api == \"iNaturalist\"):\n",
261+
" time.sleep(1) # limitation that just 60 requests per minute are allowed\n",
262+
"\n",
263+
"else:\n",
264+
" print('API must be one of \"plantNet\",\"iNaturalist\", \"plantID\", \"NIA\"')\n",
265+
"\n",
266+
" \n",
267+
" "
268+
]
269+
}
270+
],
271+
"metadata": {
272+
"colab": {
273+
"collapsed_sections": [],
274+
"name": "apiRequests.ipynb",
275+
"provenance": []
276+
},
277+
"kernelspec": {
278+
"display_name": "Python 3",
279+
"language": "python",
280+
"name": "python3"
281+
},
282+
"language_info": {
283+
"codemirror_mode": {
284+
"name": "ipython",
285+
"version": 3
286+
},
287+
"file_extension": ".py",
288+
"mimetype": "text/x-python",
289+
"name": "python",
290+
"nbconvert_exporter": "python",
291+
"pygments_lexer": "ipython3",
292+
"version": "3.7.9"
293+
}
294+
},
295+
"nbformat": 4,
296+
"nbformat_minor": 1
297+
}

0 commit comments

Comments
 (0)