基于Opencv的视频人脸检测程序源代码(可运行)

合集下载

关于Opencv实现人脸检测的阐述

关于Opencv实现人脸检测的阐述

关于Opencv实现人脸检测的阐述最近用Opencv开发一个人脸检测的小程序,结构并不复杂,但对于Opencv初学者来说还是具有一定的引导意义。

接下来对于程序开发中出现的一些问题进行简单讨论。

一、图像采集。

图像既可以从摄像头设备中读取,也可以从磁盘中加载,两者方法大同小异。

以摄像头为例,Opencv对于摄像头的操作同matlab中一样,是通过一个简单的API 函数实现的,具体如下:CvCapture* m_pCapture;m_pCapture = cvCreateCameraCapture(0);IplImage* m_pFrameImage;m_pFrameImage = cvQueryFrame(m_pCapture);首先是创建一个视频流结构体指针m_pCapture,之后调用cvCreateCameraCapture(0)函数将结构体与相应视频输入设备关联,若只有一个视频输入设备(如笔记本摄像头),建议参数给0,若有多个视频设备则应给对应的ID号。

关联完成后,调用cvQueryFrame(m_pCapture);得到m_pCapture结构体中视频流的下一帧图像,存储在图像指针对应的区域,至此,完成摄像头图像采集。

从磁盘中读取图像过程相对复杂一点,需要用到MFC中关于文件及文件夹读取的知识。

具体讲用两条途径,一是定位指定文件夹,继而读取文件夹下的所有图像文件;二是直接定位文件,继而读取相应文件。

读取文件夹的具体代码如下:BROWSEINFO bi;//用来存储用户选中的目录信息TCHAR name[MAX_PATH];//存储路径name[0]='d';ZeroMemory(&bi,sizeof(BROWSEINFO));//清空目录对应的内存bi.hwndOwner=GetSafeHwnd();//得到窗口句柄bi.pszDisplayName=name;BIF_BROWSEINCLUDEFILES;//这句话是什么意思bi.lpszTitle=_T("Select folder");//对话框标题bi.ulFlags=0x80;//设置对话框形式LPITEMIDLIST idl=SHBrowseForFolder(&bi);//返回所选中文件夹的IDif(idl==NULL)return;SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));//将文件信息格式化存储到对应缓冲区中str.ReleaseBuffer();//与GerBuffer配合使用,清空内存m_Path=str;//将路径存储在m_path中if(str.GetAt(str.GetLength()-1)!='\\')m_Path+="\\";UpdateData(FALSE);文件夹读取过程中关键函数为SHBrowseForFolder,这个函数有什么样作用以及具体用法网上都有具体的帖子和博客进行说明,这里不做赘述,最终文件夹路径存储在变量m_Path中。

OpenCV实现人脸检测功能

OpenCV实现人脸检测功能

OpenCV实现⼈脸检测功能本⽂实例为⼤家分享了OpenCV实现⼈脸检测功能的具体代码,供⼤家参考,具体内容如下1、HAAR级联检测#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;#include <iostream>#include <cstdlib>using namespace std;int main(int artc, char** argv) {face_detect_haar();waitKey(0);return 0;}void face_detect_haar() {CascadeClassifier faceDetector;std::string haar_data_file = "./models/haarcascades/haarcascade_frontalface_alt_tree.xml";faceDetector.load(haar_data_file);vector<Rect> faces;//VideoCapture capture(0);VideoCapture capture("./video/test.mp4");Mat frame, gray;int count=0;while (capture.read(frame)) {int64 start = getTickCount();if (frame.empty()){break;}// ⽔平镜像调整// flip(frame, frame, 1);imshow("input", frame);if (frame.channels() == 4)cvtColor(frame, frame, COLOR_BGRA2BGR);cvtColor(frame, gray, COLOR_BGR2GRAY);equalizeHist(gray, gray);faceDetector.detectMultiScale(gray, faces, 1.2, 1, 0, Size(30, 30), Size(400, 400));for (size_t t = 0; t < faces.size(); t++) {count++;rectangle(frame, faces[t], Scalar(0, 255, 0), 2, 8, 0);}float fps = getTickFrequency() / (getTickCount() - start);ostringstream ss;ss.str("");ss << "FPS: " << fps << " ; inference time: " << time << " ms";putText(frame, ss.str(), Point(20, 20), 0, 0.75, Scalar(0, 0, 255), 2, 8);imshow("haar_face_detection", frame);if (waitKey(1) >= 0) break;}printf("total face: %d\n", count);}2、 DNN⼈脸检测#include <opencv2/dnn.hpp>#include <opencv2/opencv.hpp>using namespace cv;using namespace cv::dnn;#include <iostream>#include <cstdlib>using namespace std;const size_t inWidth = 300;const size_t inHeight = 300;const double inScaleFactor = 1.0;const Scalar meanVal(104.0, 177.0, 123.0);const float confidenceThreshold = 0.7;void face_detect_dnn();void mtcnn_demo();int main(int argc, char** argv){face_detect_dnn();waitKey(0);return 0;}void face_detect_dnn() {//这⾥采⽤tensorflow模型std::string modelBinary = "./models/dnn/face_detector/opencv_face_detector_uint8.pb"; std::string modelDesc = "./models/dnn/face_detector/opencv_face_detector.pbtxt";// 初始化⽹络dnn::Net net = readNetFromTensorflow(modelBinary, modelDesc);net.setPreferableBackend(DNN_BACKEND_OPENCV);net.setPreferableTarget(DNN_TARGET_CPU);if (net.empty()){printf("Load models fail...\n");return;}// 打开摄像头// VideoCapture capture(0);VideoCapture capture("./video/test.mp4");if (!capture.isOpened()) {printf("Don't find video...\n");return;}Mat frame;int count=0;while (capture.read(frame)) {int64 start = getTickCount();if (frame.empty()){break;}// ⽔平镜像调整// flip(frame, frame, 1);imshow("input", frame);if (frame.channels() == 4)cvtColor(frame, frame, COLOR_BGRA2BGR);// 输⼊数据调整Mat inputBlob = blobFromImage(frame, inScaleFactor,Size(inWidth, inHeight), meanVal, false, false);net.setInput(inputBlob, "data");// ⼈脸检测Mat detection = net.forward("detection_out");vector<double> layersTimings;double freq = getTickFrequency() / 1000;double time = net.getPerfProfile(layersTimings) / freq;Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>()); ostringstream ss;for (int i = 0; i < detectionMat.rows; i++){// 置信度 0~1之间float confidence = detectionMat.at<float>(i, 2);if (confidence > confidenceThreshold){count++;int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);Rect object((int)xLeftBottom, (int)yLeftBottom,(int)(xRightTop - xLeftBottom),(int)(yRightTop - yLeftBottom));rectangle(frame, object, Scalar(0, 255, 0));ss << confidence;std::string conf(ss.str());std::string label = "Face: " + conf;int baseLine = 0;Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),Size(labelSize.width, labelSize.height + baseLine)),Scalar(255, 255, 255), FILLED);putText(frame, label, Point(xLeftBottom, yLeftBottom),FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));}}float fps = getTickFrequency() / (getTickCount() - start);ss.str("");ss << "FPS: " << fps << " ; inference time: " << time << " ms";putText(frame, ss.str(), Point(20, 20), 0, 0.75, Scalar(0, 0, 255), 2, 8);imshow("dnn_face_detection", frame);if (waitKey(1) >= 0) break;}printf("total face: %d\n", count);}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

Python+OpenCV进行人脸面部表情识别

Python+OpenCV进行人脸面部表情识别

