学习深度学习已有一段时间了,总想着拿它做点什么,今天终于完成了一个基于caffe的人脸检测,这篇博文将告诉你怎样通过caffe一步步实现人脸检测。本文主要参考唐宇迪老师的教程,在这里感谢老师的辛勤付出。

传统机器学习方法实现人脸检测:

  人脸检测在opencv中已经帮我们实现了,我们要把它玩起来很简单,只需要简简单单的几行代码其实就可以搞定。(haarcascade_frontalface_alt.xml这个文件在opencv的安装目录下能找到,笔者的路径是:E:\opencv2.4.10\opencv\sources\data\haarcascades,大家可根据自己的安装路径找到)

  1. #include <opencv2\core\core.hpp>
  2. #include <opencv2\highgui\highgui.hpp>
  3. #include <opencv2\imgproc\imgproc.hpp>
  4. #include <opencv2\objdetect\objdetect.hpp>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. using namespace cv;
  10.  
  11. string xmlName = "haarcascade_frontalface_alt.xml";
  12. CascadeClassifier face_cascade;
  13. void detectFaces(Mat image);
  14.  
  15. int main()
  16. {
  17. Mat image = imread("kobe1.jpg");
  18. if (!image.data)
  19. {
  20. cout << "read image failed……" << endl;
  21. ;
  22. }
  23. face_cascade.load(xmlName);
  24. detectFaces(image);
  25. waitKey();
  26. }
  27.  
  28. void detectFaces(Mat image)
  29. {
  30. vector<Rect> faces;
  31. Mat face_gray;
  32. cvtColor(image, face_gray, CV_BGR2GRAY);
  33. face_cascade.detectMultiScale(face_gray, faces, , | CV_HAAR_SCALE_IMAGE, Size(, ));
  34. cout << faces.size() << endl;
  35. ; i < faces.size(); i++)
  36. {
  37. Rect r(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  38. rectangle(image, r, Scalar(, , ), );
  39. }
  40. namedWindow("face", CV_WINDOW_NORMAL);
  41. imshow("face", image);
  42. }

运行结果:

  

