Skip to content

Commit bf057bc

Browse files
committed
BUg corrected return enroll ID
1 parent eedc0fc commit bf057bc

7 files changed

Lines changed: 73 additions & 72 deletions

examples/CameraSettings.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ async def handle_client(reader, writer):
7979
if BB is not None:
8080
for box in BB:
8181
color = LookupTable(box['score']*100, BoxSettings['color'])
82-
bounding_boxes.append(
83-
{ "x1":box['box'][0],
84-
"y1":box['box'][1],
85-
"x2":box['box'][2],
86-
"y2":box['box'][3],
87-
"label":f"Score: {box['score']*100:.2f}%",
88-
"color":color})
82+
Dict = {"x1":box['box'][0],
83+
"y1":box['box'][1],
84+
"x2":box['box'][2],
85+
"y2":box['box'][3],
86+
"label":f"Score: {box['score']*100:.0f}%",
87+
"color":color}
88+
if Model.__class__.__name__ == "FaceRecognizer":
89+
if box['person'] is not None:
90+
Dict['label'] = f"{box['person']['name']} ({box['person']['similarity']*100:.0f}%), Score: {box['score']*100:.0f}%"
91+
bounding_boxes.append(Dict)
8992
response = 'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n' + json.dumps(bounding_boxes)
9093
writer.write(response.encode())
9194
await writer.drain()

src/lib/dl_recognition_define.hpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
11
#pragma once
2-
#include "dl_recognition_types.hpp"
2+
#include <cstdint>
3+
#include <cstring>
4+
5+
namespace mp_esp_dl {
6+
namespace recognition {
7+
8+
#define MAX_NAME_LENGTH 32
9+
10+
struct database_meta {
11+
uint16_t num_feats_total;
12+
uint16_t num_feats_valid;
13+
uint16_t feat_len;
14+
};
15+
16+
struct database_feat {
17+
uint16_t id;
18+
float *feat;
19+
char name[MAX_NAME_LENGTH];
20+
21+
database_feat() : id(0), feat(nullptr) {
22+
name[0] = '\0';
23+
}
24+
25+
database_feat(uint16_t _id, float *_feat, const char *_name = "") : id(_id), feat(_feat) {
26+
strncpy(name, _name, MAX_NAME_LENGTH - 1);
27+
name[MAX_NAME_LENGTH - 1] = '\0';
28+
}
29+
};
30+
31+
struct result_t {
32+
uint16_t id;
33+
float similarity;
34+
char name[MAX_NAME_LENGTH];
35+
36+
result_t() : id(0), similarity(0.0f) {
37+
name[0] = '\0';
38+
}
39+
40+
result_t(uint16_t _id, float _sim, const char *_name = "") : id(_id), similarity(_sim) {
41+
strncpy(name, _name, MAX_NAME_LENGTH - 1);
42+
name[MAX_NAME_LENGTH - 1] = '\0';
43+
}
44+
};
45+
46+
} // namespace recognition
47+
} // namespace mp_esp_dl

src/lib/dl_recognition_types.hpp

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +0,0 @@
1-
#pragma once
2-
#include <cstdint>
3-
#include <cstring>
4-
5-
namespace mp_esp_dl {
6-
namespace recognition {
7-
8-
#define MAX_NAME_LENGTH 32
9-
10-
struct database_meta {
11-
uint16_t num_feats_total;
12-
uint16_t num_feats_valid;
13-
uint16_t feat_len;
14-
};
15-
16-
struct database_feat {
17-
uint16_t id;
18-
float *feat;
19-
char name[MAX_NAME_LENGTH];
20-
21-
database_feat() : id(0), feat(nullptr) {
22-
name[0] = '\0';
23-
}
24-
25-
database_feat(uint16_t _id, float *_feat, const char *_name = "") : id(_id), feat(_feat) {
26-
strncpy(name, _name, MAX_NAME_LENGTH - 1);
27-
name[MAX_NAME_LENGTH - 1] = '\0';
28-
}
29-
};
30-
31-
struct result_t {
32-
uint16_t id;
33-
float similarity;
34-
char name[MAX_NAME_LENGTH];
35-
36-
result_t() : id(0), similarity(0.0f) {
37-
name[0] = '\0';
38-
}
39-
40-
result_t(uint16_t _id, float _sim, const char *_name = "") : id(_id), similarity(_sim) {
41-
strncpy(name, _name, MAX_NAME_LENGTH - 1);
42-
name[MAX_NAME_LENGTH - 1] = '\0';
43-
}
44-
};
45-
46-
} // namespace recognition
47-
} // namespace mp_esp_dl

src/lib/mp_esp_dl_human_face_recognition.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ std::vector<mp_esp_dl::recognition::result_t> HumanFaceRecognizer::recognize(con
8484
}
8585
}
8686

