| 236 | // ---------------------------------------------------------------------------------------- |
| 237 | |
| 238 | py::list bottom_up_clustering(py::list descriptors, const int min_num_clusters, const double max_dist) |
| 239 | { |
| 240 | DLIB_CASSERT(min_num_clusters > 0); |
| 241 | |
| 242 | size_t num_descriptors = py::len(descriptors); |
| 243 | matrix<float> dist(static_cast<long>(num_descriptors), static_cast<long>(num_descriptors)); |
| 244 | |
| 245 | for (size_t i = 0; i < num_descriptors; ++i) |
| 246 | { |
| 247 | for (size_t j = i+1; j < num_descriptors; ++j) |
| 248 | { |
| 249 | const long i2 = static_cast<long>(i); |
| 250 | const long j2 = static_cast<long>(j); |
| 251 | matrix<double,0,1>& first_descriptor = descriptors[i].cast<matrix<double,0,1>&>(); |
| 252 | matrix<double,0,1>& second_descriptor = descriptors[j].cast<matrix<double,0,1>&>(); |
| 253 | dist(i2, j2) = dlib::length( first_descriptor- second_descriptor); |
| 254 | dist(j2, i2) = dist(i2, j2); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | std::vector<unsigned long> labels; |
| 259 | const auto num_clusters = dlib::bottom_up_cluster(dist, labels, min_num_clusters, max_dist); |
| 260 | |
| 261 | py::list clusters; |
| 262 | for (size_t i = 0; i < labels.size(); ++i) |
| 263 | { |
| 264 | clusters.append(labels[i]); |
| 265 | } |
| 266 | return clusters; |
| 267 | } |
| 268 | |
| 269 | py::list chinese_whispers_clustering(py::list descriptors, float threshold) |
| 270 | { |