caffe实现人脸检测:

  我是在ubuntu16.04环境下完成的实验,渣渣笔记本有不起GPU跑训练,所有实验也是基于CPU的。要想把人脸检测玩起来,首先你得保证你的ubuntu已经安装了opencv和caffe,初次配这两个环境初学者往往会弄到吐血,而且还是吐老血,我自己已经记不清到底花了多久才把它们搞定(估计是我太怂,也许你很快就能弄好哟,加油)。这里给两个参考链接,opencv在ubuntu下的配置和测试:http://blog.csdn.net/a1429331875/article/details/31539129;ubuntu16.04上caffe的配置与安装(CPU ONLY):http://blog.csdn.net/u010402483/article/details/51506616;以上两个链接仅供参考,配置过程出了问题大家就多去网上搜解决方案吧,总会有人也遇到过和你一样的问题。配置好以后大家可以先跑跑MNIST手写字体识别这个案例吧,这个案例算是给自己的一个安慰。  到这里就已经默认大家环境已经配置好了。

  第一步:(这样写感觉很蠢,但还是写得尽量详细吧)在桌面或者你喜欢的路径下建一个文件夹,这个文件夹将用来存放我们实验中用到的所有东西。我自己是在桌面建了一个文件夹,取名:faceDetect

  第二步:获取人脸和非人脸图片当作训练集和验证集。首先我们一定要有样本,在本实验中我们的样本是一个二分类的样本,大家可以自行去网上找数据集,当然也可以给我发邮件(likai_uestc@163.com),我这里有数据集。数据集我们分训练集(trainData20000_20000)和验证集(testData1600_1600),trainData20000_20000文件夹和testData1600_1600文件夹我们把它们两个都放在faceDetect文件夹下,trainData20000_20000文件夹和testData1600_1600文件夹下又都放两个文件夹face和noface,人脸图片我们放在face文件夹下,非人脸图片我们放在noface文件夹下。此时的目录结构如下:

  

  第三步:给样本打标签。本实验中是人脸我们给的标签是1,不是人脸给标签0.训练样本的标签我们写入train.txt,验证样本的标签我们写入test.txt.  train.txt和test.txt文件我们放在faceDetect目录下。 txt文件中的内容如下:

  

  生成txt文件的内容的参考代码如下:(仅供参考)

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9. using namespace cv;
  10.  
  11. int main()
  12. {
  13. Directory dir;
  14. string basePath = "face";
  15. string exten = "*";
  16. bool addPath = true;
  17. vector<string> fileNames = dir.GetListFiles(basePath, exten, addPath);
  18. cout << fileNames.size() << endl;
  19. ofstream outData("train.txt");
  20. ; i < fileNames.size(); i++)
  21. {
  22. cout << fileNames[i] << endl;
  23. outData << fileNames[i] << " << endl;
  24. }
  25. outData.close();
  26. system("pause");
  27. ;
  28. }

  第四步:制作LMDB数据源。通过shell脚本制作,脚本文件名 face-lmdb.sh,face-lmdb.sh也放在faceDetect路径下。脚本的内容:

  重点关注第5,6,7,9,10,14,16,17,44,45,54,55行。第5第6行就是你faceDetect的路径,第7行是你caffe中tools的路径,第9第10行是训练样本和验证样本的路径,第14行为true表示对图片进行resize操作,第16,17行填写resize的大小,第44行和54行就是填标签文件,第45和55行是生成的lmdb文件存放的文件夹名,这两个文件夹不能自己手动提前建立。

  1. #!/usr/bin/env sh
  2. # Create the face_48 lmdb inputs
  3. # N.B. set the path to the face_48 train + val data dirs
  4.  
  5. EXAMPLE=/home/kobe/Desktop/faceDetect
  6. DATA=/home/kobe/Desktop/faceDetect
  7. TOOLS=/home/kobe/caffe/build/tools
  8.  
  9. TRAIN_DATA_ROOT=/home/kobe/Desktop/faceDetect/trainData20000_20000/
  10. VAL_DATA_ROOT=/home/kobe/Desktop/faceDetect/testData1600_1600/
  11.  
  12. # Set RESIZE= x . Leave as false if images have
  13. # already been resized using another tool.
  14. RESIZE=true
  15. if $RESIZE; then
  16. RESIZE_HEIGHT=227
  17. RESIZE_WIDTH=227
  18. else
  19. RESIZE_HEIGHT=
  20. RESIZE_WIDTH=
  21. fi
  22.  
  23. if [ ! -d "$TRAIN_DATA_ROOT" ]; then
  24. echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
  25. echo "Set the TRAIN_DATA_ROOT variable in create_face_48.sh to the path" \
  26. "where the face_48 training data is stored."
  27. exit
  28. fi
  29.  
  30. if [ ! -d "$VAL_DATA_ROOT" ]; then
  31. echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
  32. echo "Set the VAL_DATA_ROOT variable in create_face_48.sh to the path" \
  33. "where the face_48 validation data is stored."
  34. exit
  35. fi
  36.  
  37. echo "Creating train lmdb..."
  38.  
  39. GLOG_logtostderr= $TOOLS/convert_imageset \
  40. --resize_height=$RESIZE_HEIGHT \
  41. --resize_width=$RESIZE_WIDTH \
  42. --shuffle \
  43. $TRAIN_DATA_ROOT \
  44. $DATA/train.txt \
  45. $EXAMPLE/face_train_lmdb
  46.  
  47. echo "Creating val lmdb..."
  48.  
  49. GLOG_logtostderr= $TOOLS/convert_imageset \
  50. --resize_height=$RESIZE_HEIGHT \
  51. --resize_width=$RESIZE_WIDTH \
  52. --shuffle \
  53. $VAL_DATA_ROOT \
  54. $DATA/test.txt \
  55. $EXAMPLE/face_test_lmdb
  56.  
  57. echo "Done."
  58. Status API Training Shop Blog About

  现在在终端执行 ./face-lmdb.sh 命令即可,执行截图如下:

  

  第五步:准备网络模型文件(train.prototxt)及超参数文件(solver.prototxt)。train.prototxt和solver.prototxt文件也放在faceDetect路径下。网络模型各层参数详解及超参数文件详解可参考:https://wenku.baidu.com/view/f77c73d02f60ddccdb38a025.html。

  网络模型文件:

  1. ############################# DATA Layer #############################
  2.  
  3. name: "face_train_val"
  4.  
  5. layer {
  6.  
  7. top: "data"
  8.  
  9. top: "label"
  10.  
  11. name: "data"
  12.  
  13. type: "Data"
  14.  
  15. data_param {
  16.  
  17. source: "/home/kobe/Desktop/faceDetect/face_train_lmdb"#改成自己的路径
  18.  
  19. backend:LMDB
  20.  
  21. batch_size: 64
  22.  
  23. }
  24.  
  25. transform_param {
  26.  
  27. #mean_file: "/home/test/Downloads/caffe-master/data/ilsvrc12/imagenet_mean.binaryproto"
  28.  
  29. mirror: true
  30.  
  31. }
  32.  
  33. include: { phase: TRAIN }
  34.  
  35. }
  36.  
  37. layer {
  38.  
  39. top: "data"
  40.  
  41. top: "label"
  42.  
  43. name: "data"
  44.  
  45. type: "Data"
  46.  
  47. data_param {
  48.  
  49. source: "/home/kobe/Desktop/faceDetect/face_test_lmdb"#改成自己的路径
  50.  
  51. backend:LMDB
  52.  
  53. batch_size: 64
  54.  
  55. }
  56.  
  57. transform_param {
  58.  
  59. #mean_file: "/home/test/Downloads/caffe-master/data/ilsvrc12/imagenet_mean.binaryproto"
  60.  
  61. mirror: true
  62.  
  63. }
  64.  
  65. include: {
  66.  
  67. phase: TEST
  68.  
  69. }
  70.  
  71. }
  72.  
  73. layer {
  74.  
  75. name: "conv1"
  76.  
  77. type: "Convolution"
  78.  
  79. bottom: "data"
  80.  
  81. top: "conv1"
  82.  
  83. param {
  84.  
  85. lr_mult:
  86.  
  87. decay_mult:
  88.  
  89. }
  90.  
  91. param {
  92.  
  93. lr_mult:
  94.  
  95. decay_mult:
  96.  
  97. }
  98.  
  99. convolution_param {
  100.  
  101. num_output:
  102.  
  103. kernel_size:
  104.  
  105. stride:
  106.  
  107. weight_filler {
  108.  
  109. type: "gaussian"
  110.  
  111. std: 0.01
  112.  
  113. }
  114.  
  115. bias_filler {
  116.  
  117. type: "constant"
  118.  
  119. value:
  120.  
  121. }
  122.  
  123. }
  124.  
  125. }
  126.  
  127. layer {
  128.  
  129. name: "relu1"
  130.  
  131. type: "ReLU"
  132.  
  133. bottom: "conv1"
  134.  
  135. top: "conv1"
  136.  
  137. }
  138.  
  139. layer {
  140.  
  141. name: "norm1"
  142.  
  143. type: "LRN"
  144.  
  145. bottom: "conv1"
  146.  
  147. top: "norm1"
  148.  
  149. lrn_param {
  150.  
  151. local_size:
  152.  
  153. alpha: 0.0001
  154.  
  155. beta: 0.75
  156.  
  157. }
  158.  
  159. }
  160.  
  161. layer {
  162.  
  163. name: "pool1"
  164.  
  165. type: "Pooling"
  166.  
  167. bottom: "norm1"
  168.  
  169. top: "pool1"
  170.  
  171. pooling_param {
  172.  
  173. pool: MAX
  174.  
  175. kernel_size:
  176.  
  177. stride:
  178.  
  179. }
  180.  
  181. }
  182.  
  183. layer {
  184.  
  185. name: "conv2"
  186.  
  187. type: "Convolution"
  188.  
  189. bottom: "pool1"
  190.  
  191. top: "conv2"
  192.  
  193. param {
  194.  
  195. lr_mult:
  196.  
  197. decay_mult:
  198.  
  199. }
  200.  
  201. param {
  202.  
  203. lr_mult:
  204.  
  205. decay_mult:
  206.  
  207. }
  208.  
  209. convolution_param {
  210.  
  211. num_output:
  212.  
  213. pad:
  214.  
  215. kernel_size:
  216.  
  217. group:
  218.  
  219. weight_filler {
  220.  
  221. type: "gaussian"
  222.  
  223. std: 0.01
  224.  
  225. }
  226.  
  227. bias_filler {
  228.  
  229. type: "constant"
  230.  
  231. value: 0.1
  232.  
  233. }
  234.  
  235. }
  236.  
  237. }
  238.  
  239. layer {
  240.  
  241. name: "relu2"
  242.  
  243. type: "ReLU"
  244.  
  245. bottom: "conv2"
  246.  
  247. top: "conv2"
  248.  
  249. }
  250.  
  251. layer {
  252.  
  253. name: "norm2"
  254.  
  255. type: "LRN"
  256.  
  257. bottom: "conv2"
  258.  
  259. top: "norm2"
  260.  
  261. lrn_param {
  262.  
  263. local_size:
  264.  
  265. alpha: 0.0001
  266.  
  267. beta: 0.75
  268.  
  269. }
  270.  
  271. }
  272.  
  273. layer {
  274.  
  275. name: "pool2"
  276.  
  277. type: "Pooling"
  278.  
  279. bottom: "norm2"
  280.  
  281. top: "pool2"
  282.  
  283. pooling_param {
  284.  
  285. pool: MAX
  286.  
  287. kernel_size:
  288.  
  289. stride:
  290.  
  291. }
  292.  
  293. }
  294.  
  295. layer {
  296.  
  297. name: "conv3"
  298.  
  299. type: "Convolution"
  300.  
  301. bottom: "pool2"
  302.  
  303. top: "conv3"
  304.  
  305. param {
  306.  
  307. lr_mult:
  308.  
  309. decay_mult:
  310.  
  311. }
  312.  
  313. param {
  314.  
  315. lr_mult:
  316.  
  317. decay_mult:
  318.  
  319. }
  320.  
  321. convolution_param {
  322.  
  323. num_output:
  324.  
  325. pad:
  326.  
  327. kernel_size:
  328.  
  329. weight_filler {
  330.  
  331. type: "gaussian"
  332.  
  333. std: 0.01
  334.  
  335. }
  336.  
  337. bias_filler {
  338.  
  339. type: "constant"
  340.  
  341. value:
  342.  
  343. }
  344.  
  345. }
  346.  
  347. }
  348.  
  349. layer {
  350.  
  351. name: "relu3"
  352.  
  353. type: "ReLU"
  354.  
  355. bottom: "conv3"
  356.  
  357. top: "conv3"
  358.  
  359. }
  360.  
  361. layer {
  362.  
  363. name: "conv4"
  364.  
  365. type: "Convolution"
  366.  
  367. bottom: "conv3"
  368.  
  369. top: "conv4"
  370.  
  371. param {
  372.  
  373. lr_mult:
  374.  
  375. decay_mult:
  376.  
  377. }
  378.  
  379. param {
  380.  
  381. lr_mult:
  382.  
  383. decay_mult:
  384.  
  385. }
  386.  
  387. convolution_param {
  388.  
  389. num_output:
  390.  
  391. pad:
  392.  
  393. kernel_size:
  394.  
  395. group:
  396.  
  397. weight_filler {
  398.  
  399. type: "gaussian"
  400.  
  401. std: 0.01
  402.  
  403. }
  404.  
  405. bias_filler {
  406.  
  407. type: "constant"
  408.  
  409. value: 0.1
  410.  
  411. }
  412.  
  413. }
  414.  
  415. }
  416.  
  417. layer {
  418.  
  419. name: "relu4"
  420.  
  421. type: "ReLU"
  422.  
  423. bottom: "conv4"
  424.  
  425. top: "conv4"
  426.  
  427. }
  428.  
  429. layer {
  430.  
  431. name: "conv5"
  432.  
  433. type: "Convolution"
  434.  
  435. bottom: "conv4"
  436.  
  437. top: "conv5"
  438.  
  439. param {
  440.  
  441. lr_mult:
  442.  
  443. decay_mult:
  444.  
  445. }
  446.  
  447. param {
  448.  
  449. lr_mult:
  450.  
  451. decay_mult:
  452.  
  453. }
  454.  
  455. convolution_param {
  456.  
  457. num_output:
  458.  
  459. pad:
  460.  
  461. kernel_size:
  462.  
  463. group:
  464.  
  465. weight_filler {
  466.  
  467. type: "gaussian"
  468.  
  469. std: 0.01
  470.  
  471. }
  472.  
  473. bias_filler {
  474.  
  475. type: "constant"
  476.  
  477. value: 0.1
  478.  
  479. }
  480.  
  481. }
  482.  
  483. }
  484.  
  485. layer {
  486.  
  487. name: "relu5"
  488.  
  489. type: "ReLU"
  490.  
  491. bottom: "conv5"
  492.  
  493. top: "conv5"
  494.  
  495. }
  496.  
  497. layer {
  498.  
  499. name: "pool5"
  500.  
  501. type: "Pooling"
  502.  
  503. bottom: "conv5"
  504.  
  505. top: "pool5"
  506.  
  507. pooling_param {
  508.  
  509. pool: MAX
  510.  
  511. kernel_size:
  512.  
  513. stride:
  514.  
  515. }
  516.  
  517. }
  518.  
  519. layer {
  520.  
  521. name: "fc6"
  522.  
  523. type: "InnerProduct"
  524.  
  525. bottom: "pool5"
  526.  
  527. top: "fc6"
  528.  
  529. param {
  530.  
  531. lr_mult:
  532.  
  533. decay_mult:
  534.  
  535. }
  536.  
  537. param {
  538.  
  539. lr_mult:
  540.  
  541. decay_mult:
  542.  
  543. }
  544.  
  545. inner_product_param {
  546.  
  547. num_output:
  548.  
  549. weight_filler {
  550.  
  551. type: "gaussian"
  552.  
  553. std: 0.005
  554.  
  555. }
  556.  
  557. bias_filler {
  558.  
  559. type: "constant"
  560.  
  561. value: 0.1
  562.  
  563. }
  564.  
  565. }
  566.  
  567. }
  568.  
  569. layer {
  570.  
  571. name: "relu6"
  572.  
  573. type: "ReLU"
  574.  
  575. bottom: "fc6"
  576.  
  577. top: "fc6"
  578.  
  579. }
  580.  
  581. layer {
  582.  
  583. name: "drop6"
  584.  
  585. type: "Dropout"
  586.  
  587. bottom: "fc6"
  588.  
  589. top: "fc6"
  590.  
  591. dropout_param {
  592.  
  593. dropout_ratio: 0.5
  594.  
  595. }
  596.  
  597. }
  598.  
  599. layer {
  600.  
  601. name: "fc7"
  602.  
  603. type: "InnerProduct"
  604.  
  605. bottom: "fc6"
  606.  
  607. top: "fc7"
  608.  
  609. param {
  610.  
  611. lr_mult:
  612.  
  613. decay_mult:
  614.  
  615. }
  616.  
  617. param {
  618.  
  619. lr_mult:
  620.  
  621. decay_mult:
  622.  
  623. }
  624.  
  625. inner_product_param {
  626.  
  627. num_output:
  628.  
  629. weight_filler {
  630.  
  631. type: "gaussian"
  632.  
  633. std: 0.005
  634.  
  635. }
  636.  
  637. bias_filler {
  638.  
  639. type: "constant"
  640.  
  641. value: 0.1
  642.  
  643. }
  644.  
  645. }
  646.  
  647. }
  648.  
  649. layer {
  650.  
  651. name: "relu7"
  652.  
  653. type: "ReLU"
  654.  
  655. bottom: "fc7"
  656.  
  657. top: "fc7"
  658.  
  659. }
  660.  
  661. layer {
  662.  
  663. name: "drop7"
  664.  
  665. type: "Dropout"
  666.  
  667. bottom: "fc7"
  668.  
  669. top: "fc7"
  670.  
  671. dropout_param {
  672.  
  673. dropout_ratio: 0.5
  674.  
  675. }
  676.  
  677. }
  678.  
  679. layer {
  680.  
  681. name: "fc8-expr"
  682.  
  683. type: "InnerProduct"
  684.  
  685. bottom: "fc7"
  686.  
  687. top: "fc8-expr"
  688.  
  689. param {
  690.  
  691. lr_mult:
  692.  
  693. decay_mult:
  694.  
  695. }
  696.  
  697. param {
  698.  
  699. lr_mult:
  700.  
  701. decay_mult:
  702.  
  703. }
  704.  
  705. inner_product_param {
  706.  
  707. num_output:
  708.  
  709. weight_filler {
  710.  
  711. type: "gaussian"
  712.  
  713. std: 0.01
  714.  
  715. }
  716.  
  717. bias_filler {
  718.  
  719. type: "constant"
  720.  
  721. value:
  722.  
  723. }
  724.  
  725. }
  726.  
  727. }
  728.  
  729. layer {
  730.  
  731. name: "accuracy"
  732.  
  733. type: "Accuracy"
  734.  
  735. bottom: "fc8-expr"
  736.  
  737. bottom: "label"
  738.  
  739. top: "accuracy"
  740.  
  741. include {
  742.  
  743. phase: TEST
  744.  
  745. }
  746.  
  747. }
  748.  
  749. layer {
  750.  
  751. name: "loss"
  752.  
  753. type: "SoftmaxWithLoss"
  754.  
  755. bottom: "fc8-expr"
  756.  
  757. bottom: "label"
  758.  
  759. top: "loss"
  760.  
  761. }

  超参数文件:

  第1行网络模型文件的路径,第15行训练得到的模型的保存路径,在本实验中自己在faceDetect文件夹下建一个model文件夹用于保存得到的模型文件。net: "/home/kobe/Desktop/faceDetect/train.prototxt"

