尝试使用Caffe的人脸识别模型,进行人脸识别
一、数据准备
res10_300x300_ssd_iter_140000.caffemodel
deploy.prototxt.txt
下载地址:https://download.csdn.net/download/bashendixie5/13455671
二、Python版本
# import the necessary packages
import numpy as np
import argparse
import cv2
# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('C:/Users/xiaomao/Desktop/deploy.prototxt', 'C:/Users/xiaomao/Desktop/res10_300x300_ssd_iter_140000.caffemodel')
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread('C:/Users/xiaomao/Desktop/1.jpg')
(h, w) = image.shape[:2]
#调整大小版本
#blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
#不调整大小版本
blob = cv2.dnn.blobFromImage(image, 1.0, None, (104.0, 177.0, 123.0))
# pass the blob through the network and obtain the detections and
# predictions
print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the
# prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
#
if confidence > 0.13:
# compute the (x, y)-coordinates of the bounding box for the
# object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the bounding box of the face along with the associated
# probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)




三、C++版本
#include <fstream>
#include <sstream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
using namespace dnn;
int main(int argc, char** argv)
{
Net net = readNetFromCaffe("C:/Users/xiaomao/Desktop/deploy.prototxt",
"C:/Users/xiaomao/Desktop/res10_300x300_ssd_iter_140000.caffemodel");
Mat image = imread("C:/Users/xiaomao/Desktop/5.jpg");
Mat image1;
//调整图像大小
//resize(image, image1, Size(300, 300));
//Mat blob = blobFromImage(image1, 1, Size(300, 300), Scalar(104, 117, 123));
//不调整图像大小
Mat blob = blobFromImage(image, 1, Size(), Scalar(104, 117, 123));
net.setInput(blob);
Mat detections = net.forward();
Mat detectionMat(detections.size[2], detections.size[3], CV_32F, detections.ptr<float>());
for (int i = 0; i < detectionMat.rows; i++)
{
//自定义阈值
if (detectionMat.at<float>(i, 2) >= 0.14)
{
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols);
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows);
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols);
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows);
Rect object((int)xLeftBottom, (int)yLeftBottom,
(int)(xRightTop - xLeftBottom),
(int)(yRightTop - yLeftBottom));
rectangle(image, object, Scalar(0, 255, 0));
}
}
//显示图片
imshow("img", image);
waitKey(0);
return 0;
}
扫描二维码关注公众号,回复:
12726479 查看本文章