Python+OpenCV进⾏⼈脸⾯部表情识别⽬录前⾔⼀、图⽚预处理⼆、数据集划分三、识别笑脸四、Dlib提取⼈脸特征识别笑脸和⾮笑脸前⾔环境搭建可查看数据如下:⼀、图⽚预处理import dlib # ⼈脸识别的库dlibimport numpy as np # 数据处理的库numpyimport cv2 # 图像处理的库OpenCvimport os# dlib预测器detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')# 读取图像的路径path_read = ".\ImageFiles\\files"num=0for file_name in os.listdir(path_read):#aa是图⽚的全路径aa=(path_read +"/"+file_name)#读⼊的图⽚的路径中含⾮英⽂img=cv2.imdecode(np.fromfile(aa, dtype=np.uint8), cv2.IMREAD_UNCHANGED)#获取图⽚的宽⾼img_shape=img.shapeimg_height=img_shape[0]img_width=img_shape[1]# ⽤来存储⽣成的单张⼈脸的路径path_save=".\ImageFiles\\files1"# dlib检测dets = detector(img,1)print("⼈脸数:", len(dets))for k, d in enumerate(dets):if len(dets)>1:continuenum=num+1# 计算矩形⼤⼩# (x,y), (宽度width, ⾼度height)pos_start = tuple([d.left(), d.top()])pos_end = tuple([d.right(), d.bottom()])# 计算矩形框⼤⼩height = d.bottom()-d.top()width = d.right()-d.left()# 根据⼈脸⼤⼩⽣成空的图像img_blank = np.zeros((height, width, 3), np.uint8)for i in range(height):if d.top()+i>=img_height:# 防⽌越界continuefor j in range(width):if d.left()+j>=img_width:# 防⽌越界continueimg_blank[i][j] = img[d.top()+i][d.left()+j]img_blank = cv2.resize(img_blank, (200, 200), interpolation=cv2.INTER_CUBIC)cv2.imencode('.jpg', img_blank)[1].tofile(path_save+"\\"+"file"+str(num)+".jpg") # 正确⽅法运⾏结果:⼆、数据集划分import os, shutil# 原始数据集路径original_dataset_dir = '.\ImageFiles\\files1'# 新的数据集base_dir = '.\ImageFiles\\files2'os.mkdir(base_dir)# 训练图像、验证图像、测试图像的⽬录train_dir = os.path.join(base_dir, 'train')os.mkdir(train_dir)validation_dir = os.path.join(base_dir, 'validation')os.mkdir(validation_dir)test_dir = os.path.join(base_dir, 'test')os.mkdir(test_dir)train_cats_dir = os.path.join(train_dir, 'smile')os.mkdir(train_cats_dir)train_dogs_dir = os.path.join(train_dir, 'unsmile')os.mkdir(train_dogs_dir)validation_cats_dir = os.path.join(validation_dir, 'smile')os.mkdir(validation_cats_dir)validation_dogs_dir = os.path.join(validation_dir, 'unsmile')os.mkdir(validation_dogs_dir)test_cats_dir = os.path.join(test_dir, 'smile')os.mkdir(test_cats_dir)test_dogs_dir = os.path.join(test_dir, 'unsmile')os.mkdir(test_dogs_dir)# 复制1000张笑脸图⽚到train_c_dirfnames = ['file{}.jpg'.format(i) for i in range(1,900)]for fname in fnames:src = os.path.join(original_dataset_dir, fname)dst = os.path.join(train_cats_dir, fname)shutil.copyfile(src, dst)fnames = ['file{}.jpg'.format(i) for i in range(900, 1350)]for fname in fnames:src = os.path.join(original_dataset_dir, fname)dst = os.path.join(validation_cats_dir, fname)shutil.copyfile(src, dst)# Copy next 500 cat images to test_cats_dirfnames = ['file{}.jpg'.format(i) for i in range(1350, 1800)]for fname in fnames:src = os.path.join(original_dataset_dir, fname)dst = os.path.join(test_cats_dir, fname)shutil.copyfile(src, dst)fnames = ['file{}.jpg'.format(i) for i in range(2127,3000)]for fname in fnames:src = os.path.join(original_dataset_dir, fname)dst = os.path.join(train_dogs_dir, fname)shutil.copyfile(src, dst)# Copy next 500 dog images to validation_dogs_dirfnames = ['file{}.jpg'.format(i) for i in range(3000,3304)]for fname in fnames:src = os.path.join(original_dataset_dir, fname)dst = os.path.join(validation_dogs_dir, fname)shutil.copyfile(src, dst)# # Copy next 500 dog images to test_dogs_dir# fnames = ['file{}.jpg'.format(i) for i in range(3000,3878)]# for fname in fnames:# src = os.path.join(original_dataset_dir, fname)# dst = os.path.join(test_dogs_dir, fname)# shutil.copyfile(src, dst)运⾏结果:三、识别笑脸模式构建:#创建模型from keras import layersfrom keras import modelsmodel = models.Sequential()model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3))) model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(128, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(128, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Flatten())model.add(layers.Dense(512, activation='relu'))model.add(layers.Dense(1, activation='sigmoid'))model.summary()#查看进⾏归⼀化#归⼀化from keras import optimizerspile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc'])from keras.preprocessing.image import ImageDataGeneratortrain_datagen = ImageDataGenerator(rescale=1./255)validation_datagen=ImageDataGenerator(rescale=1./255)test_datagen = ImageDataGenerator(rescale=1./255)train_generator = train_datagen.flow_from_directory(# ⽬标⽂件⽬录train_dir,#所有图⽚的size必须是150x150target_size=(150, 150),batch_size=20,# Since we use binary_crossentropy loss, we need binary labelsclass_mode='binary')validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=20,class_mode='binary')test_generator = test_datagen.flow_from_directory(test_dir,target_size=(150, 150),batch_size=20,class_mode='binary')for data_batch, labels_batch in train_generator:print('data batch shape:', data_batch.shape)print('labels batch shape:', labels_batch)break#'smile': 0, 'unsmile': 1增强数据#数据增强datagen = ImageDataGenerator(rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')#数据增强后图⽚变化import matplotlib.pyplot as plt# This is module with image preprocessing utilitiesfrom keras.preprocessing import imagetrain_smile_dir = './ImageFiles//files2//train//smile/'fnames = [os.path.join(train_smile_dir, fname) for fname in os.listdir(train_smile_dir)] img_path = fnames[3]img = image.load_img(img_path, target_size=(150, 150))x = image.img_to_array(img)x = x.reshape((1,) + x.shape)i = 0for batch in datagen.flow(x, batch_size=1):plt.figure(i)imgplot = plt.imshow(image.array_to_img(batch[0]))i += 1if i % 4 == 0:breakplt.show()创建⽹络:#创建⽹络model = models.Sequential()model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(150, 150, 3))) model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(128, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(128, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Flatten())model.add(layers.Dropout(0.5))model.add(layers.Dense(512, activation='relu'))model.add(layers.Dense(1, activation='sigmoid'))pile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4),metrics=['acc'])#归⼀化处理train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,)test_datagen = ImageDataGenerator(rescale=1./255)train_generator = train_datagen.flow_from_directory(# This is the target directorytrain_dir,# All images will be resized to 150x150target_size=(150, 150),batch_size=32,# Since we use binary_crossentropy loss, we need binary labelsclass_mode='binary')validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=32,class_mode='binary')history = model.fit_generator(train_generator,steps_per_epoch=100,epochs=60,validation_data=validation_generator,validation_steps=50)model.save('smileAndUnsmile1.h5')#数据增强过后的训练集与验证集的精确度与损失度的图形acc = history.history['acc']val_acc = history.history['val_acc']loss = history.history['loss']val_loss = history.history['val_loss']epochs = range(len(acc))plt.plot(epochs, acc, 'bo', label='Training acc')plt.plot(epochs, val_acc, 'b', label='Validation acc')plt.title('Training and validation accuracy')plt.legend()plt.figure()plt.plot(epochs, loss, 'bo', label='Training loss')plt.plot(epochs, val_loss, 'b', label='Validation loss')plt.title('Training and validation loss')plt.legend()plt.show()单张图⽚测试:# 单张图⽚进⾏判断是笑脸还是⾮笑脸import cv2from keras.preprocessing import imagefrom keras.models import load_modelimport numpy as np#加载模型model = load_model('smileAndUnsmile1.h5')#本地图⽚路径img_path='test.jpg'img = image.load_img(img_path, target_size=(150, 150))img_tensor = image.img_to_array(img)/255.0img_tensor = np.expand_dims(img_tensor, axis=0)prediction =model.predict(img_tensor)print(prediction)if prediction[0][0]>0.5:result='⾮笑脸'else:result='笑脸'print(result)摄像头测试:#检测视频或者摄像头中的⼈脸import cv2from keras.preprocessing import imagefrom keras.models import load_modelimport numpy as npimport dlibfrom PIL import Imagemodel = load_model('smileAndUnsmile1.h5')detector = dlib.get_frontal_face_detector()video=cv2.VideoCapture(0)font = cv2.FONT_HERSHEY_SIMPLEXdef rec(img):gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)dets=detector(gray,1)if dets is not None:for face in dets:left=face.left()top=face.top()right=face.right()bottom=face.bottom()cv2.rectangle(img,(left,top),(right,bottom),(0,255,0),2)img1=cv2.resize(img[top:bottom,left:right],dsize=(150,150))img1=cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)img1 = np.array(img1)/255.img_tensor = img1.reshape(-1,150,150,3)prediction =model.predict(img_tensor)if prediction[0][0]>0.5:result='unsmile'else:result='smile'cv2.putText(img, result, (left,top), font, 2, (0, 255, 0), 2, cv2.LINE_AA)cv2.imshow('Video', img)while video.isOpened():res, img_rd = video.read()if not res:breakrec(img_rd)if cv2.waitKey(1) & 0xFF == ord('q'):breakvideo.release()cv2.destroyAllWindows()运⾏结果:四、Dlib提取⼈脸特征识别笑脸和⾮笑脸import cv2 # 图像处理的库 OpenCvimport dlib # ⼈脸识别的库 dlibimport numpy as np # 数据处理的库 numpyclass face_emotion():def __init__(self):self.detector = dlib.get_frontal_face_detector()self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")self.cap = cv2.VideoCapture(0)self.cap.set(3, 480)t = 0def learning_face(self):line_brow_x = []line_brow_y = []while(self.cap.isOpened()):flag, im_rd = self.cap.read()k = cv2.waitKey(1)# 取灰度img_gray = cv2.cvtColor(im_rd, cv2.COLOR_RGB2GRAY)faces = self.detector(img_gray, 0)font = cv2.FONT_HERSHEY_SIMPLEX# 如果检测到⼈脸if(len(faces) != 0):# 对每个⼈脸都标出68个特征点for i in range(len(faces)):for k, d in enumerate(faces):cv2.rectangle(im_rd, (d.left(), d.top()), (d.right(), d.bottom()), (0,0,255)) self.face_width = d.right() - d.left()shape = self.predictor(im_rd, d)mouth_width = (shape.part(54).x - shape.part(48).x) / self.face_widthmouth_height = (shape.part(66).y - shape.part(62).y) / self.face_width brow_sum = 0frown_sum = 0for j in range(17, 21):brow_sum += (shape.part(j).y - d.top()) + (shape.part(j + 5).y - d.top()) frown_sum += shape.part(j + 5).x - shape.part(j).xline_brow_x.append(shape.part(j).x)line_brow_y.append(shape.part(j).y)tempx = np.array(line_brow_x)tempy = np.array(line_brow_y)z1 = np.polyfit(tempx, tempy, 1)self.brow_k = -round(z1[0], 3)brow_height = (brow_sum / 10) / self.face_width # 眉⽑⾼度占⽐brow_width = (frown_sum / 5) / self.face_width # 眉⽑距离占⽐eye_sum = (shape.part(41).y - shape.part(37).y + shape.part(40).y - shape.part(38).y +shape.part(47).y - shape.part(43).y + shape.part(46).y - shape.part(44).y)eye_hight = (eye_sum / 4) / self.face_widthif round(mouth_height >= 0.03) and eye_hight<0.56:cv2.putText(im_rd, "smile", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0), 2, 4)if round(mouth_height<0.03) and self.brow_k>-0.3:cv2.putText(im_rd, "unsmile", (d.left(), d.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0), 2, 4)cv2.putText(im_rd, "Face-" + str(len(faces)), (20,50), font, 0.6, (0,0,255), 1, cv2.LINE_AA)else:cv2.putText(im_rd, "No Face", (20,50), font, 0.6, (0,0,255), 1, cv2.LINE_AA)im_rd = cv2.putText(im_rd, "S: screenshot", (20,450), font, 0.6, (255,0,255), 1, cv2.LINE_AA)im_rd = cv2.putText(im_rd, "Q: quit", (20,470), font, 0.6, (255,0,255), 1, cv2.LINE_AA)if (cv2.waitKey(1) & 0xFF) == ord('s'):t += 1cv2.imwrite("screenshoot" + str(t) + ".jpg", im_rd)# 按下 q 键退出if (cv2.waitKey(1)) == ord('q'):break# 窗⼝显⽰cv2.imshow("Face Recognition", im_rd)self.cap.release()cv2.destroyAllWindows()if __name__ == "__main__":my_face = face_emotion()my_face.learning_face()运⾏结果:以上就是Python+OpenCV进⾏⼈脸⾯部表情识别的详细内容,更多关于Python OpenCV 表情识别的资料请关注其它相关⽂章!。

python使用opencv进行人脸识别

python使用opencv进行人脸识别

python使用opencv进行人脸识别欢迎来到小码哥的博客博客搬家啦/RvFZs2cpython使用opencv进行人脸识别环境ubuntu 12.04 LTSpython 2.7.3opencv 2.3.1-7安装依赖sudo apt-get install libopencv-*sudo apt-get install python-opencvsudo apt-get install python-numpy示例代码#!/usr/bin/env python#coding=utf-8import osfrom PIL import Image, ImageDrawimport cvdef detect_object(image):'''检测图片,获取人脸在图片中的坐标'''grayscale = cv.CreateImage((image.width, image.height), 8, 1)cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)cascade =cv.Load("/usr/share/opencv/haarcascades/haarcascade_frontalfa ce_alt_tree.xml")rect = cv.HaarDetectObjects(grayscale, cascade,cv.CreateMemStorage(), 1.1, 2,cv.CV_HAAR_DO_CANNY_PRUNING, (20,20))result = []for r in rect:result.append((r[0][0], r[0][1], r[0][0]+r[0][2],r[0][1]+r[0][3]))return resultdef process(infile):'''在原图上框出头像并且截取每个头像到单独文件夹''' image = cv.LoadImage(infile);if image:faces = detect_object(image)im = Image.open(infile)path = os.path.abspath(infile)save_path = os.path.splitext(path)[0]+"_face"try:os.mkdir(save_path)except:passif faces:draw = ImageDraw.Draw(im)count = 0for f in faces:count += 1draw.rectangle(f, outline=(255, 0, 0))a = im.crop(f)file_name =os.path.join(save_path,str(count)+".jpg")# print file_namea.save(file_name)drow_save_path = os.path.join(save_path,"out.jpg")im.save(drow_save_path, "JPEG", quality=80) else:print "Error: cannot detect faces on %s" % infileif __name__ == "__main__":process("./opencv_in.jpg")转换效果原图:转换后使用感受对于大部分图像来说,只要是头像是正面的,没有被阻挡,识别基本没问题,准确性还是很高的。

OpenCV人脸识别C++实例代码

OpenCV人脸识别C++实例代码