87-
esp_err_t HumanFaceRecognizer::enroll(const dl::image::img_t &img, std::list<dl::detect::result_t> &detect_res, const char *name)
87+
esp_err_t HumanFaceRecognizer::enroll(const dl::image::img_t &img, std::list<dl::detect::result_t> &detect_res, const char *name, uint16_t *new_id)
8888
{
8989
if (detect_res.empty()) {
9090
ESP_LOGW("HumanFaceRecognizer", "Failed to enroll. No face detected.");
9191
return ESP_FAIL;
9292
} else if (detect_res.size() == 1) {
9393
auto feat = m_feat_extract->run(img, detect_res.back().keypoint);
94-
return enroll_feat(feat, name);
94+
return enroll_feat(feat, name, new_id);
9595
} else {
9696
auto max_detect_res =
9797
std::max_element(detect_res.begin(),
@@ -100,6 +100,6 @@ esp_err_t HumanFaceRecognizer::enroll(const dl::image::img_t &img, std::list<dl:
100100
return a.box_area() > b.box_area();
101101
});
102102
auto feat = m_feat_extract->run(img, max_detect_res->keypoint);
103-
return enroll_feat(feat, name);
103+
return enroll_feat(feat, name, new_id);
104104
}
105105
}

src/lib/mp_esp_dl_human_face_recognition.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ class HumanFaceRecognizer : public mp_esp_dl::recognition::DataBase {
4444

4545
std::vector<mp_esp_dl::recognition::result_t> recognize(const dl::image::img_t &img,
4646
std::list<dl::detect::result_t> &detect_res);
47-
esp_err_t enroll(const dl::image::img_t &img, std::list<dl::detect::result_t> &detect_res, const char *name = "");
47+
esp_err_t enroll(const dl::image::img_t &img, std::list<dl::detect::result_t> &detect_res, const char *name, uint16_t *new_id);
4848
};

src/lib/mp_esp_dl_recognition_database.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ esp_err_t DataBase::load_database_from_storage(int feat_len)
144144
return ESP_OK;
145145
}
146146

147-
esp_err_t DataBase::enroll_feat(dl::TensorBase *feat, const char *name, uint16_t *out_id) {
147+
esp_err_t DataBase::enroll_feat(dl::TensorBase *feat, const char *name, uint16_t *new_id)
148+
{
148149
ESP_LOGI(TAG, "Enrolling feature.");
149150
if (feat->dtype != dl::DATA_TYPE_FLOAT) {
150151
ESP_LOGE(TAG, "Only support float feature.");
@@ -159,9 +160,11 @@ esp_err_t DataBase::enroll_feat(dl::TensorBase *feat, const char *name, uint16_t
159160
float *feat_copy = (float *)heap_caps_malloc(m_meta.feat_len * sizeof(float), MALLOC_CAP_SPIRAM);
160161
memcpy(feat_copy, feat->data, feat->get_bytes());
161162

163+
// Neue ID generieren
164+
uint16_t id = m_meta.num_feats_total + 1;
165+
162166
// Füge das Feature zur internen Liste hinzu
163-
uint16_t new_id = m_meta.num_feats_total + 1;
164-
m_feats.emplace_back(new_id, feat_copy, name);
167+
m_feats.emplace_back(id, feat_copy, name);
165168
m_meta.num_feats_total++;
166169
m_meta.num_feats_valid++;
167170

@@ -188,7 +191,7 @@ esp_err_t DataBase::enroll_feat(dl::TensorBase *feat, const char *name, uint16_t
188191
}
189192

190193
// Schreibe die Feature-ID in die Datei
191-
size = mp_write(f, &new_id, sizeof(uint16_t));
194+
size = mp_write(f, &m_feats.back().id, sizeof(uint16_t));
192195
if (size != sizeof(uint16_t)) {
193196
ESP_LOGE(TAG, "Failed to write feature id.");
194197
mp_close(f);
@@ -211,14 +214,11 @@ esp_err_t DataBase::enroll_feat(dl::TensorBase *feat, const char *name, uint16_t
211214
return ESP_FAIL;
212215
}
213216

214-
// Schließe die Datei
215217
mp_close(f);
216-
217-
// Setze die neue ID zurück
218-
if (out_id) {
219-
*out_id = new_id;
220-
}
221-
218+
219+
// Setze die neue ID
220+
*new_id = id;
221+
222222
return ESP_OK;
223223
}
224224

src/lib/mp_esp_dl_recognition_database.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "freertos/queue.h"
55
#include "freertos/event_groups.h"
66
#include "freertos/idf_additions.h"
7-
#include "dl_recognition_types.hpp"
7+
#include "dl_recognition_define.hpp"
88
#include "dl_tensor_base.hpp"
99
#include "esp_check.h"
1010
#include "esp_system.h"
@@ -26,7 +26,7 @@ class DataBase {
2626
DataBase(const char *db_path, int feat_len);
2727
virtual ~DataBase();
2828
esp_err_t clear_all_feats();
29-
esp_err_t enroll_feat(dl::TensorBase *feat, const char *name = "", uint16_t *out_id = nullptr);
29+
esp_err_t enroll_feat(dl::TensorBase *feat, const char *name, uint16_t *new_id);
3030
esp_err_t delete_feat(uint16_t id);
3131
esp_err_t delete_last_feat();
3232
std::vector<result_t> query_feat(dl::TensorBase *feat, float thr, int top_k);

0 commit comments

Comments
 (0)