-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKFZ_Detection_Adapter.ts
196 lines (162 loc) · 9.17 KB
/
KFZ_Detection_Adapter.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// ===========================================================================================
// PREPARATIONS:
// ===========================================================================================
// 1. INSTALL AI-SERVER:
// https://www.codeproject.com/Articles/5322557/CodeProject-AI-Server-AI-the-easy-way
// 2. Install NPM-Modul:
// npm install jimp --save
// 3. Add NPM Modul "jimp" to JavaScript-Instance
// ===========================================================================================
// USER CONFIG:
// ===========================================================================================
const serverURL = "http://192.168.178.124:32168/v1/image/alpr"; // API-URL zum AI-Server
const imageURL = "http://192.168.178.90/snap.jpeg"; // Das Bild, welches erkannt werden soll
const minimumConfidencePercent = 75; // Mindesterkennungsrate des Bildes in Prozent
const minimumDetectionDurationTimeMS = 5000; // Mindest Detection-Intervall in ms
// ===========================================================================================
// DO NOT MODIFY CODE BELOW THIS LINE!
// ===========================================================================================
const baseDirectory = "/opt/iobroker/";
const baseState = "0_userdata.0.aiserver.Plate";
const fs = require('fs');
const Jimp = require('jimp') ;
var runningIndex = 0;
// Ergebnis der Detection als Json_Stringify (flachgeklopftes JSON)
const stateResult = baseState + ".result";
createState(stateResult, "", {
name: stateResult,
desc: stateResult,
type: 'string',
read: true,
write: true
});
// MinimumConfidence:
const stateMinimumConfidence = baseState + ".minimumConfidence";
createState(stateMinimumConfidence, minimumConfidencePercent, {
name: stateMinimumConfidence,
desc: stateMinimumConfidence,
type: 'number',
read: true,
write: true
});
setState(stateMinimumConfidence, minimumConfidencePercent);
start();
async function start() {
let i = 0;
do {
i++;
await doDetection(detectionResult);
} while (true/* && i < 1*/);
}
async function doDetection(detectionResultCallback) {
return new Promise(iterationCallback => {
var startTimeMs = new Date().getTime();
// Bild laden, beispielsweise von der Kamera:
request.get({url: imageURL,
encoding: null}, async function (err, response, body) {
if (err) {
throw err;
}
// Das geladene Bild auf der Festplatte abspeichern:
runningIndex++;
if (runningIndex > 10) {
runningIndex = 0;
}
var filename = baseDirectory + "detection_plate_" + runningIndex + ".jpg";
fs.writeFile(filename, body, null, function(err) {
if (err) {
log("Error" + err);
} else {
// Danach Bild wieder in den Speicher laden:
var image = fs.createReadStream(filename);
if (err) {
throw err;
}
// KI-Übergabeparameter vorbereiten:
var options = {
method: 'POST',
url: serverURL,
headers: {
'Content-Type': 'multipart/form-data'
},
formData: {
image: image,
// min_confidence: minimumConfidencePercent/100
}
};
// KI API aufrufen mit unserem geladenen Bild:
request(options, function (error, response) {
if (error) throw new Error(error);
var json_object = JSON.parse(response.body);
json_object.filename_source = filename;
// Erfolg:
if (json_object.success) {
Jimp.read(filename, function(err, imageJimp) { // Gets the image file from the URL
if (err) throw new Error(error);
Jimp.loadFont(Jimp.FONT_SANS_32_WHITE, function (err, fontJimp) {
if (err) throw new Error(error);
json_object.predictions.forEach(function (prediction) {
if (prediction.confidence > (minimumConfidencePercent/100)) {
// Prozentzahl als Text:
imageJimp.scan(prediction.x_min, prediction.y_max+5, 75, 40, makeIteratorThatFillsWithColor(0x00000040));
imageJimp.print(fontJimp, prediction.x_min+7, prediction.y_max+8, (prediction.confidence*100).toFixed(0)+"%");
// border
const fillCrimson = makeIteratorThatFillsWithColor(0xED143DFF);
// Waagrecht; x1, y1, Länge Strich, Strichbreite (obere waagreche Linie):
imageJimp.scan(prediction.x_min, prediction.y_min, (prediction.x_max-prediction.x_min), 3, fillCrimson);
// Waagrecht; x1, y1, Länge Strich, Strichbreite (untere waagreche Linie):
imageJimp.scan(prediction.x_min, prediction.y_max, (prediction.x_max-prediction.x_min), 3, fillCrimson);
// Senkrecht; x1, y1, Strichbreite, Länge Strich (linke sekrechte Linie)
imageJimp.scan(prediction.x_min, prediction.y_min, 3, (prediction.y_max-prediction.y_min), fillCrimson);
// Senkrecht; x1, y1, Strichbreite, Länge Strich (rechte sekrechte Linie)
imageJimp.scan(prediction.x_max, prediction.y_min, 3, (prediction.y_max-prediction.y_min), fillCrimson);
}
});
var filenameBoundingBox = baseDirectory + "detection_plate_boundingbox" + runningIndex + ".jpg";
json_object.filename_boundingbox = filenameBoundingBox;
imageJimp.write(filenameBoundingBox, function(err) {
if (err) throw new Error(error);
// Callback-Aufruf. Dort kann man irgendwas mit dem Ergebnis machen:
detectionResultCallback(JSON.stringify(json_object));
// Nun gehen wir nicht gleich zur nächsten Iteration über, sondern warten/respektieren die Mindestdauer:
var endTimeMs = new Date().getTime();
var waitDuration = minimumDetectionDurationTimeMS - (endTimeMs-startTimeMs);
if (waitDuration > 0) {
setTimeout(time => {
iterationCallback("");
}, waitDuration);
} else {
iterationCallback("");
}
});
});
});
} else {
// Kein Erfolg:
// Callback-Aufruf. Dort kann man irgendwas mit dem Ergebnis machen:
detectionResultCallback(JSON.stringify(json_object));
// Nun gehen wir nicht gleich zur nächsten Iteration über, sondern warten/respektieren die Mindestdauer:
var endTimeMs = new Date().getTime();
var waitDuration = minimumDetectionDurationTimeMS - (endTimeMs-startTimeMs);
if (waitDuration > 0) {
setTimeout(time => {
iterationCallback("");
}, waitDuration);
} else {
iterationCallback("");
}
}
}); // end request
}
}); // end write
});
});
}
function makeIteratorThatFillsWithColor(color) {
return function (x, y, offset) {
this.bitmap.data.writeUInt32BE(color, offset, true);
}
};
function detectionResult(jsonObjectStringify) { // Parameter: Flachgeklopfte JSON-Struktur als String
setState(stateResult, jsonObjectStringify);
}