OpenCV⼈脸识别C++实例代码#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/core/core.hpp>#include <opencv2/objdetect/objdetect.hpp>using namespace cv;using namespace std;void detectAndDraw( Mat& img, CascadeClassifier& cascade,CascadeClassifier& nestedCascade,double scale, bool tryflip );int main(){//VideoCapture cap(0); //打开默认摄像头//if(!cap.isOpened())//{// return -1;//}Mat frame;Mat edges;CascadeClassifier cascade, nestedCascade;bool stop = false;//训练好的⽂件名称,放置在可执⾏⽂件同⽬录下cascade.load("D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");nestedCascade.load("D:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");frame = imread("E:\\tmpimg\\hezhao.jpg");detectAndDraw( frame, cascade, nestedCascade,2,0 );waitKey();//while(!stop)//{// cap>>frame;// detectAndDraw( frame, cascade, nestedCascade,2,0 );// if(waitKey(30) >=0)// stop = true;//}return0;}void detectAndDraw( Mat& img, CascadeClassifier& cascade,CascadeClassifier& nestedCascade,double scale, bool tryflip ){int i = 0;double t = 0;//建⽴⽤于存放⼈脸的向量容器vector<Rect> faces, faces2;//定义⼀些颜⾊,⽤来标⽰不同的⼈脸const static Scalar colors[] = {CV_RGB(0,0,255),CV_RGB(0,128,255),CV_RGB(0,255,255),CV_RGB(0,255,0),CV_RGB(255,128,0),CV_RGB(255,255,0),CV_RGB(255,0,0),CV_RGB(255,0,255)} ;//建⽴缩⼩的图⽚,加快检测速度//nt cvRound (double value) 对⼀个double型的数进⾏四舍五⼊,并返回⼀个整型数!Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );//转成灰度图像,Harr特征基于灰度图cvtColor( img, gray, CV_BGR2GRAY );imshow("灰度",gray);//改变图像⼤⼩,使⽤双线性差值resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );imshow("缩⼩尺⼨",smallImg);//变换后的图像进⾏直⽅图均值化处理equalizeHist( smallImg, smallImg );imshow("直⽅图均值处理",smallImg);//程序开始和结束插⼊此函数获取时间,经过计算求得算法执⾏时间t = (double)cvGetTickCount();//检测⼈脸//detectMultiScale函数中smallImg表⽰的是要检测的输⼊图像为smallImg,faces表⽰检测到的⼈脸⽬标序列,1.1表⽰//每次图像尺⼨减⼩的⽐例为1.1,2表⽰每⼀个⽬标⾄少要被检测到3次才算是真的⽬标(因为周围的像素和不同的窗⼝⼤//⼩都可以检测到⼈脸),CV_HAAR_SCALE_IMAGE表⽰不是缩放分类器来检测,⽽是缩放图像,Size(30, 30)为⽬标的//最⼩最⼤尺⼨cascade.detectMultiScale( smallImg, faces,1.1, 2, 0//|CV_HAAR_FIND_BIGGEST_OBJECT//|CV_HAAR_DO_ROUGH_SEARCH|CV_HAAR_SCALE_IMAGE,Size(30, 30));//如果使能,翻转图像继续检测if( tryflip ){flip(smallImg, smallImg, 1);imshow("反转图像",smallImg);cascade.detectMultiScale( smallImg, faces2,1.1, 2, 0//|CV_HAAR_FIND_BIGGEST_OBJECT//|CV_HAAR_DO_ROUGH_SEARCH|CV_HAAR_SCALE_IMAGE,Size(30, 30) );for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ ){faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));}}t = (double)cvGetTickCount() - t;// qDebug( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ ){Mat smallImgROI;vector<Rect> nestedObjects;Point center;Scalar color = colors[i%8];int radius;double aspect_ratio = (double)r->width/r->height;if( 0.75 < aspect_ratio && aspect_ratio < 1.3 ){//标⽰⼈脸时在缩⼩之前的图像上标⽰,所以这⾥根据缩放⽐例换算回去center.x = cvRound((r->x + r->width*0.5)*scale);center.y = cvRound((r->y + r->height*0.5)*scale);radius = cvRound((r->width + r->height)*0.25*scale);circle( img, center, radius, color, 3, 8, 0 );}elserectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),color, 3, 8, 0);if( nestedCascade.empty() )continue;smallImgROI = smallImg(*r);//同样⽅法检测⼈眼nestedCascade.detectMultiScale( smallImgROI, nestedObjects,1.1, 2, 0//|CV_HAAR_FIND_BIGGEST_OBJECT//|CV_HAAR_DO_ROUGH_SEARCH//|CV_HAAR_DO_CANNY_PRUNING|CV_HAAR_SCALE_IMAGE,Size(30, 30) );for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ ) {center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);radius = cvRound((nr->width + nr->height)*0.25*scale);circle( img, center, radius, color, 3, 8, 0 );}}imshow( "识别结果", img );}opencv 连接器配置[debug]opencv_ml2413d.libopencv_calib3d2413d.libopencv_contrib2413d.libopencv_core2413d.libopencv_features2d2413d.lib opencv_flann2413d.libopencv_gpu2413d.libopencv_highgui2413d.libopencv_imgproc2413d.libopencv_legacy2413d.libopencv_objdetect2413d.lib opencv_ts2413d.libopencv_video2413d.libopencv_nonfree2413d.libopencv_ocl2413d.libopencv_photo2413d.libopencv_stitching2413d.lib opencv_superres2413d.lib opencv_videostab2413d.lib [release]opencv_ml2413.libopencv_calib3d2413.libopencv_contrib2413.libopencv_core2413.libopencv_features2d2413.lib opencv_flann2413.libopencv_gpu2413.libopencv_highgui2413.libopencv_imgproc2413.libopencv_legacy2413.libopencv_objdetect2413.libopencv_ts2413.libopencv_video2413.libopencv_nonfree2413.libopencv_ocl2413.libopencv_photo2413.libopencv_stitching2413.libopencv_superres2413.libopencv_videostab2413.lib// 根据你的版本批量替换2413版本号。

python基于opencv实现人脸识别

python基于opencv实现人脸识别

python基于opencv实现⼈脸识别将opencv中haarcascade_frontalface_default.xml⽂件下载到本地,我们调⽤它辅助进⾏⼈脸识别。

识别图像中的⼈脸#coding:utf-8import cv2 as cv# 读取原始图像img = cv.imread('face.png')# 调⽤熟悉的⼈脸分类器识别特征类型# ⼈脸 - haarcascade_frontalface_default.xml# ⼈眼 - haarcascade_eye.xml# 微笑 - haarcascade_smile.xmlface_detect = cv.CascadeClassifier('haarcascade_frontalface_default.xml')gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 检查⼈脸按照1.1倍放到周围最⼩像素为5face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)print ('识别⼈脸的信息:',face_zone)# 绘制矩形和圆形检测⼈脸for x, y, w, h in face_zone:# 绘制矩形⼈脸区域 thickness表⽰线的粗细cv.rectangle(img, pt1=(x, y), pt2=(x+w, y+h),color=[0,0,255], thickness=2)# 绘制圆形⼈脸区域 radius表⽰半径cv.circle(img, center=(x+w//2, y+h//2), radius=w//2, color=[0,255,0], thickness=2)# 设置图⽚可以⼿动调节⼤⼩dWindow("Easmount-CSDN", 0)# 显⽰图⽚cv.imshow("Easmount-CSDN", img)# 等待显⽰设置任意键退出程序cv.waitKey(0)cv.destroyAllWindows()注意,此算法只能检测正脸,并且任何算法都有⼀定的准确率。

基于OpenCV的人脸识别技术研究与实现(C语言)

基于OpenCV的人脸识别技术研究与实现(C语言)

基于OpenCV的人脸识别技术研究与实现(C语言)一、引言人脸识别技术是近年来备受关注的领域之一,随着计算机视觉和人工智能的发展,人脸识别技术在各个领域得到了广泛的应用。

本文将重点介绍基于OpenCV库的人脸识别技术研究与实现,使用C语言进行编程实现。

二、OpenCV简介OpenCV是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。

它支持多种编程语言,包括C++、Python等。

在本文中,我们将使用C语言结合OpenCV库来实现人脸识别技术。

三、人脸检测人脸检测是人脸识别技术的第一步,通过检测输入图像中的人脸位置来进行后续的识别工作。

OpenCV提供了Haar级联分类器来进行人脸检测,我们可以利用该分类器来实现简单而有效的人脸检测功能。

四、人脸特征提取在进行人脸识别之前,需要对检测到的人脸进行特征提取。

常用的方法包括主成分分析(PCA)和线性判别分析(LDA)等。

通过提取人脸的特征向量,可以将其表示为一个高维向量,便于后续的比对和识别。

五、人脸识别算法在得到人脸特征向量后,我们可以使用不同的算法来进行人脸识别。

常见的算法包括最近邻算法(KNN)、支持向量机(SVM)和深度学习方法等。

这些算法可以根据实际需求选择合适的模型进行训练和测试。

六、实验设计与实现在本节中,我们将介绍如何使用OpenCV库和C语言来实现基于人脸识别技术的实验。

首先,我们需要准备训练数据集和测试数据集,并对数据集进行预处理和特征提取。

然后,我们可以选择合适的算法模型进行训练,并对测试数据集进行验证和评估。

七、实验结果与分析通过实验我们可以得到不同算法在人脸识别任务上的表现结果,并对比它们的准确率、召回率等指标。

通过分析实验结果,可以帮助我们选择最适合当前任务需求的人脸识别算法,并对其性能进行优化和改进。

八、应用与展望人脸识别技术在安防监控、人机交互、身份认证等领域有着广泛的应用前景。

未来随着技术的不断发展,人脸识别技术将会变得更加智能化和便捷化,为社会生活带来更多便利。

OpenCV Haar人脸检测的代码分析

OpenCV Haar人脸检测的代码分析

OpenCV Haar人脸检测的代码分析/* cascade or tree of stage classifiers */int flags; /* signature */int count; /* number of stages */CvSize orig_window_size; /* original object size (the cascade is trained for) *//* these two parameters are set by cvSetImagesForHaarClassifierCascade */CvSize real_window_size; /* current object size */double scale; /* current scale */CvHaarStageClassifier* stage_classifier; /* array of stage classifiers */ CvHidHaarClassifierCascade* hid_cascade; /* hidden optimized representation of the cascade, created by cvSetImagesForHaarClassifierCascade */所有的结构都代表一个级联boosted Haar分类器。

级联有下面的等级结构:Cascade:Stage1:Classifier11:Feature11Classifier12:Feature12...Stage2:Classifier21:Feature21......全部等级可以手农构修,也可以应用函数cvLoadHaarClassifierCascade从已有的磁盘文件或嵌进式基中导进。

特征检测用到的函数:cvLoadHaarClassifierCascade从文件中装载练习佳的级联分类器或者从OpenCV中嵌入的分类器数据库中导入CvHaarClassifierCascade* cvLoadHaarClassifierCascade(const char* directory,CvSize orig_window_size );directory :练习的级联分类器的门路orig_window_size:级联分类器训练中采取的检测目标的尺寸。

基于Opencv的视频人脸检测程序源代码(可运行)

基于Opencv的视频人脸检测程序源代码(可运行)

1. 打开Microsoft Visual Studio 2008,新建一个Win32控制台项目;控制台项目; 2. 配置好项目的包含文件和库文件;配置好项目的包含文件和库文件;3. 将…………\OpenCV\data\haarcascades\OpenCV\data\haarcascades 中的haarcascade_frontalface_alt.xml 拷贝到所建项目的文件夹中;文件夹中;4. 然后添加代码:然后添加代码:#include "stdafx.h"#include "cv.h"#include "highgui.h"#include <stdio.h>int _tmain _tmain((int argc argc, , _TCHAR _TCHAR* * argv argv[]) []){CvCapture CvCapture* * capture capture=0; =0;/*初始化一个视频捕获操作。

