尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
C++ 机器学习 dlib 实践
test// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt /* This is an example illustrating the use of the perspective_window tool in the dlib C Library. It is a simple tool for displaying 3D point clouds on the screen. */ #include dlib/gui_widgets.h #include dlib/image_transforms.h #include cmath using namespace dlib; using namespace std; // ---------------------------------------------------------------------------------------- int test3DPointCloudPreview() { // Lets make a point cloud that looks like a 3D spiral. std::vectorperspective_window::overlay_dot points; dlib::rand rnd; for (double i 0; i 20; i 0.001) { // Get a point on a spiral dlib::vectordouble val(sin(i), cos(i), i / 4); // Now add some random noise to it dlib::vectordouble temp(rnd.get_random_gaussian(), rnd.get_random_gaussian(), rnd.get_random_gaussian()); val temp / 20; // Pick a color based on how far we are along the spiral rgb_pixel color colormap_jet(i, 0, 20); // And add the point to the list of points we will display points.push_back(perspective_window::overlay_dot(val, color)); } // Now finally display the point cloud. perspective_window win; win.set_title(perspective_window 3D point cloud); win.add_overlay(points); win.wait_until_closed(); return 0; } // ---------------------------------------------------------------------------- // K分类算法 #include dlib/dnn.h int testDNNMetricLearning() try { using namespace std; using namespace dlib; // The API for doing metric learning is very similar to the API for // multi-class classification. In fact, the inputs are the same, a bunch of // labeled objects. So here we create our dataset. We make up some simple // vectors and label them with the integers 1,2,3,4. The specific values of // the integer labels dont matter. std::vectormatrixdouble, 0, 1 samples; std::vectorunsigned long labels; // class 1 training vectors samples.push_back({ 1,0,0,0,0,0,0,0 }); labels.push_back(1); samples.push_back({ 0,1,0,0,0,0,0,0 }); labels.push_back(1); // class 2 training vectors samples.push_back({ 0,0,1,0,0,0,0,0 }); labels.push_back(2); samples.push_back({ 0,0,0,1,0,0,0,0 }); labels.push_back(2); // class 3 training vectors samples.push_back({ 0,0,0,0,1,0,0,0 }); labels.push_back(3); samples.push_back({ 0,0,0,0,0,1,0,0 }); labels.push_back(3); // class 4 training vectors samples.push_back({ 0,0,0,0,0,0,1,0 }); labels.push_back(4); samples.push_back({ 0,0,0,0,0,0,0,1 }); labels.push_back(4); // Make a network that simply learns a linear mapping from 8D vectors to 2D // vectors. using net_type loss_metricfc2, inputmatrixdouble, 0, 1; net_type net; dnn_trainernet_type trainer(net); trainer.set_learning_rate(0.1); // It should be emphasized out that its really important that each mini-batch contain // multiple instances of each class of object. This is because the metric learning // algorithm needs to consider pairs of objects that should be close as well as pairs // of objects that should be far apart during each training step. Here we just keep // training on the same small batch so this constraint is trivially satisfied. while (trainer.get_learning_rate() 1e-4) trainer.train_one_step(samples, labels); // Wait for training threads to stop trainer.get_net(); cout done training endl; // Run all the samples through the network to get their 2D vector embeddings. std::vectormatrixfloat, 0, 1 embedded net(samples); // Print the embedding for each sample to the screen. If you look at the // outputs carefully you should notice that they are grouped together in 2D // space according to their label. for (size_t i 0; i embedded.size(); i) cout label: labels[i] \t trans(embedded[i]); // Now, check if the embedding puts things with the same labels near each other and // things with different labels far apart. int num_right 0; int num_wrong 0; for (size_t i 0; i embedded.size(); i) { for (size_t j i 1; j embedded.size(); j) { if (labels[i] labels[j]) { // The loss_metric layer will cause things with the same label to be less // than net.loss_details().get_distance_threshold() distance from each // other. So we can use that distance value as our testing threshold for // being near to each other. if (length(embedded[i] - embedded[j]) net.loss_details().get_distance_threshold()) num_right; else num_wrong; } else { if (length(embedded[i] - embedded[j]) net.loss_details().get_distance_threshold()) num_right; else num_wrong; } } } cout num_right: num_right endl; cout num_wrong: num_wrong endl; return 0; } catch (std::exception e) { cout e.what() endl; } #include dlib/image_processing/frontal_face_detector.h #include dlib/gui_widgets.h #include dlib/image_io.h #include filesystem // ---------------------------------------------------------------------------------------- int testRecognizeFace(const char* filePath) { using namespace dlib; using namespace std; try { frontal_face_detector detector get_frontal_face_detector(); image_window win; if (!std::filesystem::is_directory(filePath)) return 1; for (const auto entry : std::filesystem::directory_iterator(filePath)) { if (entry.is_regular_file()) { std::cout processing image File: entry.path().filename() std::endl; array2dunsigned char img; load_image(img, entry.path().string()); // Make the image bigger by a factor of two. This is useful since // the face detector looks for faces that are about 80 by 80 pixels // or larger. Therefore, if you want to find faces that are smaller // than that then you need to upsample the image as we do here by // calling pyramid_up(). So this will allow it to detect faces that // are at least 40 by 40 pixels in size. We could call pyramid_up() // again to find even smaller faces, but note that every time we // upsample the image we make the detector run slower since it must // process a larger image. pyramid_up(img); // Now tell the face detector to give us a list of bounding boxes // around all the faces it can find in the image. std::vectorrectangle dets detector(img); cout Number of faces detected: dets.size() endl; // Now we show the image on the screen and the face detections as // red overlay boxes. win.clear_overlay(); win.set_image(img); win.add_overlay(dets, rgb_pixel(255, 0, 0)); cout Hit enter to process the next image... endl; cin.get(); } } } catch (exception e) { cout \nexception thrown! endl; cout e.what() endl; } } // ---------------------------------------------------------------------------------------- void test() { //test3DPointCloudPreview(); // pcl 3D点云展示 testDNNMetricLearning(); // k分类算法 /* done training label: 1 -0.151457 -0.076636 label: 1 -0.147003 -0.0889589 label: 2 0.0614508 0.529338 label: 2 0.136569 0.497333 label: 3 0.561938 0.00232342 label: 3 0.624519 -0.0519932 label: 4 -0.538596 -0.606784 label: 4 -0.429636 -0.711575 num_right: 28 num_wrong: 0 */ testRecognizeFace(D:/dlib/examples/faces); // 人脸识别 /* processing image File: 2007_007763.jpg Number of faces detected: 7 Hit enter to process the next image... processing image File: 2008_001009.jpg Number of faces detected: 2 Hit enter to process the next image... processing image File: 2008_001322.jpg Number of faces detected: 3 Hit enter to process the next image... */ }效果参考GitHub - davisking/dlib: A toolkit for making real world machine learning and data analysis applications in C创作不易小小的支持一下吧
RELATED

