-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plantnet.js
106 lines (98 loc) · 4.45 KB
/
Plantnet.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {clone, isSet, loadJsonResource} from "../lib/Common.js";
import {firstImageOf} from "../domain/post.js";
import {IDENTIFY_RESULT, PLANTNET_MINIMAL_PERCENT} from "../servicesExternal/PlantnetApiService.js";
export default class Plantnet {
constructor(config, loggerService, blueskyService, pluginsCommonService, plantnetCommonService, plantnetApiService) {
this.isAvailable = false;
this.logger = loggerService.getLogger().child({label: 'Pl@ntNet'});
this.blueskyService = blueskyService;
this.plantnetSimulate = (config.bot.plantnetSimulate === true);
this.pluginsCommonService = pluginsCommonService;
this.plantnetCommonService = plantnetCommonService;
this.plantnetApiService = plantnetApiService;
try {
this.questions = loadJsonResource('src/data/questionsPlantnet.json');
this.isAvailable = plantnetApiService.isReady();
this.logger.info((this.isAvailable ? "available" : "not available") +
" with " + this.questions.length + " questions");
} catch (exception) {
pluginsCommonService.logError("init", exception);
}
}
getName() {
return "Plantnet";
}
getPluginTags() {
return ["#BeSPlantnet", "#IndentificationDePlantes"].join(' ');
}
getQuestions() {
return clone(this.questions);
}
isReady() {
return this.isAvailable;
}
async process(config) {
const pluginName = this.getName();
const {
plantnetSimulate,
pluginsCommonService,
plantnetCommonService,
plantnetApiService,
logger,
questions
} = this;
let {doSimulate, simulateIdentifyCase, context} = config;
const doSimulateIdentify = plantnetSimulate || isSet(simulateIdentifyCase);// if at least one want to simulate then simulate
let candidate = null;
let step = "searchNextCandidate";
const hasImages = true;
try {
candidate = await pluginsCommonService.searchNextCandidate({...config, questions, hasImages});
if (candidate === null) {
return pluginsCommonService.resultNoCandidate(pluginName, context);
}
step = "firstImageOf";
const candidatePhoto = firstImageOf(candidate);
if (!candidatePhoto) {
return pluginsCommonService.rejectNoCandidateImage(pluginName, candidate, context);
}
step = "logCandidate";
pluginsCommonService.logCandidate(pluginName, candidate, candidatePhoto, context);
step = "plantnetIdentify";
const tags = this.getPluginTags();
const {
result,
plantnetResult = null
} = await plantnetApiService.plantnetIdentify({
"imageUrl": candidatePhoto?.fullsize,
doSimulate,
doSimulateIdentify,
simulateIdentifyCase,
candidate,
tags,
context
});
step = "handle plantnet identification";
if (result === IDENTIFY_RESULT.OK) {
const {scoredResult, firstImageOriginalUrl, firstImageText} = plantnetResult;
return await plantnetCommonService.replyToWithIdentificationResult(candidate,
{tags, doSimulate, context},
{scoredResult, firstImageOriginalUrl, firstImageText}
);
} else if (result === IDENTIFY_RESULT.BAD_SCORE) {
return await pluginsCommonService.handleWithoutScoredResult(pluginName, PLANTNET_MINIMAL_PERCENT,
{doSimulate, candidate, "replyTo": null, "muteAuthor": true, context}
);
} else {
if (result !== IDENTIFY_RESULT.NONE) {
logger.warn(`unable to handle plantnetService.plantnetIdentify result:${result} so consider it as NONE`);
}
return await pluginsCommonService.handleWithNoIdentificationResult(pluginName,
{doSimulate, candidate, "replyTo": null, "muteAuthor": true, context}
);
}
} catch (err) {
return pluginsCommonService.rejectWithIdentifyError(pluginName, step, candidate, err, context);
}
}
}