告诉底层的捕获api 我想从Capture1.avi 中捕获图片,底层底层api 将检测并选择相应的解码器并做好准备工作*/capture capture = = c vCaptureFromFile cvCaptureFromFile cvCaptureFromFile( ( "F:\\1.avi"); ////设置要读的视频(avi 格式)static CvMemStorage CvMemStorage* * storage storage = 0; = 0;static CvHaarClassifierCascade CvHaarClassifierCascade* * cascade cascade = 0; = 0;cascade cascade = ( = (CvHaarClassifierCascade CvHaarClassifierCascade*)*)*)cvLoad cvLoad cvLoad(("haarcascade_frontalface_alt.xml",0,0,0);if ( !c ascade cascade cascade || ! || !capture capture ) )return -1;storage storage = = c vCreateMemStorage cvCreateMemStorage cvCreateMemStorage(0); (0);/*创建一个窗口,用“Video ”作为窗口的标识符*/cvNamedWindow cvNamedWindow( ( "Video",1);/*如果初始化失败,那么capture 为空指针,程序停止,否则进入捕获循环*/if ( capture capture ) ){for (;;){IplImage IplImage* * frame frame = = c vQueryFrame cvQueryFrame cvQueryFrame( ( capture capture ); );IplImage IplImage* * img img = = N ULL NULL NULL; ;CvSeq CvSeq* * faces faces; ;if ( !f rame frame frame ) )break ; img img = = c vCloneImage cvCloneImage cvCloneImage((frame frame); );img img->->->origin origin origin = 0; = 0; if ( frame frame->->->origin origin origin ) ) cvFlip cvFlip((img img,,img img); );cvClearMemStorage cvClearMemStorage( ( storage storage ); );//目标检测faces faces = = c vHaarDetectObjects cvHaarDetectObjects cvHaarDetectObjects( ( img img, , cascade cascade, , storage storage,1.1, 2, ,1.1, 2, CV_HAAR_DO_CANNY_PRUNING CV_HAAR_DO_CANNY_PRUNING, , cvSize cvSize(20, 20) ); (20, 20) ); for ( int i = 0; i < (faces faces ? ? f aces faces faces->->->total total total : 0); : 0); i ++ ) { CvRect CvRect* * r = (CvRect CvRect*)*)*)cvGetSeqElem cvGetSeqElem cvGetSeqElem( ( faces faces, , i ); cvRectangle cvRectangle( ( img img, , cvPoint cvPoint((r ->->x x ,r ->->y y ), cvPoint cvPoint((r ->->x x +r ->->width width width,,r ->->y y +r ->->height height height), ), CV_RGB CV_RGB(255,0,0), 1); (255,0,0), 1); } cvShowImage cvShowImage( ( "Video", img img ); ); //设置每帧图像的间隔 Sleep Sleep(50); (50); /*如果你敲了键盘,就退出程序,否则继续捕获下一帧*/ if ( cvWaitKey cvWaitKey(10)>0 ) (10)>0 ) break ; } /*退出之前结束底层api 的捕获操作,比如会使得别的程序无法访问已经被它们打开的文件*/ cvReleaseCapture cvReleaseCapture( &( &c apture capture capture); ); } /*销毁窗口*/ cvDestroyWindow cvDestroyWindow(("Video"); return 0; }。

人脸识别程序源代码

人脸识别程序源代码

1.利用OpenCV进行人脸检测
人脸检测程序主要完成3部分功能,即加载分类器、加载待检测图象以及检测并标示;本程序使用OpenCV中提供的“haarcascade_frontalface_alt.xml”文件存储的目标检测分类,用cvLoad函数载入后,进行强制类型转换;OpenCV中提供的用于检测图像中目标的函数是cvHaarDetectObjects,该函数使用指针对某目标物体如人脸训练的级联分类器在图象中找到包含目标物体的矩形区域,并将这些区域作为一序列的矩形框返回;分类器在使用后需要被显式释放,所用的函数为cvReleaseHaarCla ssifierCascade;这些函数原型请参看有关OpenCV手册;
2.程序实现
1新建一个VisualC++MFC项目,取名为“FaceDetection”,选择应用程序类型为“单文档”;将菜单中多余的项去掉,并添加一项“人脸检测”,其ID为“ID_FaceDe tected”,并生成该菜单项的消息映射函数;
2在“FaceDetectionView.h”头文件中添加以下灰底色部分程序代码:
3在“FaceDetectionView.cpp”文件中添加以下灰底色部分程序代码:
需要注意的是,本程序运行时应将分类器文件置于程序目录下,如果运行的是生成的EXE文件,则应将分类器文件与该EXE文件放在同一个目录下;
三、程序运行结果
运行该程序,选择人脸检测菜单项,弹出文件打开对话框,选择要检测的图像文件,程序就会将检测到的人脸用圆圈标示出来,如图3所示;本程序能顺利检测出大部分人脸,但由于光照、遮挡和倾斜等原因,部分人脸不能正确检测,另外,也有一些非人脸部分由于具有人脸的某些特征,也被当成了人脸,这些都是本程序需要改进的部分;。

Python基于Opencv来快速实现人脸识别过程详解(完整版)

Python基于Opencv来快速实现人脸识别过程详解(完整版)

Python基于Opencv来快速实现⼈脸识别过程详解(完整版)前⾔随着⼈⼯智能的⽇益⽕热,计算机视觉领域发展迅速,尤其在⼈脸识别或物体检测⽅向更为⼴泛,今天就为⼤家带来最基础的⼈脸识别基础,从⼀个个函数开始⾛进这个奥妙的世界。

⾸先看⼀下本实验需要的数据集,为了简便我们只进⾏两个⼈的识别,选取了beyond乐队的主唱黄家驹和贝斯⼿黄家强,这哥俩长得有⼏分神似,这也是对⼈脸识别的⼀个考验:两个⽂件夹,⼀个为训练数据集,⼀个为测试数据集,训练数据集中有两个⽂件夹0和1,之前看⼀些资料有说这⾥要遵循“slabel”命名规则,但后⾯处理起来⽐较⿇烦,因为⽬前opencv接受的⼈脸识别标签为整数,那我们就直接⽤整数命名吧:为了⽅便,我们每个⼈⽤20张照⽚来训练,0代表黄家驹,1代表黄家强:开始啦:1.检测⼈脸这应该是最基本的,给我们⼀张图⽚,我们要先检测出⼈脸的区域,然后才能进⾏操作,opencv已经内置了很多分类检测器,我们这次⽤haar:def detect_face(img):#将测试图像转换为灰度图像,因为opencv⼈脸检测器需要灰度图像gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#加载OpenCV⼈脸检测分类器Haarface_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')#检测多尺度图像,返回值是⼀张脸部区域信息的列表(x,y,宽,⾼)faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)# 如果未检测到⾯部,则返回原始图像if (len(faces) == 0):return None, None#⽬前假设只有⼀张脸,xy为左上⾓坐标,wh为矩形的宽⾼(x, y, w, h) = faces[0]#返回图像的正⾯部分return gray[y:y + w, x:x + h], faces[0]2.有了数据集和检测⼈脸的功能后,我们就可以进⾏预训练了最后返回所有训练图⽚的⼈脸检测信息和标签:# 该函数将读取所有的训练图像,从每个图像检测⼈脸并将返回两个相同⼤⼩的列表,分别为脸部信息和标签def prepare_training_data(data_folder_path):# 获取数据⽂件夹中的⽬录(每个主题的⼀个⽬录)dirs = os.listdir(data_folder_path)# 两个列表分别保存所有的脸部和标签faces = []labels = []# 浏览每个⽬录并访问其中的图像for dir_name in dirs:# dir_name(str类型)即标签label = int(dir_name)# 建⽴包含当前主题主题图像的⽬录路径subject_dir_path = data_folder_path + "/" + dir_name# 获取给定主题⽬录内的图像名称subject_images_names = os.listdir(subject_dir_path)# 浏览每张图⽚并检测脸部,然后将脸部信息添加到脸部列表faces[]for image_name in subject_images_names:# 建⽴图像路径image_path = subject_dir_path + "/" + image_name# 读取图像image = cv2.imread(image_path)# 显⽰图像0.1scv2.imshow("Training on image...", image)cv2.waitKey(100)# 检测脸部face, rect = detect_face(image)# 我们忽略未检测到的脸部if face is not None:#将脸添加到脸部列表并添加相应的标签faces.append(face)labels.append(label)cv2.waitKey(1)cv2.destroyAllWindows()#最终返回值为⼈脸和标签列表return faces, labels3.有了脸部信息和对应标签后,我们就可以使⽤opencv⾃带的识别器来进⾏训练了:#调⽤prepare_training_data()函数faces, labels = prepare_training_data("training_data")#创建LBPH识别器并开始训练,当然也可以选择Eigen或者Fisher识别器face_recognizer = cv2.face.LBPHFaceRecognizer_create()face_recognizer.train(faces, np.array(labels))4.训练完毕后就可以进⾏预测了在这之前我们可以设定⼀下预测的格式,包括⽤矩形框框出⼈脸并标出其名字,当然最后别忘了建⽴标签与真实姓名直接的映射表:#根据给定的(x,y)坐标和宽度⾼度在图像上绘制矩形def draw_rectangle(img, rect):(x, y, w, h) = rectcv2.rectangle(img, (x, y), (x + w, y + h), (128, 128, 0), 2)# 根据给定的(x,y)坐标标识出⼈名def draw_text(img, text, x, y):cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (128, 128, 0), 2)#建⽴标签与⼈名的映射列表(标签只能为整数)subjects = ["jiaju", "jiaqiang"]5.现在就可以定义我们的预测函数了:# 此函数识别传递的图像中的⼈物并在检测到的脸部周围绘制⼀个矩形及其名称def predict(test_img):#⽣成图像的副本,这样就能保留原始图像img = test_img.copy()#检测⼈脸face, rect = detect_face(img)#预测⼈脸label = face_recognizer.predict(face)# 获取由⼈脸识别器返回的相应标签的名称label_text = subjects[label[0]]# 在检测到的脸部周围画⼀个矩形draw_rectangle(img, rect)# 标出预测的名字draw_text(img, label_text, rect[0], rect[1] - 5)#返回预测的图像return img6.最后使⽤我们test_data中的图⽚进⾏预测并显⽰最终效果:#加载测试图像test_img1 = cv2.imread("test_data/test1.jpg")test_img2 = cv2.imread("test_data/test2.jpg")#执⾏预测predicted_img1 = predict(test_img1)predicted_img2 = predict(test_img2)#显⽰两个图像cv2.imshow(subjects[0], predicted_img1)cv2.imshow(subjects[1], predicted_img2)cv2.waitKey(0)cv2.destroyAllWindows()来看看识别的结果:这就是⼈脸识别最基本的流程,后续还会进⼀步的研究,下⼀篇我们将讨论本次实验的⼀些细节和注意事项,算是对本篇的⼀次挖掘和总结吧。

python中使用Opencv进行人脸识别

python中使用Opencv进行人脸识别

python中使⽤Opencv进⾏⼈脸识别上⼀节讲到⼈脸检测,现在讲⼀下⼈脸识别。

具体是通过程序采集图像并进⾏训练,并且基于这些训练的图像对⼈脸进⾏动态识别。

⼈脸识别前所需要的⼈脸库可以通过两种⽅式获得:1.⾃⼰从视频获取图像 2.从⼈脸数据库免费获得可⽤⼈脸图像,如ORL⼈脸库(包含40个⼈每⼈10张⼈脸,总共400张⼈脸),ORL⼈脸库中的每⼀张图像⼤⼩为92x112。

若要对这些样本进⾏⼈脸识别必须要在包含⼈脸的样本图像上进⾏⼈脸识别。

这⾥提供⾃⼰准备图像识别出⾃⼰的⽅法。

1.采集⼈脸信息:通过摄像头采集⼈脸信息,10张以上即可,把图像⼤⼩调整为92x112,保存在⼀个指定的⽂件夹,⽂件名后缀为.pngdef generator(data):'''打开摄像头,读取帧,检测该帧图像中的⼈脸,并进⾏剪切、缩放⽣成图⽚满⾜以下格式:1.灰度图,后缀为 .png2.图像⼤⼩相同params:data:指定⽣成的⼈脸数据的保存路径'''name=input('my name:')#如果路径存在则删除路径path=os.path.join(data,name)if os.path.isdir(path):shutil.rmtree(path)#创建⽂件夹os.mkdir(path)#创建⼀个级联分类器face_casecade=cv2.CascadeClassifier('../haarcascades/haarcascade_frontalface_default.xml')#打开摄像头camera=cv2.VideoCapture(0)dWindow('Dynamic')#计数count=1while(True):#读取⼀帧图像ret,frame=camera.read()if ret:#转换为灰度图gray_img=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#⼈脸检测face=face_casecade.detectMultiScale(gray_img,1.3,5)for (x,y,w,h) in face:#在原图上绘制矩形cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)#调整图像⼤⼩new_frame=cv2.resize(frame[y:y+h,x:x+w],(92,112))#保存⼈脸cv2.imwrite('%s/%s.png'%(path,str(count)),new_frame)count+=1cv2.imshow('Dynamic',frame)#按下q键退出if cv2.waitKey(100) & 0xff==ord('q'):breakcamera.release()cv2.destroyAllWindows()该程序运⾏后会在指定的data路径下创建⼀个你输⼊的⼈名的⽂件夹⽤于存放采集到的图像,在这⾥我输⼊了wjy,结果如图2.⼈脸识别OpenCV有3中⼈脸识别⽅法,分别基于三个不同算法,分别为Eigenfaces,Fisherfaces和Local Binary Pattern Histogram这些⽅法都有类似的⼀个过程,即先对数据集进⾏训练,对图像或视频中的⼈脸进⾏分析,并且从两个⽅⾯确定:1.是否识别到对应的⽬标,2.识别到的⽬标的置信度,在实际中通过阈值进⾏筛选,置信度⾼于阈值的⼈脸将被丢弃这⾥介绍⼀下利⽤特征脸即Eigenfaces进⾏⼈脸识别算法,特征脸法本质上就是PCA降维,基本思路是先把图像灰度化,转化为单通道,再将它⾸位相接转换为列向量,假设图像的⼤⼩是20*20的,那么这个向量就是400维,但是维度太⾼算法复杂度也会升⾼,所以需要降维,再使⽤简单排序即可#载⼊图像读取ORL⼈脸数据库,准备训练数据def LoadImages(data):'''加载图⽚数据⽤于训练params:data:训练数据所在的⽬录,要求图⽚尺⼨⼀样ret:images:[m,height,width] m为样本数,height为⾼,width为宽names:名字的集合labels:标签'''images=[]names=[]labels=[]label=0#遍历所有⽂件夹for subdir in os.listdir(data):subpath=os.path.join(data,subdir)#print('path',subpath)#判断⽂件夹是否存在if os.path.isdir(subpath):#在每⼀个⽂件夹中存放着⼀个⼈的许多照⽚names.append(subdir)#遍历⽂件夹中的图⽚⽂件for filename in os.listdir(subpath):imgpath=os.path.join(subpath,filename)img=cv2.imread(imgpath,cv2.IMREAD_COLOR)gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#cv2.imshow('1',img)#cv2.waitKey(0)images.append(gray_img)labels.append(label)label+=1images=np.asarray(images)#names=np.asarray(names)labels=np.asarray(labels)return images,labels,names#检验训练结果def FaceRec(data):#加载训练的数据X,y,names=LoadImages(data)#print('x',X)model=cv2.face.EigenFaceRecognizer_create()model.train(X,y)#打开摄像头camera=cv2.VideoCapture(0)dWindow('Dynamic')#创建级联分类器face_casecade=cv2.CascadeClassifier('../haarcascades/haarcascade_frontalface_default.xml')while(True):#读取⼀帧图像#ret:图像是否读取成功#frame:该帧图像ret,frame=camera.read()#判断图像是否读取成功#print('ret',ret)if ret:#转换为灰度图gray_img=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#利⽤级联分类器鉴别⼈脸faces=face_casecade.detectMultiScale(gray_img,1.3,5)#遍历每⼀帧图像,画出矩形for (x,y,w,h) in faces:frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) #蓝⾊roi_gray=gray_img[y:y+h,x:x+w]try:#将图像转换为宽92 ⾼112的图像#resize(原图像,⽬标⼤⼩,(插值⽅法)interpolation=,)roi_gray=cv2.resize(roi_gray,(92,112),interpolation=cv2.INTER_LINEAR)params=model.predict(roi_gray)print('Label:%s,confidence:%.2f'%(params[0],params[1]))'''putText:给照⽚添加⽂字putText(输⼊图像,'所需添加的⽂字',左上⾓的坐标,字体,字体⼤⼩,颜⾊,字体粗细)'''cv2.putText(frame,names[params[0]],(x,y-20),cv2.FONT_HERSHEY_SIMPLEX,1,255,2)except:continuecv2.imshow('Dynamic',frame)#按下q键退出if cv2.waitKey(100) & 0xff==ord('q'):breakcamera.release()cv2.destroyAllWindows()在程序中,我们⽤cv2.face.EigenFaceRecognizer_create()创建⼈脸识别的模型,通过图像数组和对应标签数组来训练模型,该函数有两个重要的参数,1.保留主成分的数⽬,2.指定的置信度阈值,为⼀个浮点型。

python基于OpenCV的人脸识别系统

python基于OpenCV的人脸识别系统

from training import training from datasets import datasets from delFile import del_file
def main(): facedict = {} cur_path = r'./dataset/' while True: print('*' * 31) print(''' opencv人脸识别 -------------输入1,人脸采集 输入2,人脸训练 输入3,人脸识别 输入d,删除数据 输入q,退出程序 ''') print('*' * 31) num = input("请输入您的操作选择: ") if num == '1': mydict = datasets() facedict.update(mydict) print(facedict) elif num == '2': training() elif num == '3': recognition(facedict) elif num == 'd': del_file(cur_path) elif num == 'q': print("退出程序成功!") break else: print("您输入有误,请重新输入!")
if __name__ == '__main__&好我按照您的方法跑通了objectdetection单机单卡但是在跑单机多卡一段时间后就会出现报错然后就停止了请问这个是什么原因导致的我该如何修改谢谢
python基于 OpenCV的人脸识别系统

OpenCV+python实现人脸检测(基于照片和视频进行检测)

OpenCV+python实现人脸检测(基于照片和视频进行检测)

OpenCV+python实现⼈脸检测(基于照⽚和视频进⾏检测)OpenCV + python 实现⼈脸检测(基于照⽚和视频进⾏检测)Haar-like通俗的来讲,就是作为⼈脸特征即可。

Haar特征值反映了图像的灰度变化情况。

例如:脸部的⼀些特征能由矩形特征简单的描述,如:眼睛要⽐脸颊颜⾊要深,⿐梁两侧⽐⿐梁颜⾊要深,嘴巴⽐周围颜⾊要深等。

opencv api要想使⽤opencv,就必须先知道其能⼲什么,怎么做。

于是API的重要性便体现出来了。

就本例⽽⾔,使⽤到的函数很少,也就普通的读取图⽚,灰度转换,显⽰图像,简单的编辑图像罢了。

如下:读取图⽚只需要给出待操作的图⽚的路径即可。

import cv2image = cv2.imread(imagepath)灰度转换灰度转换的作⽤就是:转换成灰度的图⽚的计算强度得以降低。

import cv2gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)画图opencv 的强⼤之处的⼀个体现就是其可以对图⽚进⾏任意编辑,处理。

