当前位置:   article > 正文

Pycharm - OpenCV 01人脸识别_pycharm人脸识别

pycharm人脸识别
人脸识别

Python
Pycharm
Python-opencv

1. Pycharm+opencv
import numpy as np
import cv2 as cv
# 编辑要调用的方法
def fac_detect_demo():
    # 将图片转换为灰度图
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    # 人脸检测参量
    face_detector = cv.CascadeClassifier('D:/LenovoQMDownload/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
    # 对gray进行识别,人脸检测完成后返回一个人脸区域faces
    faces = face_detector.detectMultiScale(gray)
    # x,y是坐标,w,h是宽度和高度
    for x, y, w, h in faces:
        # 在彩色图像上绘制矩形框,调用rectangle,(x,y)是左上角,(x + w, y + h)右下角,图框颜色BGR绿色,图框宽度
        cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)
        # 画完图框后进行显示
        cv.imshow('result', img)
img = cv.imread('E:\\Picture\\example.jpg')
cv.imshow('input_img', img)
# 调用函数
fac_detect_demo()
cv.waitKey(0)
# 释放内存空间
cv.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

2. VS 2019+opencv

调整了一下算法适合 VS 2019

#include<opencv2\opencv.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_default.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;   //定义人脸分类器
CascadeClassifier eyes_cascade;   //定义人眼分类器
String window_name = "Capture - Face detection";

/** @function main */
int main(void)
{
	Mat frame = imread("E:\\Picture\\orl_faces\\1.pgm");

	//VideoCapture capture;
	//Mat frame;

	//-- 1. Load the cascades
	if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); return -1; };
	if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading eyes cascade\n"); return -1; };

	//-- 2. Read the video stream
	//capture.open(0);
	//if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }

	//while (capture.read(frame))
	//{
	//	if (frame.empty())
	//	{
	//		printf(" --(!) No captured frame -- Break!");
	//		break;
	//	}

		//-- 3. Apply the classifier to the frame
	detectAndDisplay(frame);

	int c = waitKey(0);
	if ((char)c == 27) { return 0; } // escape
//}
	return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
	std::vector<Rect> faces;
	Mat frame_gray;

	cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
	equalizeHist(frame_gray, frame_gray);

	//-- Detect faces
	face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3, CASCADE_SCALE_IMAGE, Size(70, 70), Size(100, 100));

	for (size_t i = 0; i < faces.size(); i++)
	{
		//Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
		//ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
		rectangle(frame, faces[i], Scalar(255, 0, 0), 2, 8, 0);

		Mat faceROI = frame_gray(faces[i]);
		std::vector<Rect> eyes;

		//-- In each face, detect eyes
		eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 1, CASCADE_SCALE_IMAGE, Size(3, 3));

		for (size_t j = 0; j < eyes.size(); j++)
		{
			Rect rect(faces[i].x + eyes[j].x, faces[i].y + eyes[j].y, eyes[j].width, eyes[j].height);

			//Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
			//int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
			//circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
			rectangle(frame, rect, Scalar(0, 255, 0), 2, 8, 0);
		}
	}
	//-- Show what you got
	namedWindow(window_name, 2);
	imshow(window_name, frame);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

参考(感谢)
1: 人脸检测
2: haarcascade_frontalface_alt2.xml.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/101020
推荐阅读
相关标签
  

闽ICP备14008679号