一.简介
1.OpenCV:OpenCV的全称是Open Source Computer Vision Library,是一个开源的跨平台的计算机视觉库。可以运行在Linux、Windows、Android和macOS操作系统上,帮助人们快速构建复杂的视觉应用程序。
2.计算机视觉:计算机视觉(Computer Vision)就是利用计算机来处理图像,将来自静止或摄像机的数据转换成新的表示方式,获得我们想要的信息。
二.编程步骤
1.环境配置
2.读取图片
3.灰度转换
4.修改尺寸
5.绘制矩形
6.人脸检测
7.检测多个
8.视频检测
9.拍照保存
10.训练数据
11.人脸识别
三.简单实例
1.环境配置
python下编程,先安装库
pip install opencv-python
pip install opencv-contrib-python
通过pip下载的只是阉割版的,部分功能不全,要实现人脸识别还需在官网Home - OpenCV下载完整的库(官网有点慢,可以多等等)(在人脸检测的部分需要手动添加库的路径,我的是Mac所以路径是/Users/....)(主要用到haarcascade_frontalface_default.xml、haarcascade_frontalface_alt2.xml
)
2.读取图片
#导入cv模块
import cv2 as cv
#读取图片
img = cv.imread('face1.jpg')#图片路径
#显示图片
cv.imshow('read_img',img)
#等待
cv.waitKey(0)
#释放内存
cv.destroyAllWindows()
3.灰度转换
#导入cv模块
import cv2 as cv
#读取图片
img = cv.imread('face1.jpg')
#灰度转换
gray_img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#显示灰度图片
cv.imshow('gray',gray_img)
#保存灰度图片
cv.imwrite('gray_face1.jpg',gray_img)
#显示图片
cv.imshow('read_img',img)
#等待
cv.waitKey(0)
#释放内存
cv.destroyAllWindows()
4.修改尺寸
#导入cv模块
import cv2 as cv
#读取图片
img = cv.imread('face1.jpg')
#修改尺寸
resize_img = cv.resize(img,dsize=(200,200))
#显示原图
cv.imshow('img',img)
#显示修改后的
cv.imshow('resize_img',resize_img)
#打印原图尺寸大小
print('未修改:',img.shape)
#打印修改后的大小
print('修改后:',resize_img.shape)
#等待
while True:
if ord('q') == cv.waitKey(0):
break
#释放内存
cv.destroyAllWindows()
5.绘制矩形
#导入cv模块
import cv2 as cv
#读取图片
img = cv.imread('face1.jpg')
#坐标
x,y,w,h = 100,100,100,100
#绘制矩形
cv.rectangle(img,(x,y,x+w,y+h),color=(0,0,255),thickness=1)
#绘制圆形
cv.circle(img,center=(x+w,y+h),radius=100,color=(255,0,0),thickness=5)
#显示
cv.imshow('re_img',img)
while True:
if ord('q') == cv.waitKey(0):
break
#释放内存
cv.destroyAllWindows()
6.人脸检测
# 导入cv模块
import cv2 as cv
# 检测函数
def face_detect_demo():
gary = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier('/Users/huangjw/Downloads/opencv-4.x/data/haarcascades/haarcascade_frontalface_alt2.xml')
face = face_detect.detectMultiScale(gary, 1.01, 5, 0, (100, 100), (300, 300))
for x, y, w, h in face:
cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
cv.imshow('result', img)
# 读取图像
img = cv.imread('face1.jpg')
#img = cv.imread('/Users/huangjw/Downloads/mycodetest/opencv/data/hjw/2.hjw.jpg')
# 检测函数
face_detect_demo()
# 等待
while True:
if ord('q') == cv.waitKey(0):
break
# 释放内存
cv.destroyAllWindows()
7.检测多个(同时检测多个人脸)
# 导入cv模块
import cv2 as cv
# 检测函数
def face_detect_demo():
gary = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier( '/Users/huangjw/Downloads/opencv-4.x/data/haarcascades/haarcascade_frontalface_default.xml')
face = face_detect.detectMultiScale(gary)
for x, y, w, h in face:
cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
cv.imshow('result', img)
# 读取图像
img = cv.imread('face2.jpg')
# 检测函数
face_detect_demo()
# 等待
while True:
if ord('q') == cv.waitKey(0):
break
# 释放内存
cv.destroyAllWindows()
8.视频检测
# 导入cv模块
import cv2 as cv
# 检测函数
def face_detect_demo(img):
gary = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier(
'/Users/huangjw/Downloads/opencv-4.x/data/haarcascades/haarcascade_frontalface_default.xml')
face = face_detect.detectMultiScale(gary)
for x, y, w, h in face:
cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
cv.imshow('result', img)
# 读取摄像头
cap = cv.VideoCapture(0)
# 循环
while True:
flag, frame = cap.read()
if not flag:
break
face_detect_demo(frame)
if ord('q') == cv.waitKey(1):
break
# 释放内存
cv.destroyAllWindows()
# 释放摄像头
cap.release()
9.拍照保存
调用摄像头拍照,按下‘s’键拍照,将照片保存在指定路径,空格键退出程序
# 导入模块
import cv2
# 摄像头
cap = cv2.VideoCapture(0)
#falg = 1
num = 1
while (cap.isOpened()): # 检测是否在开启状态
ret_flag, Vshow = cap.read() # 得到每帧图像
cv2.imshow("Capture_Test", Vshow) # 显示图像
k = cv2.waitKey(1) & 0xFF # 按键判断
if k == ord('s'): # 保存
cv2.imwrite("/Users/huangjw/Downloads/mycodetest/opencv/data/four/" + str(num) + ".hjc" + ".jpg", Vshow)
print("success to save" + str(num) + ".jpg")
num += 1
elif k == ord(' '): # 退出
break
# 释放摄像头
cap.release()
# 释放内
cv2.destroyAllWindows()
10.训练数据
将所需人脸识别的图片训练,生成yml文件,以供人脸识别
import os
import cv2
import sys
from PIL import Image
import numpy as np
def getImageAndLabels(path):
facesSamples = []
ids = []
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
# 检测人脸
face_detector = cv2.CascadeClassifier(
'/Users/huangjw/Downloads/opencv-4.x/data/haarcascades/haarcascade_frontalface_alt2.xml')
# 打印数组imagePaths
print('数据排列:', imagePaths)
# 遍历列表中的图片
for imagePath in imagePaths:
# 打开图片,黑白化
PIL_img = Image.open(imagePath).convert('L')
# 将图像转换为数组,以黑白深浅
# PIL_img = cv2.resize(PIL_img, dsize=(400, 400))
img_numpy = np.array(PIL_img, 'uint8')
# 获取图片人脸特征
faces = face_detector.detectMultiScale(img_numpy)
# 获取每张图片的id和姓名
id = int(os.path.split(imagePath)[1].split('.')[0])
# 预防无面容照片
for x, y, w, h in faces:
ids.append(id)
facesSamples.append(img_numpy[y:y + h, x:x + w])
# 打印脸部特征和id
# print('fs:', facesSamples)
print('id:', id)
# print('fs:', facesSamples[id])
print('fs:', facesSamples)
# print('脸部例子:',facesSamples[0])
# print('身份信息:',ids[0])
return facesSamples, ids
if __name__ == '__main__':
# 图片路径
path = './data/four/'
# 获取图像数组和id标签数组和姓名
faces, ids = getImageAndLabels(path)
# 获取训练对象
recognizer = cv2.face.LBPHFaceRecognizer_create()
# recognizer.train(faces,names)#np.array(ids)
recognizer.train(faces, np.array(ids))
# 保存文件
recognizer.write('trainer/four.yml')
# save_to_file('names.txt',names)
11.人脸识别
通过之前训练好的数据,对所需识别的图像、视频等加以分类(一下程序需要修改训练数据路径、训练图片路径(获取图片名字)、识别图像的方式)
import cv2
import numpy as np
import os
# coding=utf-8
import urllib
import urllib.request
import hashlib
# 加载训练数据集文件
recogizer = cv2.face.LBPHFaceRecognizer_create()
recogizer.read('/Users/huangjw/Downloads/mycodetest/opencv/trainer/trainer.yml')
# recogizer.read('trainer/trainer.yml')
names = []
warningtime = 0
# 准备识别的图片
def face_detect_demo(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换为灰度
# face_detector = cv2.CascadeClassifier(
# '/Users/huangjw/Downloads/opencv-4.x/data/haarcascades/haarcascade_frontalface_alt2.xml')
# 加载分类器(opencv已经训练好了)
face_detector = cv2.CascadeClassifier(
'/Users/huangjw/Downloads/opencv-4.x/data/haarcascades/haarcascade_frontalface_default.xml')
face = face_detector.detectMultiScale(gray, 1.1, 5, cv2.CASCADE_SCALE_IMAGE, (100, 100), (500, 500))
# face=face_detector.detectMultiScale(gray)
for x, y, w, h in face:
cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
cv2.circle(img, center=(x + w // 2, y + h // 2), radius=w // 2, color=(0, 255, 0), thickness=1)
# 人脸识别
ids, confidence = recogizer.predict(gray[y:y + h, x:x + w])
# print('标签id:',ids,'置信评分:', confidence)
if confidence > 70:
global warningtime
warningtime += 1
if warningtime > 100:
# 发送警报
# warning()
print("陌生人")
warningtime = 0
cv2.putText(img, 'unkonw', (x + 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 1)
else:
#print(ids - 1)
cv2.putText(img, str(names[ids - 1]), (x + 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 1)
cv2.imshow('result', img)
# print('bug:',ids)
def name():
path = '/Users/huangjw/Downloads/mycodetest/opencv/data/jm/'
# names = []
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
print('数据排列:', imagePaths)
for imagePath in imagePaths:
name = str(os.path.split(imagePath)[1].split('.', 2)[1])
names.append(name)
#print(names)
# , cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 读取视频
#cap = cv2.VideoCapture('1.mp4')
# 读取摄像头
#cap = cv2.VideoCapture(0)
name()
print(names)
# names.reverse() # 队列反转
# print(names)
while True:
flag, frame = cap.read()
if not flag:
break
face_detect_demo(frame)
if ord(' ') == cv2.waitKey(10):
break
cv2.destroyAllWindows()
cap.release()
# print(names)
版权归原作者 a卷心菜 所有, 如有侵权,请联系我们删除。