下⾯的这个函数最后⼀个参数指定的就是画笔的⼤⼩。

import cv2cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)显⽰图像编辑完的图像要么直接的被显⽰出来,要么就保存到物理的存储介质。

import cv2cv2.imshow("Image Title",image)获取⼈脸识别训练数据看似复杂,其实就是对于⼈脸特征的⼀些描述,这样opencv在读取完数据后很据训练中的样品数据,就可以感知读取到的图⽚上的特征,进⽽对图⽚进⾏⼈脸识别。

import cv2face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')⾥卖弄的这个xml⽂件,就是opencv在GitHub上共享出来的具有普适的训练好的数据。

python利用Opencv实现人脸识别功能

python利用Opencv实现人脸识别功能

python利⽤Opencv实现⼈脸识别功能本⽂实例为⼤家分享了python利⽤Opencv实现⼈脸识别功能的具体代码,供⼤家参考,具体内容如下⾸先:需要在在⾃⼰本地安装opencv具体步骤可以问度娘如果从事于开发中的话建议⽤第三⽅的⼈脸识别(推荐阿⾥)1、视频流中进⾏⼈脸识别# -*- coding: utf-8 -*-import cv2import sysfrom PIL import Imagedef CatchUsbVideo(window_name, camera_idx):dWindow(window_name)# 视频来源,可以来⾃⼀段已存好的视频,也可以直接来⾃USB摄像头cap = cv2.VideoCapture(camera_idx)# 告诉OpenCV使⽤⼈脸识别分类器classfier = cv2.CascadeClassifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml")# 识别出⼈脸后要画的边框的颜⾊,RGB格式color = (0, 255, 0)count=0while cap.isOpened():ok, frame = cap.read() # 读取⼀帧数据if not ok:break# 将当前帧转换成灰度图像grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# ⼈脸检测,1.2和2分别为图⽚缩放⽐例和需要检测的有效点数faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))if len(faceRects) > 0: # ⼤于0则检测到⼈脸count=count+1return countif __name__ == '__main__':result=CatchUsbVideo("识别⼈脸区域", '2222.mp4')if result>0:print('视频中有⼈!!')else:print('视频中⽆⼈!!')2、通过图⽚识别⼈脸#-*-coding:utf8-*-#import osimport cv2from PIL import Image,ImageDrawfrom datetime import datetimeimport time#detectFaces()返回图像中所有⼈脸的矩形坐标(矩形左上、右下顶点)#使⽤haar特征的级联分类器haarcascade_frontalface_default.xml,在haarcascades⽬录下还有其他的训练好的xml⽂件可供选择。

Python学习笔记之视频人脸检测识别实例教程

Python学习笔记之视频人脸检测识别实例教程

Python学习笔记之视频人脸检测识别实例教程# -*- coding: utf-8 -*-__author__ = "小柒"__blog__ = ""import cv2import os# 保存好的视频检测人脸并截图def CatchPICFromVideo(window_name, camera_idx, catch_pic_num, path_name):dWindow(window_name)# 视频来源cap = cv2.VideoCapture(camera_idx)# 告诉OpenCV使用人脸识别分类器classfier = cv2.CascadeClassifier(os.getcwd()+"\\haarcascade\\haarcascade _frontalface_alt.xml")# 识别出人脸后要画的边框的颜色,RGB格式, color是一个不可增删的数组color = (0, 255, 0)num = 0while cap.isOpened():ok, frame = cap.read() # 读取一帧数据if not ok:breakgrey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 将当前桢图像转换成灰度图像# 人脸检测,1.2和2分别为图片缩放比例和需要检测的有效点数faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))if len(faceRects) > 0: # 大于0则检测到人脸for faceRect in faceRects: # 单独框出每一张人脸x, y, w, h = faceRect# 将当前帧保存为图片img_name = "%s/%d.jpg" % (path_name, num)# print(img_name)image = frame[y - 10: y + h + 10, x - 10: x + w + 10]cv2.imwrite(img_name, image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])num += 1if num > (catch_pic_num): # 如果超过指定最大保存数量退出循环break# 画出矩形框cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)# 显示当前捕捉到了多少人脸图片了,这样站在那里被拍摄时心里有个数,不用两眼一抹黑傻等着font = cv2.FONT_HERSHEY_SIMPLEXcv2.putT ext(frame, 'num:%d/100' % (num), (x + 30, y + 30), font, 1, (255, 0, 255), 4)# 超过指定最大保存数量结束程序if num > (catch_pic_num): break# 显示图像cv2.imshow(window_name, frame)c = cv2.waitKey(10)if c & 0xFF == ord('q'):break# 释放摄像头并销毁所有窗口cap.release()cv2.destroyAllWindows()if __name__ == '__main__':# 连续截100张图像CatchPICFromVideo("get face", os.getcwd()+"\\video\\kelake.mp4", 100, "E:\\VideoCapture")。

