MapComplete/src/Logic/Web/PlantNet.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-08-17 01:30:07 +02:00
import { Utils } from "../../Utils"
export default class PlantNet {
2023-09-27 22:21:35 +02:00
public static baseUrl =
2022-08-17 01:30:07 +02:00
"https://my-api.plantnet.org/v2/identify/all?api-key=2b10AAsjzwzJvucA5Ncm5qxe"
public static async query(imageUrls: string[]): Promise<PlantNetResult | "no_plant_detected"> {
2022-08-17 01:30:07 +02:00
if (imageUrls.length > 5) {
throw "At most 5 images can be given to PlantNet.query"
}
if (imageUrls.length == 0) {
throw "At least one image should be given to PlantNet.query"
}
let url = PlantNet.baseUrl
for (const image of imageUrls) {
url += "&images=" + encodeURIComponent(image)
}
2025-05-08 11:44:03 +02:00
const result = await Utils.downloadJsonCachedAdvanced(
url,
365 * 24 * 60 * 60 * 1000,
undefined,
true,
1
)
if (result["content"]) {
return result["content"]
}
2025-05-08 11:44:03 +02:00
const errResult = <
{
statusCode: 404
error: "Not Found"
message: "Species not found" | string
}
>JSON.parse(result["errContent"])
if (errResult.message === "Species not found") {
return "no_plant_detected"
}
const err = result["error"]
console.log("Get error result:", err, ">>>>", result["errContent"])
throw err
2022-08-17 01:30:07 +02:00
}
}
export interface PlantNetSpeciesMatch {
score: number
gbif: { id: string /*Actually a number*/ }
species: {
scientificNameWithoutAuthor: string
scientificNameAuthorship: string
genus: {
scientificNameWithoutAuthor: string
scientificNameAuthorship: string
scientificName: string
}
family: {
scientificNameWithoutAuthor: string
scientificNameAuthorship: string
scientificName: string
}
commonNames: string[]
scientificName: string
}
}
2022-08-17 01:30:07 +02:00
export interface PlantNetResult {
query: {
project: string
images: string[]
organs: string[]
includeRelatedImages: boolean
}
language: string
preferedReferential: string
bestMatch: string
results: PlantNetSpeciesMatch[]
2022-08-17 01:30:07 +02:00
version: string
remainingIdentificationRequests: number
}