test_iter: 50
test_interval: 500
# lr for fine-tuning should be lower than when starting from scratch
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
# stepsize should also be lower, as we're closer to being done
stepsize: 20000
display: 100
max_iter: 100000
momentum: 0.9
weight_decay: 0.0005
snapshot: 10000
snapshot_prefix: "
/home/kobe/Desktop/faceDetect/model/"

# uncomment the following to default to CPU mode solving

solver_mode: CPU

  第六步:开始训练。运行train.sh脚本进行训练,train.sh也放在faceDetect路径下。脚本的内容:(根据自己的实际路径进行修改)

  1. #!/usr/bin/env sh
  2.  
  3. /home/kobe/caffe/build/tools/caffe train --solver=/home/kobe/Desktop/faceDetect/solver.prototxt #\
  4. #--weights=models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \
  5. #--gpu

   在终端中运行 ./train.sh 就可以开始训练了,训练很花时间的,根据机器配置不同所花时间也不同。训练截图如下:

  

  训练结束后,我们得到以下的文件(model下的文件),caffemodel结尾的文件就是我们最终需要的文件了:

  

  第七步:实现多尺度人脸检测。基于滑动窗口的人脸检测,在训练的时候样本已经resize成227*227,所以对于输入图片,我们在输入图片中截取227*227大小的窗口放入模型进行分类,依次这样进行窗口滑动,最终找出其中的人脸区域。但是图片中人脸并不一定就是227*227大小的,所以我们需要进行多尺度变换,所谓多尺度变换就是指对于输入图片我们进行放大和缩小变换,这样输入一张图片,就可以得到很多经过放大或缩小的图片了,把所有图片当作一组输入进行人脸检测。那么现在问题又来了,我们输入的是不同大小的图片,网络中有一个全连接层,全连接层的存在导致输入的图片大小必须一样大小,要解决这个问题我们的解决方法是把全连接层转换成全卷积层(可参考caffe官网进行操作)。

  转换过程中需要用到的两个deploy文件。

  全连接时使用(deploy.prototxt):

  1. name: "CaffeNet"
  2. input: "data"
  3. input_dim:
  4. input_dim:
  5. input_dim:
  6. input_dim:
  7. layer {
  8. name: "conv1"
  9. type: "Convolution"
  10. bottom: "data"
  11. top: "conv1"
  12. param {
  13. lr_mult:
  14. decay_mult:
  15. }
  16. param {
  17. lr_mult:
  18. decay_mult:
  19. }
  20. convolution_param {
  21. num_output:
  22. kernel_size:
  23. stride:
  24. weight_filler {
  25. type: "gaussian"
  26. std: 0.01
  27. }
  28. bias_filler {
  29. type: "constant"
  30. value:
  31. }
  32. }
  33. }
  34. layer {
  35. name: "relu1"
  36. type: "ReLU"
  37. bottom: "conv1"
  38. top: "conv1"
  39. }
  40. layer {
  41. name: "pool1"
  42. type: "Pooling"
  43. bottom: "conv1"
  44. top: "pool1"
  45. pooling_param {
  46. pool: MAX
  47. kernel_size:
  48. stride:
  49. }
  50. }
  51. layer {
  52. name: "norm1"
  53. type: "LRN"
  54. bottom: "pool1"
  55. top: "norm1"
  56. lrn_param {
  57. local_size:
  58. alpha: 0.0001
  59. beta: 0.75
  60. }
  61. }
  62. layer {
  63. name: "conv2"
  64. type: "Convolution"
  65. bottom: "norm1"
  66. top: "conv2"
  67. param {
  68. lr_mult:
  69. decay_mult:
  70. }
  71. param {
  72. lr_mult:
  73. decay_mult:
  74. }
  75. convolution_param {
  76. num_output:
  77. pad:
  78. kernel_size:
  79. group:
  80. weight_filler {
  81. type: "gaussian"
  82. std: 0.01
  83. }
  84. bias_filler {
  85. type: "constant"
  86. value:
  87. }
  88. }
  89. }
  90. layer {
  91. name: "relu2"
  92. type: "ReLU"
  93. bottom: "conv2"
  94. top: "conv2"
  95. }
  96. layer {
  97. name: "pool2"
  98. type: "Pooling"
  99. bottom: "conv2"
  100. top: "pool2"
  101. pooling_param {
  102. pool: MAX
  103. kernel_size:
  104. stride:
  105. }
  106. }
  107. layer {
  108. name: "norm2"
  109. type: "LRN"
  110. bottom: "pool2"
  111. top: "norm2"
  112. lrn_param {
  113. local_size:
  114. alpha: 0.0001
  115. beta: 0.75
  116. }
  117. }
  118. layer {
  119. name: "conv3"
  120. type: "Convolution"
  121. bottom: "norm2"
  122. top: "conv3"
  123. param {
  124. lr_mult:
  125. decay_mult:
  126. }
  127. param {
  128. lr_mult:
  129. decay_mult:
  130. }
  131. convolution_param {
  132. num_output:
  133. pad:
  134. kernel_size:
  135. weight_filler {
  136. type: "gaussian"
  137. std: 0.01
  138. }
  139. bias_filler {
  140. type: "constant"
  141. value:
  142. }
  143. }
  144. }
  145. layer {
  146. name: "relu3"
  147. type: "ReLU"
  148. bottom: "conv3"
  149. top: "conv3"
  150. }
  151. layer {
  152. name: "conv4"
  153. type: "Convolution"
  154. bottom: "conv3"
  155. top: "conv4"
  156. param {
  157. lr_mult:
  158. decay_mult:
  159. }
  160. param {
  161. lr_mult:
  162. decay_mult:
  163. }
  164. convolution_param {
  165. num_output:
  166. pad:
  167. kernel_size:
  168. group:
  169. weight_filler {
  170. type: "gaussian"
  171. std: 0.01
  172. }
  173. bias_filler {
  174. type: "constant"
  175. value:
  176. }
  177. }
  178. }
  179. layer {
  180. name: "relu4"
  181. type: "ReLU"
  182. bottom: "conv4"
  183. top: "conv4"
  184. }
  185. layer {
  186. name: "conv5"
  187. type: "Convolution"
  188. bottom: "conv4"
  189. top: "conv5"
  190. param {
  191. lr_mult:
  192. decay_mult:
  193. }
  194. param {
  195. lr_mult:
  196. decay_mult:
  197. }
  198. convolution_param {
  199. num_output:
  200. pad:
  201. kernel_size:
  202. group:
  203. weight_filler {
  204. type: "gaussian"
  205. std: 0.01
  206. }
  207. bias_filler {
  208. type: "constant"
  209. value:
  210. }
  211. }
  212. }
  213. layer {
  214. name: "relu5"
  215. type: "ReLU"
  216. bottom: "conv5"
  217. top: "conv5"
  218. }
  219. layer {
  220. name: "pool5"
  221. type: "Pooling"
  222. bottom: "conv5"
  223. top: "pool5"
  224. pooling_param {
  225. pool: MAX
  226. kernel_size:
  227. stride:
  228. }
  229. }
  230. layer {
  231. name: "fc6"
  232. type: "InnerProduct"
  233. bottom: "pool5"
  234. top: "fc6"
  235. param {
  236. lr_mult:
  237. decay_mult:
  238. }
  239. param {
  240. lr_mult:
  241. decay_mult:
  242. }
  243. inner_product_param {
  244. num_output:
  245. weight_filler {
  246. type: "gaussian"
  247. std: 0.005
  248. }
  249. bias_filler {
  250. type: "constant"
  251. value:
  252. }
  253. }
  254. }
  255. layer {
  256. name: "relu6"
  257. type: "ReLU"
  258. bottom: "fc6"
  259. top: "fc6"
  260. }
  261. layer {
  262. name: "drop6"
  263. type: "Dropout"
  264. bottom: "fc6"
  265. top: "fc6"
  266. dropout_param {
  267. dropout_ratio: 0.5
  268. }
  269. }
  270. layer {
  271. name: "fc7"
  272. type: "InnerProduct"
  273. bottom: "fc6"
  274. top: "fc7"
  275. # Note that lr_mult can be to disable any fine-tuning of this, and any other, layer
  276. param {
  277. lr_mult:
  278. decay_mult:
  279. }
  280. param {
  281. lr_mult:
  282. decay_mult:
  283. }
  284. inner_product_param {
  285. num_output:
  286. weight_filler {
  287. type: "gaussian"
  288. std: 0.005
  289. }
  290. bias_filler {
  291. type: "constant"
  292. value:
  293. }
  294. }
  295. }
  296. layer {
  297. name: "relu7"
  298. type: "ReLU"
  299. bottom: "fc7"
  300. top: "fc7"
  301. }
  302. layer {
  303. name: "drop7"
  304. type: "Dropout"
  305. bottom: "fc7"
  306. top: "fc7"
  307. dropout_param {
  308. dropout_ratio: 0.5
  309. }
  310. }
  311. layer {
  312. name: "fc8_flickr"
  313. type: "InnerProduct"
  314. bottom: "fc7"
  315. top: "fc8_flickr"
  316. # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained
  317. param {
  318. lr_mult:
  319. decay_mult:
  320. }
  321. param {
  322. lr_mult:
  323. decay_mult:
  324. }
  325. inner_product_param {
  326. num_output:
  327. weight_filler {
  328. type: "gaussian"
  329. std: 0.01
  330. }
  331. bias_filler {
  332. type: "constant"
  333. value:
  334. }
  335. }
  336. }
  337. layer {
  338. name: "prob"
  339. type: "Softmax"
  340. bottom: "fc8_flickr"
  341. top: "prob"
  342. }

  全卷积时使用(deploy_full_conv.prototxt):

  1. name: "CaffeNet_full_conv"
  2. input: "data"
  3. input_dim:
  4. input_dim:
  5. input_dim:
  6. input_dim:
  7.  
  8. layer {
  9. name: "conv1"
  10. type: "Convolution"
  11. bottom: "data"
  12. top: "conv1"
  13. param {
  14. lr_mult:
  15. decay_mult:
  16. }
  17. param {
  18. lr_mult:
  19. decay_mult:
  20. }
  21. convolution_param {
  22. num_output:
  23. kernel_size:
  24. stride:
  25. weight_filler {
  26. type: "gaussian"
  27. std: 0.01
  28. }
  29. bias_filler {
  30. type: "constant"
  31. value:
  32. }
  33. }
  34. }
  35. layer {
  36. name: "relu1"
  37. type: "ReLU"
  38. bottom: "conv1"
  39. top: "conv1"
  40. }
  41. layer {
  42. name: "pool1"
  43. type: "Pooling"
  44. bottom: "conv1"
  45. top: "pool1"
  46. pooling_param {
  47. pool: MAX
  48. kernel_size:
  49. stride:
  50. }
  51. }
  52. layer {
  53. name: "norm1"
  54. type: "LRN"
  55. bottom: "pool1"
  56. top: "norm1"
  57. lrn_param {
  58. local_size:
  59. alpha: 0.0001
  60. beta: 0.75
  61. }
  62. }
  63. layer {
  64. name: "conv2"
  65. type: "Convolution"
  66. bottom: "norm1"
  67. top: "conv2"
  68. param {
  69. lr_mult:
  70. decay_mult:
  71. }
  72. param {
  73. lr_mult:
  74. decay_mult:
  75. }
  76. convolution_param {
  77. num_output:
  78. pad:
  79. kernel_size:
  80. group:
  81. weight_filler {
  82. type: "gaussian"
  83. std: 0.01
  84. }
  85. bias_filler {
  86. type: "constant"
  87. value:
  88. }
  89. }
  90. }
  91. layer {
  92. name: "relu2"
  93. type: "ReLU"
  94. bottom: "conv2"
  95. top: "conv2"
  96. }
  97. layer {
  98. name: "pool2"
  99. type: "Pooling"
  100. bottom: "conv2"
  101. top: "pool2"
  102. pooling_param {
  103. pool: MAX
  104. kernel_size:
  105. stride:
  106. }
  107. }
  108. layer {
  109. name: "norm2"
  110. type: "LRN"
  111. bottom: "pool2"
  112. top: "norm2"
  113. lrn_param {
  114. local_size:
  115. alpha: 0.0001
  116. beta: 0.75
  117. }
  118. }
  119. layer {
  120. name: "conv3"
  121. type: "Convolution"
  122. bottom: "norm2"
  123. top: "conv3"
  124. param {
  125. lr_mult:
  126. decay_mult:
  127. }
  128. param {
  129. lr_mult:
  130. decay_mult:
  131. }
  132. convolution_param {
  133. num_output:
  134. pad:
  135. kernel_size:
  136. weight_filler {
  137. type: "gaussian"
  138. std: 0.01
  139. }
  140. bias_filler {
  141. type: "constant"
  142. value:
  143. }
  144. }
  145. }
  146. layer {
  147. name: "relu3"
  148. type: "ReLU"
  149. bottom: "conv3"
  150. top: "conv3"
  151. }
  152. layer {
  153. name: "conv4"
  154. type: "Convolution"
  155. bottom: "conv3"
  156. top: "conv4"
  157. param {
  158. lr_mult:
  159. decay_mult:
  160. }
  161. param {
  162. lr_mult:
  163. decay_mult:
  164. }
  165. convolution_param {
  166. num_output:
  167. pad:
  168. kernel_size:
  169. group:
  170. weight_filler {
  171. type: "gaussian"
  172. std: 0.01
  173. }
  174. bias_filler {
  175. type: "constant"
  176. value:
  177. }
  178. }
  179. }
  180. layer {
  181. name: "relu4"
  182. type: "ReLU"
  183. bottom: "conv4"
  184. top: "conv4"
  185. }
  186. layer {
  187. name: "conv5"
  188. type: "Convolution"
  189. bottom: "conv4"
  190. top: "conv5"
  191. param {
  192. lr_mult:
  193. decay_mult:
  194. }
  195. param {
  196. lr_mult:
  197. decay_mult:
  198. }
  199. convolution_param {
  200. num_output:
  201. pad:
  202. kernel_size:
  203. group:
  204. weight_filler {
  205. type: "gaussian"
  206. std: 0.01
  207. }
  208. bias_filler {
  209. type: "constant"
  210. value:
  211. }
  212. }
  213. }
  214. layer {
  215. name: "relu5"
  216. type: "ReLU"
  217. bottom: "conv5"
  218. top: "conv5"
  219. }
  220. layer {
  221. name: "pool5"
  222. type: "Pooling"
  223. bottom: "conv5"
  224. top: "pool5"
  225. pooling_param {
  226. pool: MAX
  227. kernel_size:
  228. stride:
  229. }
  230. }
  231. layer {
  232. name: "fc6-conv"
  233. type: "Convolution"
  234. bottom: "pool5"
  235. top: "fc6-conv"
  236. param {
  237. lr_mult:
  238. decay_mult:
  239. }
  240. param {
  241. lr_mult:
  242. decay_mult:
  243. }
  244. convolution_param {
  245. num_output:
  246. kernel_size:
  247. weight_filler {
  248. type: "gaussian"
  249. std: 0.005
  250. }
  251. bias_filler {
  252. type: "constant"
  253. value:
  254. }
  255. }
  256. }
  257. layer {
  258. name: "relu6"
  259. type: "ReLU"
  260. bottom: "fc6-conv"
  261. top: "fc6-conv"
  262. }
  263. layer {
  264. name: "drop6"
  265. type: "Dropout"
  266. bottom: "fc6-conv"
  267. top: "fc6-conv"
  268. dropout_param {
  269. dropout_ratio: 0.5
  270. }
  271. }
  272. layer {
  273. name: "fc7-conv"
  274. type: "Convolution"
  275. bottom: "fc6-conv"
  276. top: "fc7-conv"
  277. param {
  278. lr_mult:
  279. decay_mult:
  280. }
  281. param {
  282. lr_mult:
  283. decay_mult:
  284. }
  285. convolution_param {
  286. num_output:
  287. kernel_size:
  288. weight_filler {
  289. type: "gaussian"
  290. std: 0.005
  291. }
  292. bias_filler {
  293. type: "constant"
  294. value:
  295. }
  296. }
  297. }
  298. layer {
  299. name: "relu7"
  300. type: "ReLU"
  301. bottom: "fc7-conv"
  302. top: "fc7-conv"
  303. }
  304. layer {
  305. name: "drop7"
  306. type: "Dropout"
  307. bottom: "fc7-conv"
  308. top: "fc7-conv"
  309. dropout_param {
  310. dropout_ratio: 0.5
  311. }
  312. }
  313. layer {
  314. name: "fc8-conv"
  315. type: "Convolution"
  316. bottom: "fc7-conv"
  317. top: "fc8-conv"
  318. param {
  319. lr_mult:
  320. decay_mult:
  321. }
  322. param {
  323. lr_mult:
  324. decay_mult:
  325. }
  326. convolution_param {
  327. num_output:
  328. kernel_size:
  329. weight_filler {
  330. type: "gaussian"
  331. std: 0.01
  332. }
  333. bias_filler {
  334. type: "constant"
  335. value:
  336. }
  337. }
  338. }
  339. layer {
  340. name: "prob"
  341. type: "Softmax"
  342. bottom: "fc8-conv"
  343. top: "prob"
  344. }

  全连接转全卷积参考代码:

  1. # -*- coding: utf- -*-
  2. import caffe
  3. #matplotlib inline
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. import matplotlib.cbook as cbook
  7. #import Image
  8. import sys
  9. import os
  10. from math import pow
  11. from PIL import Image, ImageDraw, ImageFont
  12. import cv2
  13. import math
  14. import random
  15. caffe_root = '/home/kobe/caffe/'
  16.  
  17. sys.path.insert(, caffe_root + 'python')
  18. os.environ['
  19.  
  20. caffe.set_mode_cpu()
  21. #####################################################################################################
  22. # Load the original network and extract the fully connected layers' parameters.
  23. net = caffe.Net('/home/kobe/Desktop/faceDetect/project/deploy.prototxt',
  24. '/home/kobe/Desktop/faceDetect/model/_iter_50000.caffemodel',
  25. caffe.TEST)
  26. params = ['fc6', 'fc7', 'fc8_flickr']
  27. # fc_params = {name: (weights, biases)}
  28. fc_params = {pr: (net.].data, net.].data) for pr in params}
  29.  
  30. for fc in params:
  31. print ].shape, fc_params[fc][].shape)
  32. #######################################################################################################
  33. # Load the fully convolutional network to transplant the parameters.
  34. net_full_conv = caffe.Net('/home/kobe/Desktop/faceDetect/project/deploy_full_conv.prototxt',
  35. '/home/kobe/Desktop/faceDetect/model/_iter_50000.caffemodel',
  36. caffe.TEST)
  37. params_full_conv = ['fc6-conv', 'fc7-conv', 'fc8-conv']
  38. # conv_params = {name: (weights, biases)}
  39. conv_params = {pr: (net_full_conv.].data, net_full_conv.].data) for pr in params_full_conv}
  40.  
  41. for conv in params_full_conv:
  42. print ].shape, conv_params[conv][].shape)
  43. #############################################################################################################
  44. for pr, pr_conv in zip(params, params_full_conv):
  45. conv_params[pr_conv][].flat = fc_params[pr][].flat # flat unrolls the arrays
  46. conv_params[pr_conv][][...] = fc_params[pr][]
  47. ##############################################################################################################
  48. net_full_conv.save('/home/kobe/Desktop/faceDetect/alexnet_iter_50000_full_conv.caffemodel')

  转换后得到另一个模型(alexnet_iter_50000_full_conv.caffemodel).

  实现多尺度人脸检测代码:

  1. # -*- coding: utf- -*-
  2. import caffe
  3. #matplotlib inline
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. import matplotlib.cbook as cbook
  7. #import Image
  8. import sys
  9. import os
  10. from math import pow
  11. from PIL import Image, ImageDraw, ImageFont
  12. import cv2
  13. import math
  14. import random
  15. caffe_root = '/home/kobe/caffe/'
  16.  
  17. sys.path.insert(, caffe_root + 'python')
  18. os.environ['
  19.  
  20. caffe.set_mode_cpu()
  21.  
  22. #非极大值抑制算法NMS
  23. class Point(object):
  24. def __init__(self, x, y):
  25. self.x = x
  26. self.y = y
  27. def calculateDistance(x1,y1,x2,y2): #计算人脸框的对角线距离
  28. dist = math.sqrt((x2 - x1)** + (y2 - y1)**)
  29. return dist
  30.  
  31. def range_overlap(a_min, a_max, b_min, b_max):
  32.  
  33. return (a_min <= b_max) and (b_min <= a_max)
  34.  
  35. def rect_overlaps(r1,r2):
  36. return range_overlap(r1.left, r1.right, r2.left, r2.right) and range_overlap(r1.bottom, r1.top, r2.bottom, r2.top)
  37.  
  38. def rect_merge(r1,r2, mergeThresh):
  39.  
  40. if rect_overlaps(r1,r2):
  41. # dist = calculateDistance((r1.left + r1.right)/, (r1.top + r1.bottom)/, (r2.left + r2.right)/, (r2.top + r2.bottom)/)
  42. SI= abs(min(r1.right, r2.right) - max(r1.left, r2.left)) * abs(max(r1.bottom, r2.bottom) - min(r1.top, r2.top))
  43. SA = abs(r1.right - r1.left)*abs(r1.bottom - r1.top)
  44. SB = abs(r2.right - r2.left)*abs(r2.bottom - r2.top)
  45. S=SA+SB-SI
  46. ratio = float(SI) / float(S)
  47. if ratio > mergeThresh :
  48.  
  49. class Rect(object):
  50. def __init__(self, p1, p2): #p1和p2为对角线上的两个点
  51. '''Store the top, bottom, left and right values for points
  52. p1 and p2 are the (corners) in either order
  53. '''
  54. self.left = min(p1.x, p2.x) #?????
  55. self.right = max(p1.x, p2.x)
  56. self.bottom = min(p1.y, p2.y)
  57. self.top = max(p1.y, p2.y)
  58.  
  59. def __str__(self):
  60. return "Rect[%d, %d, %d, %d]" % ( self.left, self.top, self.right, self.bottom )
  61. def nms_average(boxes, groupThresh=, overlapThresh=0.2):
  62. rects = []
  63. temp_boxes = []
  64. weightslist = []
  65. new_rects = []
  66. for i in range(len(boxes)):
  67. ] > 0.2:
  68. rects.append([boxes[i,], boxes[i,], boxes[i,]-boxes[i,], boxes[i,]-boxes[i,]])
  69.  
  70. rects, weights = cv2.groupRectangles(rects, groupThresh, overlapThresh) #函数解释http://blog.csdn.net/nongfu_spring/article/details/38977833
  71.  
  72. rectangles = []
  73. for i in range(len(rects)):
  74.  
  75. testRect = Rect( Point(rects[i,], rects[i,]), Point(rects[i,]+rects[i,], rects[i,]+rects[i,]))
  76. rectangles.append(testRect)
  77. clusters = []
  78. for rect in rectangles:
  79. matched =
  80. for cluster in clusters:
  81. if (rect_merge( rect, cluster , 0.2) ):
  82. matched=
  83. cluster.left = (cluster.left + rect.left )/
  84. cluster.right = ( cluster.right+ rect.right )/
  85. cluster.top = ( cluster.top+ rect.top )/
  86. cluster.bottom = ( cluster.bottom+ rect.bottom )/
  87.  
  88. if ( not matched ):
  89. clusters.append( rect )
  90. result_boxes = []
  91. for i in range(len(clusters)):
  92.  
  93. result_boxes.append([clusters[i].left, clusters[i].bottom, clusters[i].right, clusters[i].top, ])
  94.  
  95. return result_boxes
  96.  
  97. def generateBoundingBox(featureMap, scale): #由于做了scale变换,所以在这里还要将坐标反变换回去
  98. boundingBox = [] #存储候选框,以及属于人脸的概率
  99. stride = #感受野的大小,filter大小,这个需要自己不断地去调整;
  100. cellSize = #人脸框的大小,它这里是认为特征图上的一块区域的prob大于95%,就以那个点在原始图像中相应的位置作为人脸框的左上角点,然后框出候选框,但这么做感觉会使候选框变多
  101. #遍历最终的特征图,寻找属于人脸的概率大于95%的那些区域,加上Box
  102. for (x,y), prob in np.ndenumerate(featureMap):
  103. if(prob >= 0.95):
  104. boundingBox.append([float(stride * y)/ scale,
  105. float(x * stride)/scale,
  106. )/scale,
  107. )/scale, prob])
  108.  
  109. return boundingBox
  110.  
  111. def face_detection(imgFile):
  112. net_full_conv = caffe.Net(os.path.join(caffe_root, 'faceDetect', 'deploy_full_conv.prototxt'),
  113. os.path.join(caffe_root, 'faceDetect', 'alexnet_iter_50000_full_conv.caffemodel'),
  114. caffe.TEST)#全卷积网络(导入训练好的模型和deploy配置文件)
  115. randNum = random.randint(,) #设置一个在1到10000之间的随机数
  116.  
  117. scales = [] #设置几个scale,组成图像金字塔
  118. factor = 0.793700526 #图像放大或者缩小的一个因子(经验值)
  119.  
  120. img = cv2.imread(imgFile) #读入测试图像
  121.  
  122. largest = min(, /max(img.shape[:])) #设定做scale变幻时最大的scale
  123. scale = largest
  124. minD = largest*min(img.shape[:]) #设定最小的scale
  125. : #只要最小的边做完最大的scale变换后大于227,之前得到的largest就可以作为最大的scale来用,并依此乘上factor,加入到scale列表中
  126. scales.append(scale)
  127. scale *= factor
  128. minD *= factor
  129.  
  130. total_boxes = [] #存储所有的候选框
  131. #进行多尺度的人脸检测
  132. for scale in scales:
  133.  
  134. scale_img = cv2.resize(img,((] * scale), ] * scale)))) #调整图像的长和高
  135. cv2.imwrite('/home/kobe/Desktop/faceDetect/project/1.jpg',scale_img) #保存图像
  136. #图像预处理
  137. im = caffe.io.load_image('/home/kobe/Desktop/faceDetect/project/1.jpg') #得到的特征值是0到1之间的小数
  138. net_full_conv.blobs[,,scale_img.shape[],scale_img.shape[]) #blobs['data']指data层,字典用法;同时由于图像大小发生了变化,data层的输入接口也要发生相应的变化
  139. transformer = caffe.io.Transformer({'data': net_full_conv.blobs['data'].data.shape}) #设定图像的shape格式
  140. transformer.set_mean('data', np.load(caffe_root +
  141. ).mean()) #减去均值操作
  142. transformer.set_transpose(,,)) #move image channels to outermost dimension
  143. transformer.set_channel_swap(,,)) #swap channels from RGB to BGR
  144. transformer.set_raw_scale(,] to [,]
  145.  
  146. out = net_full_conv.forward_all(data=np.asarray([transformer.preprocess('data', im)])) #进行一次前向传播,out包括所有经过每一层后的特征图,其中元素为[(x,y),prob](特征图中的每一个小区域都代表一个概率)
  147.  
  148. boxes = generateBoundingBox(,], scale) #输出两类的可能性,并经过筛选获得候选框
  149. if(boxes):
  150. total_boxes.extend(boxes) #将每次scale变换后的图片得到的候选框存进total_boxes中
  151.  
  152. boxes_nms = np.array(total_boxes)
  153. true_boxes = nms_average(boxes_nms, , 0.2) #利用非极大值算法过滤出人脸概率最大的框
  154. if not true_boxes == []:
  155. (x1, y1, x2, y2) = true_boxes[][:-]
  156. print (x1, y1, x2, y2)
  157. cv2.rectangle(img, (,,),thickness = )
  158. cv2.imwrite('/home/kobe/Desktop/faceDetect/project/result.jpg',img)
  159.  
  160. imgFile = '/home/kobe/Desktop/faceDetect/project/test.jpg'
  161. print('start detect')
  162. face_detection(imgFile)
  163. print('finish detect')
  164. #image_file = cbook.get_sample_data(imgFile)
  165. img = plt.imread(imgFile)
  166. plt.imshow(img)
  167. plt.show()