opencv人脸识别代码

opencv人脸识别代码

opencv⼈脸识别代码opencv⼈脸识别C++代码/** Copyright (c) 2011,2012. Philipp Wagner <bytefish[at]gmx[dot]de>.* Released to public domain under terms of the BSD Simplified license.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:* * Redistributions of source code must retain the above copyright* notice, this list of conditions and the following disclaimer.* * Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.* * Neither the name of the organization nor the names of its contributors* may be used to endorse or promote products derived from this software* without specific prior written permission.** See </licenses/bsd-license>*/#include "precomp.hpp"#include <set>namespace cv{using std::set;// Reads a sequence from a FileNode::SEQ with type _Tp into a result vector.template<typename _Tp>inline void readFileNodeList(const FileNode& fn, vector<_Tp>& result) {if (fn.type() == FileNode::SEQ) {for (FileNodeIterator it = fn.begin(); it != fn.end();) {_Tp item;it >> item;result.push_back(item);}}}// Writes the a list of given items to a cv::FileStorage.template<typename _Tp>inline void writeFileNodeList(FileStorage& fs, const string& name,const vector<_Tp>& items) {// typedefstypedef typename vector<_Tp>::const_iterator constVecIterator;// write the elements in item to fsfs << name << "[";for (constVecIterator it = items.begin(); it != items.end(); ++it) {fs << *it;}fs << "]";}static Mat asRowMatrix(InputArrayOfArrays src, int rtype, double alpha=1, double beta=0) {// make sure the input data is a vector of matrices or vector of vectorif(src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR) {string error_message = "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."; CV_Error(CV_StsBadArg, error_message);}// number of samplessize_t n = src.total();// return empty matrix if no matrices givenif(n == 0)return Mat();// dimensionality of (reshaped) samplessize_t d = src.getMat(0).total();// create data matrixMat data((int)n, (int)d, rtype);// now copy datafor(unsigned int i = 0; i < n; i++) {// make sure data can be reshaped, throw exception if not!if(src.getMat(i).total() != d) {string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src.getMat(i).total());CV_Error(CV_StsBadArg, error_message);}// get a hold of the current rowMat xi = data.row(i);// make reshape happy by cloning for non-continuous matricesif(src.getMat(i).isContinuous()) {src.getMat(i).reshape(1, 1).convertTo(xi, rtype, alpha, beta);} else {src.getMat(i).clone().reshape(1, 1).convertTo(xi, rtype, alpha, beta);}}return data;}// Removes duplicate elements in a given vector.template<typename _Tp>inline vector<_Tp> remove_dups(const vector<_Tp>& src) {typedef typename set<_Tp>::const_iterator constSetIterator;typedef typename vector<_Tp>::const_iterator constVecIterator;set<_Tp> set_elems;for (constVecIterator it = src.begin(); it != src.end(); ++it)set_elems.insert(*it);vector<_Tp> elems;for (constSetIterator it = set_elems.begin(); it != set_elems.end(); ++it)elems.push_back(*it);return elems;}// Turk, M., and Pentland, A. "Eigenfaces for recognition.". Journal of// Cognitive Neuroscience 3 (1991), 71–86.//特征脸类class Eigenfaces : public FaceRecognizer{private:int _num_components;//对应“数学上的事”中所提到的q个主成分double _threshold;vector<Mat> _projections;//原始向量投影后的坐标Mat _labels;//每幅图像的标签,⽤于分类Mat _eigenvectors;//特征向量Mat _eigenvalues;//特征值Mat _mean;//均值public:using FaceRecognizer::save;using FaceRecognizer::load;// Initializes an empty Eigenfaces model.Eigenfaces(int num_components = 0, double threshold = DBL_MAX) :_num_components(num_components),_threshold(threshold) {}// Initializes and computes an Eigenfaces model with images in src and// corresponding labels in labels. num_components will be kept for// classification.Eigenfaces(InputArrayOfArrays src, InputArray labels,int num_components = 0, double threshold = DBL_MAX) :_num_components(num_components),_threshold(threshold) {train(src, labels);}// Computes an Eigenfaces model with images in src and corresponding labels // in labels.void train(InputArrayOfArrays src, InputArray labels);// Predicts the label of a query image in src.int predict(InputArray src) const;// Predicts the label and confidence for a given sample.void predict(InputArray _src, int &label, double &dist) const;// See FaceRecognizer::load.void load(const FileStorage& fs);// See FaceRecognizer::save.void save(FileStorage& fs) const;AlgorithmInfo* info() const;};// Belhumeur, P. N., Hespanha, J., and Kriegman, D. "Eigenfaces vs. Fisher-// faces: Recognition using class specific linear projection.". IEEE// Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997),// 711–720.class Fisherfaces: public FaceRecognizer{private:int _num_components;double _threshold;Mat _eigenvectors;Mat _eigenvalues;Mat _mean;vector<Mat> _projections;Mat _labels;public:using FaceRecognizer::save;using FaceRecognizer::load;// Initializes an empty Fisherfaces model.Fisherfaces(int num_components = 0, double threshold = DBL_MAX) :_num_components(num_components),_threshold(threshold) {}// corresponding labels in labels. num_components will be kept for// classification.Fisherfaces(InputArrayOfArrays src, InputArray labels,int num_components = 0, double threshold = DBL_MAX) :_num_components(num_components),_threshold(threshold) {train(src, labels);}~Fisherfaces() {}// Computes a Fisherfaces model with images in src and corresponding labels // in labels.void train(InputArrayOfArrays src, InputArray labels);// Predicts the label of a query image in src.int predict(InputArray src) const;// Predicts the label and confidence for a given sample.void predict(InputArray _src, int &label, double &dist) const;// See FaceRecognizer::load.void load(const FileStorage& fs);// See FaceRecognizer::save.void save(FileStorage& fs) const;AlgorithmInfo* info() const;};// Face Recognition based on Local Binary Patterns.//// Ahonen T, Hadid A. and Pietikäinen M. "Face description with local binary// patterns: Application to face recognition." IEEE Transactions on Pattern// Analysis and Machine Intelligence, 28(12):2037-2041.//class LBPH : public FaceRecognizer{private:int _grid_x;int _grid_y;int _radius;int _neighbors;double _threshold;vector<Mat> _histograms;Mat _labels;// Computes a LBPH model with images in src and// corresponding labels in labels, possibly preserving// old model data.void train(InputArrayOfArrays src, InputArray labels, bool preserveData); public:using FaceRecognizer::save;using FaceRecognizer::load;// Initializes this LBPH Model. The current implementation is rather fixed// as it uses the Extended Local Binary Patterns per default.//// radius, neighbors are used in the local binary patterns creation.// grid_x, grid_y control the grid size of the spatial histograms.LBPH(int radius_=1, int neighbors_=8,int gridx=8, int gridy=8,double threshold = DBL_MAX) :_grid_x(gridx),_grid_y(gridy),_radius(radius_),_neighbors(neighbors_),_threshold(threshold) {}// Initializes and computes this LBPH Model. The current implementation is// rather fixed as it uses the Extended Local Binary Patterns per default.//// (radius=1), (neighbors=8) are used in the local binary patterns creation.// (grid_x=8), (grid_y=8) controls the grid size of the spatial histograms.LBPH(InputArrayOfArrays src,InputArray labels,int radius_=1, int neighbors_=8,int gridx=8, int gridy=8,double threshold = DBL_MAX) :_grid_x(gridx),_grid_y(gridy),_radius(radius_),_neighbors(neighbors_),_threshold(threshold) {train(src, labels);}~LBPH() { }// Computes a LBPH model with images in src and// corresponding labels in labels.void train(InputArrayOfArrays src, InputArray labels);// Updates this LBPH model with images in src and// corresponding labels in labels.void update(InputArrayOfArrays src, InputArray labels);// Predicts the label of a query image in src.int predict(InputArray src) const;// Predicts the label and confidence for a given sample.void predict(InputArray _src, int &label, double &dist) const;// See FaceRecognizer::load.void load(const FileStorage& fs);// See FaceRecognizer::save.void save(FileStorage& fs) const;// Getter functions.int neighbors() const { return _neighbors; }int radius() const { return _radius; }int grid_x() const { return _grid_x; }int grid_y() const { return _grid_y; }AlgorithmInfo* info() const;};//------------------------------------------------------------------------------// FaceRecognizer//------------------------------------------------------------------------------void FaceRecognizer::update(InputArrayOfArrays src, InputArray labels ) {if( dynamic_cast<LBPH*>(this) != 0 ){dynamic_cast<LBPH*>(this)->update( src, labels );return;}string error_msg = format("This FaceRecognizer (%s) does not support updating, you have to use FaceRecognizer::train to update it.", this->name().c_str());CV_Error(CV_StsNotImplemented, error_msg);}void FaceRecognizer::save(const string& filename) const {FileStorage fs(filename, FileStorage::WRITE);if (!fs.isOpened())CV_Error(CV_StsError, "File can't be opened for writing!");this->save(fs);fs.release();}void FaceRecognizer::load(const string& filename) {FileStorage fs(filename, FileStorage::READ);if (!fs.isOpened())CV_Error(CV_StsError, "File can't be opened for writing!");this->load(fs);fs.release();}//------------------------------------------------------------------------------// Eigenfaces特征脸训练函数//------------------------------------------------------------------------------void Eigenfaces::train(InputArrayOfArrays _src, InputArray _local_labels) {if(_src.total() == 0) {string error_message = format("Empty training data was given. You'll need more than one sample to learn a model.");CV_Error(CV_StsBadArg, error_message);} else if(_local_labels.getMat().type() != CV_32SC1) {string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _local_labels.type());CV_Error(CV_StsBadArg, error_message);}// make sure data has correct size确保输⼊的图像数据尺⼨正确(所有尺⼨相同)if(_src.total() > 1) {for(int i = 1; i < static_cast<int>(_src.total()); i++) {if(_src.getMat(i-1).total() != _src.getMat(i).total()) {string error_message = format("In the Eigenfaces method all input samples (training images) must be of equal size! Expected %d pixels, but was %d pixels.", _src.getMat(i-1).total(), _src.getMat(i).total()); CV_Error(CV_StsUnsupportedFormat, error_message);}}}// get labelsMat labels = _local_labels.getMat();// observations in rowMat data = asRowMatrix(_src, CV_64FC1);//将_src中存放的图像列表中的每幅图像(reshape成1⾏)作为data的⼀⾏// number of samplesint n = data.rows;// assert there are as much samples as labelsif(static_cast<int>(labels.total()) != n) {string error_message = format("The number of samples (src) must equal the number of labels (labels)! len(src)=%d, len(labels)=%d.", n, labels.total());CV_Error(CV_StsBadArg, error_message);// clear existing model data_labels.release();_projections.clear();// clip number of components to be validif((_num_components <= 0) || (_num_components > n))_num_components = n;// perform the PCAPCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, _num_components);// copy the PCA results_mean = pca.mean.reshape(1,1); // store the mean vector获取均值向量_eigenvalues = pca.eigenvalues.clone(); // eigenvalues by row获取特征值transpose(pca.eigenvectors, _eigenvectors); // eigenvectors by column获取特征向量// store labels for prediction_labels = labels.clone();//获取分类标签// save projectionsfor(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) {Mat p = subspaceProject(_eigenvectors, _mean, data.row(sampleIdx));_projections.push_back(p);}}void Eigenfaces::predict(InputArray _src, int &minClass, double &minDist) const {// get dataMat src = _src.getMat();// make sure the user is passing correct dataif(_projections.empty()) {// throw error if no data (or simply return -1?)string error_message = "This Eigenfaces model is not computed yet. Did you call Eigenfaces::train?";CV_Error(CV_StsError, error_message);} else if(_eigenvectors.rows != static_cast<int>(src.total())) {// check data alignment just for clearer exception messagesstring error_message = format("Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with %d elements, but got %d.", _eigenvectors.rows, src.total()); CV_Error(CV_StsBadArg, error_message);}// project into PCA subspaceMat q = subspaceProject(_eigenvectors, _mean, src.reshape(1,1));// 投影到PCA的主成分空间minDist = DBL_MAX;minClass = -1;//求L2范数也就是欧式距离for(size_t sampleIdx = 0; sampleIdx < _projections.size(); sampleIdx++) {double dist = norm(_projections[sampleIdx], q, NORM_L2);if((dist < minDist) && (dist < _threshold)) {minDist = dist;minClass = _labels.at<int>((int)sampleIdx);}}}int Eigenfaces::predict(InputArray _src) const {int label;double dummy;predict(_src, label, dummy);return label;}void Eigenfaces::load(const FileStorage& fs) {//read matricesfs["num_components"] >> _num_components;fs["mean"] >> _mean;fs["eigenvalues"] >> _eigenvalues;fs["eigenvectors"] >> _eigenvectors;// read sequencesreadFileNodeList(fs["projections"], _projections);fs["labels"] >> _labels;}void Eigenfaces::save(FileStorage& fs) const {// write matricesfs << "num_components" << _num_components;fs << "mean" << _mean;fs << "eigenvalues" << _eigenvalues;fs << "eigenvectors" << _eigenvectors;// write sequenceswriteFileNodeList(fs, "projections", _projections);fs << "labels" << _labels;}//------------------------------------------------------------------------------// Fisherfaces//------------------------------------------------------------------------------void Fisherfaces::train(InputArrayOfArrays src, InputArray _lbls) {if(src.total() == 0) {string error_message = format("Empty training data was given. You'll need more than one sample to learn a model.");CV_Error(CV_StsBadArg, error_message);} else if(_lbls.getMat().type() != CV_32SC1) {string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _lbls.type());CV_Error(CV_StsBadArg, error_message);}// make sure data has correct sizeif(src.total() > 1) {if(src.getMat(i-1).total() != src.getMat(i).total()) {string error_message = format("In the Fisherfaces method all input samples (training images) must be of equal size! Expected %d pixels, but was %d pixels.", src.getMat(i-1).total(), src.getMat(i).total()); CV_Error(CV_StsUnsupportedFormat, error_message);}}}// get dataMat labels = _lbls.getMat();Mat data = asRowMatrix(src, CV_64FC1);// number of samplesint N = data.rows;// make sure labels are passed in correct shapeif(labels.total() != (size_t) N) {string error_message = format("The number of samples (src) must equal the number of labels (labels)! len(src)=%d, len(labels)=%d.", N, labels.total());CV_Error(CV_StsBadArg, error_message);} else if(labels.rows != 1 && labels.cols != 1) {string error_message = format("Expected the labels in a matrix with one row or column! Given dimensions are rows=%s, cols=%d.", labels.rows, labels.cols);CV_Error(CV_StsBadArg, error_message);}// clear existing model data_labels.release();_projections.clear();// safely copy from cv::Mat to std::vectorvector<int> ll;for(unsigned int i = 0; i < labels.total(); i++) {ll.push_back(labels.at<int>(i));}// get the number of unique classesint C = (int) remove_dups(ll).size();// clip number of components to be a valid numberif((_num_components <= 0) || (_num_components > (C-1)))_num_components = (C-1);// perform a PCA and keep (N-C) componentsPCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, (N-C));// project the data and perform a LDA on itLDA lda(pca.project(data),labels, _num_components);// store the total mean vector_mean = pca.mean.reshape(1,1);// store labels_labels = labels.clone();// store the eigenvalues of the discriminantslda.eigenvalues().convertTo(_eigenvalues, CV_64FC1);// Now calculate the projection matrix as pca.eigenvectors * lda.eigenvectors.// Note: OpenCV stores the eigenvectors by row, so we need to transpose it!gemm(pca.eigenvectors, lda.eigenvectors(), 1.0, Mat(), 0.0, _eigenvectors, GEMM_1_T);// store the projections of the original datafor(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) {Mat p = subspaceProject(_eigenvectors, _mean, data.row(sampleIdx));_projections.push_back(p);}}void Fisherfaces::predict(InputArray _src, int &minClass, double &minDist) const {Mat src = _src.getMat();// check data alignment just for clearer exception messagesif(_projections.empty()) {// throw error if no data (or simply return -1?)string error_message = "This Fisherfaces model is not computed yet. Did you call Fisherfaces::train?";CV_Error(CV_StsBadArg, error_message);} else if(src.total() != (size_t) _eigenvectors.rows) {string error_message = format("Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with %d elements, but got %d.", _eigenvectors.rows, src.total());CV_Error(CV_StsBadArg, error_message);}// project into LDA subspaceMat q = subspaceProject(_eigenvectors, _mean, src.reshape(1,1));// find 1-nearest neighborminDist = DBL_MAX;minClass = -1;for(size_t sampleIdx = 0; sampleIdx < _projections.size(); sampleIdx++) {double dist = norm(_projections[sampleIdx], q, NORM_L2);if((dist < minDist) && (dist < _threshold)) {minDist = dist;minClass = _labels.at<int>((int)sampleIdx);}}}int Fisherfaces::predict(InputArray _src) const {int label;double dummy;predict(_src, label, dummy);return label;}// See FaceRecognizer::load.void Fisherfaces::load(const FileStorage& fs) {//read matricesfs["num_components"] >> _num_components;fs["mean"] >> _mean;fs["eigenvalues"] >> _eigenvalues;fs["eigenvectors"] >> _eigenvectors;readFileNodeList(fs["projections"], _projections);fs["labels"] >> _labels;}// See FaceRecognizer::save.void Fisherfaces::save(FileStorage& fs) const {// write matricesfs << "num_components" << _num_components;fs << "mean" << _mean;fs << "eigenvalues" << _eigenvalues;fs << "eigenvectors" << _eigenvectors;// write sequenceswriteFileNodeList(fs, "projections", _projections);fs << "labels" << _labels;}//------------------------------------------------------------------------------// LBPH//------------------------------------------------------------------------------template <typename _Tp> staticvoid olbp_(InputArray _src, OutputArray _dst) {// get matricesMat src = _src.getMat();// allocate memory for result_dst.create(src.rows-2, src.cols-2, CV_8UC1);Mat dst = _dst.getMat();// zero the result matrixdst.setTo(0);// calculate patternsfor(int i=1;i<src.rows-1;i++) {for(int j=1;j<src.cols-1;j++) {_Tp center = src.at<_Tp>(i,j);unsigned char code = 0;code |= (src.at<_Tp>(i-1,j-1) >= center) << 7;code |= (src.at<_Tp>(i-1,j) >= center) << 6;code |= (src.at<_Tp>(i-1,j+1) >= center) << 5;code |= (src.at<_Tp>(i,j+1) >= center) << 4;code |= (src.at<_Tp>(i+1,j+1) >= center) << 3;code |= (src.at<_Tp>(i+1,j) >= center) << 2;code |= (src.at<_Tp>(i+1,j-1) >= center) << 1;code |= (src.at<_Tp>(i,j-1) >= center) << 0;dst.at<unsigned char>(i-1,j-1) = code;}}}//------------------------------------------------------------------------------// cv::elbp//------------------------------------------------------------------------------template <typename _Tp> staticinline void elbp_(InputArray _src, OutputArray _dst, int radius, int neighbors) {//get matricesMat src = _src.getMat();// allocate memory for result_dst.create(src.rows-2*radius, src.cols-2*radius, CV_32SC1);Mat dst = _dst.getMat();// zerodst.setTo(0);for(int n=0; n<neighbors; n++) {// sample pointsfloat x = static_cast<float>(radius * cos(2.0*CV_PI*n/static_cast<float>(neighbors)));float y = static_cast<float>(-radius * sin(2.0*CV_PI*n/static_cast<float>(neighbors)));// relative indicesint fx = static_cast<int>(floor(x));int fy = static_cast<int>(floor(y));int cx = static_cast<int>(ceil(x));int cy = static_cast<int>(ceil(y));// fractional partfloat ty = y - fy;float tx = x - fx;// set interpolation weightsfloat w1 = (1 - tx) * (1 - ty);float w2 = tx * (1 - ty);float w3 = (1 - tx) * ty;float w4 = tx * ty;// iterate through your datafor(int i=radius; i < src.rows-radius;i++) {for(int j=radius;j < src.cols-radius;j++) {// calculate interpolated valuefloat t = static_cast<float>(w1*src.at<_Tp>(i+fy,j+fx) + w2*src.at<_Tp>(i+fy,j+cx) + w3*src.at<_Tp>(i+cy,j+fx) + w4*src.at<_Tp>(i+cy,j+cx));// floating point precision, so check some machine-dependent epsilondst.at<int>(i-radius,j-radius) += ((t > src.at<_Tp>(i,j)) || (std::abs(t-src.at<_Tp>(i,j)) < std::numeric_limits<float>::epsilon())) << n;}}}}static void elbp(InputArray src, OutputArray dst, int radius, int neighbors){int type = src.type();case CV_8SC1: elbp_<char>(src,dst, radius, neighbors); break;case CV_8UC1: elbp_<unsigned char>(src, dst, radius, neighbors); break;case CV_16SC1: elbp_<short>(src,dst, radius, neighbors); break;case CV_16UC1: elbp_<unsigned short>(src,dst, radius, neighbors); break;case CV_32SC1: elbp_<int>(src,dst, radius, neighbors); break;case CV_32FC1: elbp_<float>(src,dst, radius, neighbors); break;case CV_64FC1: elbp_<double>(src,dst, radius, neighbors); break;default:string error_msg = format("Using Original Local Binary Patterns for feature extraction only works on single-channel images (given %d). Please pass the image data as a grayscale image!", type); CV_Error(CV_StsNotImplemented, error_msg);break;}}static Mathistc_(const Mat& src, int minVal=0, int maxVal=255, bool normed=false){Mat result;// Establish the number of bins.int histSize = maxVal-minVal+1;// Set the ranges.float range[] = { static_cast<float>(minVal), static_cast<float>(maxVal+1) };const float* histRange = { range };// calc histogramcalcHist(&src, 1, 0, Mat(), result, 1, &histSize, &histRange, true, false);// normalizeif(normed) {result /= (int)src.total();}return result.reshape(1,1);}static Mat histc(InputArray _src, int minVal, int maxVal, bool normed){Mat src = _src.getMat();switch (src.type()) {case CV_8SC1:return histc_(Mat_<float>(src), minVal, maxVal, normed);break;case CV_8UC1:return histc_(src, minVal, maxVal, normed);break;case CV_16SC1:return histc_(Mat_<float>(src), minVal, maxVal, normed);break;case CV_16UC1:return histc_(src, minVal, maxVal, normed);break;case CV_32SC1:return histc_(Mat_<float>(src), minVal, maxVal, normed);break;case CV_32FC1:return histc_(src, minVal, maxVal, normed);break;default:CV_Error(CV_StsUnmatchedFormats, "This type is not implemented yet."); break;}return Mat();}static Mat spatial_histogram(InputArray _src, int numPatterns,int grid_x, int grid_y, bool/*normed*/){Mat src = _src.getMat();// calculate LBP patch sizeint width = src.cols/grid_x;int height = src.rows/grid_y;// allocate memory for the spatial histogramMat result = Mat::zeros(grid_x * grid_y, numPatterns, CV_32FC1);// return matrix with zeros if no data was givenif(src.empty())return result.reshape(1,1);// initial result_rowint resultRowIdx = 0;// iterate through gridfor(int i = 0; i < grid_y; i++) {for(int j = 0; j < grid_x; j++) {Mat src_cell = Mat(src, Range(i*height,(i+1)*height), Range(j*width,(j+1)*width));Mat cell_hist = histc(src_cell, 0, (numPatterns-1), true);// copy to the result matrixMat result_row = result.row(resultRowIdx);cell_hist.reshape(1,1).convertTo(result_row, CV_32FC1);// increase row count in result matrixresultRowIdx++;}}// return result as reshaped feature vectorreturn result.reshape(1,1);}// wrapper to cv::elbp (extended local binary patterns)//------------------------------------------------------------------------------static Mat elbp(InputArray src, int radius, int neighbors) {Mat dst;elbp(src, dst, radius, neighbors);return dst;}void LBPH::load(const FileStorage& fs) {fs["radius"] >> _radius;fs["neighbors"] >> _neighbors;fs["grid_x"] >> _grid_x;fs["grid_y"] >> _grid_y;//read matricesreadFileNodeList(fs["histograms"], _histograms);fs["labels"] >> _labels;}// See FaceRecognizer::save.void LBPH::save(FileStorage& fs) const {fs << "radius" << _radius;fs << "neighbors" << _neighbors;fs << "grid_x" << _grid_x;fs << "grid_y" << _grid_y;// write matriceswriteFileNodeList(fs, "histograms", _histograms);fs << "labels" << _labels;}void LBPH::train(InputArrayOfArrays _in_src, InputArray _in_labels) {this->train(_in_src, _in_labels, false);}void LBPH::update(InputArrayOfArrays _in_src, InputArray _in_labels) {// got no data, just returnif(_in_src.total() == 0)return;this->train(_in_src, _in_labels, true);}void LBPH::train(InputArrayOfArrays _in_src, InputArray _in_labels, bool preserveData) {if(_in_src.kind() != _InputArray::STD_VECTOR_MAT && _in_src.kind() != _InputArray::STD_VECTOR_VECTOR) {string error_message = "The images are expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."; CV_Error(CV_StsBadArg, error_message);}if(_in_src.total() == 0) {string error_message = format("Empty training data was given. You'll need more than one sample to learn a model.");CV_Error(CV_StsUnsupportedFormat, error_message);} else if(_in_labels.getMat().type() != CV_32SC1) {string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _in_labels.type());CV_Error(CV_StsUnsupportedFormat, error_message);}// get the vector of matricesvector<Mat> src;_in_src.getMatVector(src);// get the label matrixMat labels = _in_labels.getMat();// check if data is well- alignedif(labels.total() != src.size()) {string error_message = format("The number of samples (src) must equal the number of labels (labels). Was len(samples)=%d, len(labels)=%d.", src.size(), _labels.total());CV_Error(CV_StsBadArg, error_message);}// if this model should be trained without preserving old data, delete old model dataif(!preserveData) {_labels.release();_histograms.clear();}// append labels to _labels matrixfor(size_t labelIdx = 0; labelIdx < labels.total(); labelIdx++) {_labels.push_back(labels.at<int>((int)labelIdx));}// store the spatial histograms of the original datafor(size_t sampleIdx = 0; sampleIdx < src.size(); sampleIdx++) {// calculate lbp imageMat lbp_image = elbp(src[sampleIdx], _radius, _neighbors);// get spatial histogram from this lbp imageMat p = spatial_histogram(lbp_image, /* lbp_image */static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */_grid_x, /* grid size x */_grid_y, /* grid size y */true);// add to templates_histograms.push_back(p);}}void LBPH::predict(InputArray _src, int &minClass, double &minDist) const {if(_histograms.empty()) {。