相关推荐

C++ 有向图拓扑排序算法

C++ 有向图拓扑排序算法

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/2 14:56:00
PIC18F57K42与MAX14431的SPI隔离方案设计与实践

PIC18F57K42与MAX14431的SPI隔离方案设计与实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/2 14:51:00
家政上门预约小程序源码实战:订单状态机、排班冲突与支付分账

家政上门预约小程序源码实战:订单状态机、排班冲突与支付分账

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/2 14:51:00
MORE NEWS

更多资讯

📰

物联网技术架构与行业应用深度解析

1. 物联网技术架构解析 物联网系统通常采用三层或四层架构设计。三层架构由下至上分别为感测层、网络层和应用层;四层架构则进一步将应用层拆分为平台工具层和应用服务层,这种划分能更精细地管理软件组件。在实际工业场景中,四层架构的采用率…

📰

libcurl 7.40 Windows预编译包VS工程接入与排错

简介:这是一份面向 Windows 平台 C/C 开发者的 libcurl 7.40 预编译依赖包,适合需要在 Visual Studio 各版本中快速接入 HTTP/HTTPS 请求、又不想自行处理 OpenSSL、zlib、libssh2 等依赖编译的开发者。压缩包共 17 个文件,约 1.23MB&#xf…

📰

从马尔可夫链到隐马尔可夫模型:序列建模的核心概念与工程判断

简介:围绕模式识别核心知识整理的PPT课件,面向人工智能、机器学习初学者及需要补充概率模型基础的读者,重点解决马尔科夫模型与隐马尔科夫模型概念抽象、公式与实例难以对应的问题。课件从马尔可夫性、马尔科夫链及转移概率矩阵讲起&#xff…

📰

工业机器人手眼协同技术:精准操作与动态避障实践

1. 项目背景与核心价值 速腾聚创手眼协同方案是工业自动化领域的一项前沿技术突破,这个项目在广东地区的调研考察由万祥军博士带队,属于国科院科研成果转化的重要实践案例。手眼协同技术本质上解决的是机器人在复杂环境中的精准操作问题——就像人类能够…

📰

Flynn分类法:计算机体系结构的四大基本类型解析

1. Flynn分类法概述Flynn分类法是计算机体系结构领域最基础也最重要的分类方法之一。1966年,斯坦福大学教授Michael J. Flynn在论文《Very High-Speed Computing Systems》中首次系统性地提出了这一分类框架。它通过两个关键维度——指令流(Instruction …

📰

Java H5微信支付APIv3实战:签名、回调幂等与查单补偿

简介:这份资源围绕 Java 后端接入微信 H5 支付展开,面向需要为电商网站、移动端网页补齐在线收款能力的 Java 开发者,也适合作为课程设计或小型项目中的支付模块参考。内容涵盖统一下单获取预支付标识、前端唤起支付、异步回调校验、订单查询…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