运行结果:

用caffe一步一步实现人脸检测的更多相关文章

  1. 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑

    阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...

  2. 如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成

    阅读目录 前言 建模 实现 结语 一.前言 前面几篇已经实现了一个基本的购买+售价计算的过程,这次再让售价丰满一些,增加一个会员价的概念.会员价在现在的主流电商中,是一个不大常见的模式,其带来的问题是 ...

  3. 如何一步一步用DDD设计一个电商网站(十)—— 一个完整的购物车

     阅读目录 前言 回顾 梳理 实现 结语 一.前言 之前的文章中已经涉及到了购买商品加入购物车,购物车内购物项的金额计算等功能.本篇准备把剩下的购物车的基本概念一次处理完. 二.回顾 在动手之前我对之 ...

  4. 如何一步一步用DDD设计一个电商网站(七)—— 实现售价上下文

    阅读目录 前言 明确业务细节 建模 实现 结语 一.前言 上一篇我们已经确立的购买上下文和销售上下文的交互方式,传送门在此:http://www.cnblogs.com/Zachary-Fan/p/D ...

  5. 如何一步一步用DDD设计一个电商网站(六)—— 给购物车加点料,集成售价上下文

    阅读目录 前言 如何在一个项目中实现多个上下文的业务 售价上下文与购买上下文的集成 结语 一.前言 前几篇已经实现了一个最简单的购买过程,这次开始往这个过程中增加一些东西.比如促销.会员价等,在我们的 ...

  6. 如何一步一步用DDD设计一个电商网站(五)—— 停下脚步,重新出发

    阅读目录 前言 单元测试 纠正错误,重新出发 结语 一.前言 实际编码已经写了2篇了,在这过程中非常感谢有听到观点不同的声音,借着这个契机,今天这篇就把大家提出的建议一个个的过一遍,重新整理,重新出发 ...

  7. 如何一步一步用DDD设计一个电商网站(四)—— 把商品卖给用户

    阅读目录 前言 怎么卖 领域服务的使用 回到现实 结语 一.前言 上篇中我们讲述了“把商品卖给用户”中的商品和用户的初步设计.现在把剩余的“卖”这个动作给做了.这里提醒一下,正常情况下,我们的每一步业 ...

  8. 如何一步一步用DDD设计一个电商网站(三)—— 初涉核心域

    一.前言 结合我们本次系列的第一篇博文中提到的上下文映射图(传送门:如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念),得知我们这个电商网站的核心域就是销售子域.因为电子商务是以信息网络 ...

  9. 一步一步使用ABP框架搭建正式项目系列教程

    研究ABP框架好多天了,第一次看到这个框架的名称到现在已经很久了,但由于当时内功有限,看不太懂,所以就只是大概记住了ABP这个名字.最近几天,看到了园友@阳光铭睿的系列ABP教程,又点燃了我内心要研究 ...