python调用OpenCV实现人脸识别功能

python调用OpenCV实现人脸识别功能

python调⽤OpenCV实现⼈脸识别功能Python调⽤OpenCV实现⼈脸识别,供⼤家参考,具体内容如下硬件环境:Win10 64位软件环境:Python版本:2.7.3IDE:JetBrains PyCharm 2016.3.2Python库:1.1) opencv-python(3.2.0.6)搭建过程:OpenCV Python库:1. PyCharm的插件源中选择opencv-python(3.2.0.6)库安装题外话:Python⼊门TipsPS1:如何安装whl⽂件1.先安装PIP2.CMD命令进⼊D:\Python27\Scripts⾥⾯后再执⾏PIP命令安装pip install wheel如果提⽰'pip'不是内部或外部命令,也不是可运⾏的程序或批处理⽂件:①将python安装⽬录下的scripts⽬录(例如D:\Python27\Scripts)添加到系统环境变量path⾥,注意前加分号。

再执⾏该命令pip install wheel②在cmd下进⼊到D:\Python27\Scripts⽬录下执⾏该命令pip install wheel3.把⽂件最好放在\Script⽂件夹⾥⾯再pip install xxxx.whl4.注意whl⽂件名不能改必须⼀模⼀样和原名PS2:到哪找?相关代码:import cv2import numpy as npdWindow("test") # Create a windowcap = cv2.VideoCapture(0) #Open camera onesuccess, frame = cap.read() #Read one frameprint("Camera open operation is: ", success);color = (255,0,0) #Config the colorclassfier = cv2.CascadeClassifier("Resources\haarcascade_frontalface_alt.xml")#Make sure this xml file is in the same directory with py file#Otherwise change it to absolute directory. This xml file can be found in D:\MyDocuments\Downloads\opencv\sources\data\haarcascadeswhile success:success, frame = cap.read()size = frame.shape[:2] #image = np.zeros(size, dtype = np.float16) #image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #cv2.equalizeHist(image, image) ##Below three lines config the minimal image sizedivisor = 8h, w = sizeminSize = ((int)(w/divisor), (int)(h/divisor))faceRects = classfier.detectMultiScale(image, 1.2, 2, cv2.CASCADE_SCALE_IMAGE, minSize) #Face detectif len(faceRects) > 0:#If face array length > 0for faceRect in faceRects: #Draw a rectangle for every facexf, yf, wf, hf = faceRectx = int((float)(xf))y = int((float)(yf))w = int((float)(wf))h = int((float)(hf))cv2.rectangle(frame, (x, y), (x + w, y + h), color)cv2.circle(frame, ((int)(x + 1.2 * w / 4), (int)(y + h / 3)), min((int)(w / 8), (int)(h / 8)), (255, 0, 0))cv2.circle(frame, ((int)(x + 2.8 * w / 4), (int)(y + h / 3)), min((int)(w / 8), (int)(h / 8)), (255, 0, 0))#cv2.rectangle(frame, ((int)(x + 3 * w / 8, (int)(y + 3 * h / 4))), ((int)(x + 5 * w / 8), (int)(y + 7 * h / 8)), (255, 0, 0)) cv2.imshow("test", frame) #Display imagekey = cv2.waitKey(10)c = chr(key & 255)if c in ['q', 'Q', chr(27)]:breakcv2.destroyWindow("test")以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