随机推荐

  1. Coursera 机器学习笔记(八)

    主要为第十周内容:大规模机器学习.案例.总结 (一)随机梯度下降法 如果有一个大规模的训练集,普通的批量梯度下降法需要计算整个训练集的误差的平方和,如果学习方法需要迭代20次,这已经是非常大的计算代价 ...

  2. 利用document的readyState去实现页面加载中的效果

    打开新的网页时,为了增强友好性体验,告知用户网页正在加载数据需要呈现一个"页面加载中"之类的提示,只需要利用document就可以实现. 实现示例代码如下: <style&g ...

  3. CSS3的使用方法解析

    自己过去有段时间使用CSS3开发过一些小的部件和效果,但是由于太久没有再次去使用,导致当自己再次去使用的时候我就需要去翻手册重新找一次然后按着方法使用才可以. 现在我就把这份CSS3的使用技巧展示给各 ...

  4. 利用浏览器查找font-family的css编码

    提供一种利用Chrome快速查找字体编码的小技巧 打开浏览器,按下键盘F12 点击Console控制台 输入escape("要查询的字体中文名称")(注意:括号与引号都是英文输入法 ...

  5. WPF Dashboard仪表盘控件的实现

    1.确定控件应该继承的基类 从表面上看,目前WPF自带常用控件中,没有一个是接近这个表盘控件的,但将该控件拆分就能够发现,该控件的每个子部分都是在WPF中存在的,因此我们需要将各个子控件组合才能形成这 ...

  6. Spring mvc 中使用 kaptcha 验证码

    生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的开源项目.使用Kaptcha 生成验证码十分简单并且参数可以进行自定义.只需添加jar包配置下就可 ...

  7. WCF入门, 到创建一个简单的WCF应用程序

    什么是WCF?  WCF, 英文全称(windows Communication Foundation) , 即为windows通讯平台. windows想到这里大家都知道了 , WCF也正是由微软公 ...

  8. ionic+AnjularJs实现省市县三级联动效果

    建议对ionic和AnjularJs有一定了解的人可以用到,很多时候我们要用到选择省份.城市.区县的功能,现在就跟着我来实现这个功能吧,用很少的代码(我这里是根据客户的要求,只显示想要显示的部分省份和 ...

  9. CoolBlog开发笔记第1课:项目分析

    首先说一下CoolBlog开发笔记是我制作的<Django实战项目>系列教程基础篇的内容,使用Django来开发一个酷炫的个人博客,涉及的知识包括项目的分析,环境的搭建,模型和视图定义等等 ...

  10. 从Owin到System.Web.Http.Owin的HttpMessageHandlerAdapter看适配器模式

    .mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...