基于OpenCv的人脸识别系统设计与实现代码大全

基于OpenCv的人脸识别系统设计与实现代码大全

基于OpenCv的人脸识别系统设计与实现1.1 题目的主要研究内容(宋体四号加粗左对齐)(1)工作的主要描述:本文设计了基于 OpenCV 库的人脸识别系统,采用Haar 人脸特征,详细描述了人脸识别设计实现的方式,并设计所对应阶段的逻辑框架,最后结合逻辑框架运用 Python 语言编写并验证本文设计的人脸识别系统。

通过多次训练验证并实现人脸识别及判别功能。

(2)系统流程图:1.2 题目研究的工作基础或实验条件(1)硬件环境笔记本电脑,处理器:R7 5800H(2)软件环境开发语言:Python开发工具:PyCharm20211.3 数据集描述一个程序能识别给定图像或视频中的人脸。

实现这一目标的方法之一是用一系列分类好的图像来“训练”数据,并基于这些图像来进行识别。

获取数据:可以通过网上搜索图片或者自己录入(我们选择的是从网上下载某些人的图片来训练数据)有了数据,需要将这些样本图像加载到人脸识别算法中。

所有的人脸识别算法在它们的train()函数中都有两个参数:图像数组和标签数组。

这些标签表示进行识别时候某人人脸的ID,因此根据ID可以知道被识别的人是谁。

要做的这一点需要生成一个.yml文件。

1.4 人脸特征提取过程描述特征提取是人脸识别的关键问题之一。

本设计中主要采用的是Harr特征提取方法。

摄影作品可能包含很多令人愉悦的细节。

但是,由于灯光、视角、视距、摄像头抖动以及数字噪声的变化,图像细节变得不稳定。

人们在分类时不会受这些物理细节方面差异的影响。

提取出图像的细节对产生稳定分类结果和跟踪结果很有用处。

这些提取的结果被称为特征,专业的表述为:从图像数据中提取特征。

Haar特征是一种用于实现实时人脸跟踪的特征。

每一个Haar特征都描述了相邻图像区域的对比模式。

1.5 人脸识别过程描述人脸识别采用的是LBPH算法,LBPH(Local Binary Pattern Histogram)将检测到的人脸分为小单元,并将其与模型中的对应单元进行比较,对每个区域的匹配值产生一个直方图。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.打开Microsoft Visual Studio 2008,新建一个Win32控制台项目;
2.配置好项目的包含文件和库文件;
3.将……\OpenCV\data\haarcascades中的haarcascade_frontalface_alt.xml拷贝到所建项目的文件夹中;
4.然后添加代码:
#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
#include<stdio.h>
int_tmain(int argc, _TCHAR* argv[])
{
CvCapture* capture=0;
/*初始化一个视频捕获操作。

告诉底层的捕获api我想从Capture1.avi中捕获图片,底层api将检测并选择相应的解码器并做好准备工作*/
capture = cvCaptureFromFile( "F:\\1.avi"); //设置要读的视频(avi格式)
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
cascade = (CvHaarClassifierCascade*)cvLoad("haarcascade_frontalface_alt.xml",0,0,0);
if( !cascade || !capture )
return -1;
storage = cvCreateMemStorage(0);
/*创建一个窗口,用“Video”作为窗口的标识符*/
cvNamedWindow( "Video",1);
/*如果初始化失败,那么capture为空指针,程序停止,否则进入捕获循环*/
if( capture )
{
for(;;)
{
IplImage* frame = cvQueryFrame( capture );
IplImage* img = NULL;
CvSeq* faces;
if( !frame )
break;
img = cvCloneImage(frame);
img->origin = 0;
if( frame->origin )
cvFlip(img,img);
cvClearMemStorage( storage );
//目标检测
faces = cvHaarDetectObjects( img, cascade, storage,1.1, 2,
CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20) );
for( int i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
cvRectangle( img, cvPoint(r->x,r->y),
cvPoint(r->x+r->width,r->y+r->height), CV_RGB(255,0,0), 1);
}
cvShowImage( "Video", img );
//设置每帧图像的间隔
Sleep(50);
/*如果你敲了键盘,就退出程序,否则继续捕获下一帧*/
if( cvWaitKey(10)>0 )
break;
}
/*退出之前结束底层api的捕获操作,比如会使得别的程序无法访问已经被它们打开的文件*/ cvReleaseCapture( &capture);
}
/*销毁窗口*/
cvDestroyWindow("Video");
return 0;
}。

相关文档
最新文档