opencv

  • 时间:
  • 浏览:
  • 来源:互联网

前情提要:
在jupyter上使用了markdown语法,有注解,并通过教程形成了目录。
整体边际更加高效了。
在这里插入图片描述
点击目录内的内容可进行直接跳转。

人脸检测基础

显示图片-图片添加灰度-便于计算

cv2.imshow显示图片-RGB转换

import cv2
# 读取图片
img=cv2.imread('py1.jpg')
# 显示图片
cv2.imshow('img',img)
# 任意键关闭窗口,()内参数为检测频率,单位为ms
cv2.waitKey(0)
# 防治运算过载
cv2.destroyAllWindows()

plt.imshow呈现图片

用plt显示图片,直接显示是负片形式,原因是cv2的编码形式为BGR

import cv2
from matplotlib import pyplot as plt
img=cv2.imread('py1.jpg')
plt.imshow(img)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-490hK1YR-1634834413134)(output_6_0.png)]

去除plt负片效果(两种方法):
img = img[:, :, [2,1,0]]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

import cv2
from matplotlib import pyplot as plt
img=cv2.imread('py1.jpg')
# 添加转换代码恢复为正常颜色
img = img[:, :, [2,1,0]]
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hPDC08sn-1634834413137)(output_8_0.png)]

重新设定图片大小

import cv2
img=cv2.imread('py1.jpg')
# 重设大小,新定义
resize_img=cv2.resize(img,dsize=(200,250))
cv2.imshow('resize_img',resize_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2,os
img=cv2.imread('py1.jpg')
# 上灰色,需要重新定义
gray_img=cv2.cvtColor(img,code=cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_img',gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

窗口关闭

按下特定键Q关闭窗口

import cv2
img=cv2.imread('py1.jpg')
cv2.imshow('img',img)
while True:
    if ord('q')==cv2.waitKey(0):
        break
cv2.destroyAllWindows()

窗口大小可调节

namedWindow
参数0为可调节
窗口可调节设置要在的显示之前\

import cv2
img=cv2.imread('py1.jpg')
cv2.namedWindow('img',0)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

窗口大小重新设置

大小调节有了窗口之后才可以调整

import cv2
img=cv2.imread('py1.jpg')
cv2.imshow('img',img)
cv2.resizeWindow('img',500,100)
cv2.waitKey(0)
cv2.destroyAllWindows()

画图形(框、圆)

import cv2
img=cv2.imread('py1.jpg')
# 设定参数
x,y,w,h=50,50,80,80
# 参数赋值,图片名称,参数配置,颜色BGR,线条粗细
cv2.rectangle(img,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)
cv2.circle(img,center=(x+w//2,y+h//2),radius=w//2,color=(0,0,255),thickness=2)
cv2.imshow('1',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

导出并保存图片

plt 方法

import os
os.chdir('D:\pythonAds2021')
plt.savefig('1.jpg')

静态图片人脸检测

单张图片单人脸

导入图形
定义函数人脸识别:

  • 图片变灰
    
  • 加载特征数据,获取路径的两种方法:\
    

绝对路径face_detector=
cv2.CascadeClassifier(‘C:/Users/ouoli/anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml’)

非绝对路径.+方法face_cascade =
cv2.CascadeClassifier(cv2.data.haarcascades+‘haarcascade_frontalface_default.xml’)

  • 用特征数据识别图像,并返回特征区域→faces=特征数据.识别函数(detectMultiScale)[图形,参数]
    
  • 在特征区域内画图形
    
  • 展示图形
    

备注:加载灰度只是便于计算,灰度不会呈现到最终的结果中

import cv2
img=cv2.imread('aobama.png')
face_e=cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
faces=face_e.detectMultiScale(img,1.3,8)
for x,y,w,h in faces:
    cv2.rectangle(img,(x,y),(x+h,y+w),color=(255,255,0),thickness=5)
    cv2.circle(src,center=(x+w//2,y+h//2),radius=w//2,color=(0,0,255),thickness=2)

cv2.namedWindow('1',0)
cv2.resizeWindow('1',500,550)

cv2.imshow('1',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

单张照片多人脸识别

识别多张人脸,改一下文件名就行~~
函数方法调用

# 导入图片
import cv2,os
import numpy as np
from matplotlib import pyplot as plt
os.chdir('D:\pythonAds2021')

# 定义函数
def face_detect_demo():
#     变灰
    gray=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
#     加载特征数据
    face_detector=cv2.CascadeClassifier('C:/Users/ouoli/anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml')
#     进行人脸识别,返回人脸区域
    faces=face_detector.detectMultiScale(gray,1.01,5)
#     在人脸区域内画图形
    for x,y,w,h in faces:
        cv2.circle(src,center=(x+w//2,y+h//2),radius=w//2,color=(0,0,255),thickness=2)
        cv2.rectangle(src,(x,y),(x+w,y+h),color=(0,0,255))
    cv2.imshow('result',src)
src= cv2.imread('wulin.jpg')
face_detect_demo()
cv2.waitKey(0)
cv2.destroyAllWindows()

动态实时人脸检测

调取动态人脸检测,需要用while True保持软件进程。\

摄像头

如何调取摄像头

调取图像
设置人眼和人脸识别函数
执行函数

import cv2
#调用笔记本内置摄像头,所以参数为0,如果有其他的摄像头可以调整参数为1,2
cap=cv2.VideoCapture(0)
while True:
    #从摄像头读取图片
    sucess,img=cap.read()
    #显示摄像头
    cv2.imshow("img",img)
    #保持画面的持续。
    k=cv2.waitKey(1)
    if k == 27:
        #通过esc键退出摄像
        cv2.destroyAllWindows()
        break
    elif k==ord("s"):
        #通过s键保存图片,并退出。
        cv2.imwrite("image2.jpg",img)
        cv2.destroyAllWindows()
        break
# 关闭摄像头
cap.release()

极简版本

import cv2
#读取图像
cap=cv2.VideoCapture(0)
while True:
    ret,img=cap.read()
#显示图像
    cv2.imshow('img',img)
#设置图像终止情况
    k=cv2.waitKey(1)
    if k==27:
        break
cv2.destroyAllWindows()
# 关闭摄像头
cap.release()

实时边缘检测

import cv2
# import numpy as np
cap=cv2.VideoCapture(0)
while True:
    ret,img=cap.read()
    img=cv2.Canny(img,100,200)
#     img=np.dstack((img,img,img))
    cv2.imshow('img',img)
    k=cv2.waitKey(1)
    if k==27:
        break
cv2.destroyAllWindows()
# 关闭摄像头
cap.release()

实时人脸、眼睛检测

import cv2,os
os.chdir('D:\pythonAds2021')

def detect_F(frame):
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    img = frame
    for (x,y,w,h) in faces:
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        face_area = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(face_area)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
#     cv2.namedWindow('frame2',0)
    cv2.imshow('frame2',img)
    
cap = cv2.VideoCapture(0)

while(True):
    ret, src = cap.read()
#     通过函数显示处理后的图像
    detect_F(src)
    k=cv2.waitKey(100)
    if k == 27:
        break
    elif k==ord('s'):
        cv2.imwrite("image2.jpg",img)
        break
cv2.destroyAllWindows()
# 关闭摄像头
cap.release()

实时微笑检测

import cv2,os
os.chdir('D:\pythonAds2021')

def detect_F(frame):
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')
    smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_smile.xml')
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    img = frame
    for (x,y,w,h) in faces:
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        face_area = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(face_area,1.3,25)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),5)
        smiles = smile_cascade.detectMultiScale(face_area,1.3,50)
        for (sx,sy,sw,sh) in smiles:
            cv2.rectangle(face_area,(sx,sy),(sx+sw,sy+sh),(0,0,255),5)
# 实时字幕显示
            cv2.putText(img,'ye smile le',(x,y-7),3,1.2,(0,0,255),2)
#     cv2.namedWindow('frame2',0)
    cv2.imshow('frame2',img)
    
cap = cv2.VideoCapture(0)

while(True):
    ret, src = cap.read()
#     通过函数显示处理后的图像
    detect_F(src)
    k=cv2.waitKey(100)
    if k == 27:
        break
    elif k==ord('s'):
        cv2.imwrite("image2.jpg",img)
        break
cv2.destroyAllWindows()
# 关闭摄像头
cap.release()

摄像头视频保存

import cv2 as cv
import os
os.chdir('D:\pythonAds2021')
cap=cv.VideoCapture(0)

while cap.isOpened():
    ret,frame=cap.read()
    if not ret:
        break
    cv.imshow('img',frame)
    k=cv.waitKey(1)
    if k==27:
        break
    fourcc=cv.VideoWriter_fourcc(*'DIVX')
    out=cv.VideoWriter('output.mp4',fourcc,20.0,(640,480))
cv.destroyAllWindows()
cap.release()

如何读取视频

import cv2
cap=cv2.VideoCapture('aoligei.mp4')
while(True):
    ret, i = cap.read()
#增加视频结束就停止模块
    if not ret:
        break
# 显示视频
    cv2.imshow('i',i)
    k=cv2.waitKey(2)
    if k==27:
        break
cv2.destroyAllWindows()

读取视频实时人脸检测

比摄像头读取多加一个视频结束模块

人脸对齐

人脸标定点检测

过程拆解
读取图像
转换为灰度图

识别定点
获取人脸检测的数据

打印人脸标定点

import cv2
import dlib
import matplotlib.pyplot as plt

path = "py1.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
#predictor = dlib.shape_predictor(r"C:\Python36\Lib\site-packages\face_recognition_models\models\shape_predictor_68_face_landmarks.dat")
predictor = dlib.shape_predictor(r"C:\Users\ouoli\anaconda3\Lib\site-packages\face_recognition_models\models\shape_predictor_5_face_landmarks.dat")

dets = detector(gray, 1)
for face in dets:
    shape = predictor(img, face)  # 寻找人脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        img=cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
        
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)#还原图像颜色
plt.imshow(img)
plt.axis('off')  #去掉坐标轴
plt.savefig('new1.jpg',dpi=200)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bxT2iaxe-1634834413138)(output_51_0.png)]

人脸识别

import cv2
import face_recognition
import matplotlib.pyplot as plt

known_image=cv2.imread("wf.jpg")
known_image = face_recognition.load_image_file("wf.jpg")

unknown_image=cv2.imread("xn.jpeg")
unknown_image = face_recognition.load_image_file("xn.jpeg")

known_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([known_encoding],
                                         unknown_encoding,
                                         tolerance=0.6)
if results[0] == True:
    print("匹配成功,该未知图片与已有图片人脸可匹配!")
else:
    print("匹配失败!")
# print(known_encoding) 
# print(unknown_encoding)
plt.imshow(known_image)
plt.axis('off')  #去掉坐标轴
plt.show()

plt.imshow(unknown_image)
plt.axis('off')  #去掉坐标轴
plt.show()

---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-15-0020b84e34ec> in <module>
      1 import cv2
----> 2 import face_recognition
      3 import matplotlib.pyplot as plt
      4 
      5 known_image=cv2.imread("wf.jpg")


ModuleNotFoundError: No module named 'face_recognition'

人脸替换

import face_recognition
import cv2
import matplotlib.pyplot as plt
from pylab import *
from PIL import Image

image = face_recognition.load_image_file("D:\pythonAds2021\py1.jpg")
face_locations=face_recognition.face_locations(image)

face_num2=len(face_locations)
print(face_num2)       # The number of faces
org = cv2.imread("D:\pythonAds2021\py1.jpg")
box={}

for i in range(0,face_num2):
    top = face_locations[i][0]
    right = face_locations[i][1]
    bottom = face_locations[i][2]
    left = face_locations[i][3]
    length = bottom-top
    height = right-left
    box[i] =left, top, left+length, top+height
 
print(box[0])

pil_im = Image.open('D:\pythonAds2021\py1.jpg')
region = pil_im.crop(box[1])
#region = region.transpose(Image.ROTATE_180)
pil_im.paste(region, box[0])
#subplot(233)
#title(u'复制粘贴区域', fontproperties=font)
axis('off')
imshow(pil_im)
---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-1-c19b7157a603> in <module>
----> 1 import face_recognition
      2 import cv2
      3 import matplotlib.pyplot as plt
      4 from pylab import *
      5 from PIL import Image


ModuleNotFoundError: No module named 'face_recognition'
import requests,os  #导入requests库
import base64    #导入base64库
from PIL import Image
import matplotlib.pyplot as plt

os.chdir('D:\pythonAds2021')

# API_Key='wLXiL5dy2qrO6YErbUm9dF_kr4wP7EVc'
# API_Secret  = 'iEFrwgEzU_cIyrvMOSOw_vGUjNttgLhs'
#借助https://console.faceplusplus.com.cn平台来实现换脸
API_Key = 'CGj6vYsWyRnKPPfvJNNIuSujuVVyJ095'    #你自己申请的API Key
API_Secret = '7QMUgWN7CA8QlAdfDdNnQnNJX4fnZlu7'   ##你自己申请的API Secret


# 定义一个获取图片的人脸特征参数的函数
def find_face(imgpath):    #查找人脸参数(图片位置)
    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'    #使用的人脸识别网站
    data = {'api_key': API_Key, 'api_secret': API_Secret, 'image_url': imgpath, 'return_landmark': 1}    #相关个人账号参数等
    files = {'image_file': open(imgpath, 'rb')}    #打开图片
    response = requests.post(url, data=data, files=files)    #用requests.poet()函数将个人账号参数和图片发送到网站
    res_json = response.json()    #转换为json
    faces = res_json['faces'][0][
        'face_rectangle']  # 获取面部大小的四个值,分别为长宽高低{'width': 176, 'top': 128, 'left': 80, 'height': 176}
    return faces    #返回图片的面部参数


# 定义一个换脸函数,函数中number表示两张脸的相似度为99%
def change_face(image_1, image_2, number=99):
    url = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"    #使用的换脸网址
    find_p1 = find_face(image_1)    #第1张图片的人脸参数
    find_p2 = find_face(image_2)    #第2张图片的人脸参数
    rectangle1 = str(str(find_p1['top']) + ',' + str(find_p1['left']) + ',' + str(find_p1['width']) + ',' + str(find_p1['height']))  # 得到图片1坐标
    rectangle2 = str(str(find_p2['top']) + ',' + str(find_p2['left']) + ',' + str(find_p2['width']) + ',' + str(find_p2['height']))  # 得到图片2坐标

    page1 = open(image_1, 'rb')  #以二进制打开图片1
    page1_64 = base64.b64encode(page1.read())  #将字符串转成成base64编码
    page1.close()  #关闭图片1

    page2 = open(image_2, 'rb')   #二进制打开图片2
    page2_64 = base64.b64encode(page2.read())    #将字符串转成成base64编码
    page2.close()   #关闭图片2

    data = {'api_key': API_Key, 'api_secret': API_Secret, 'template_base64': page1_64,
            'template_rectangle': rectangle1, 'merge_base64': page2_64, 'merge_rectangele': rectangle2,
            'merge_rate': number}    #参数信息
    response = requests.post(url, data=data).json()    #发送参数到换脸网站
    results = response['result']    #得到返回参数
    image = base64.b64decode(results)    #转换信息
    with open('新图片.jpg', 'wb') as file:    #将信息写入到图片
        file.write(image)
    print("转换完成了!")


if __name__ == '__main__':
    change_face('xn.jpg','wf.jpg')

img1=plt.imread('xn.jpg')
plt.axis('off')
plt.imshow(img1)
plt.show()
img2=plt.imread('wf.jpg')
plt.axis('off')
plt.imshow(img2)
plt.show()
img0=Image.open('新图片.jpg')
plt.axis('off')
plt.imshow(img0)
plt.show()
转换完成了!

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I2esJ0rQ-1634834413140)(output_56_1.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DPOpFill-1634834413141)(output_56_2.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8DXSC5ku-1634834413142)(output_56_3.png)]

import cv2,os
os.chdir('D:\pythonAds2021')

def detect_F(frame):
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    for (x,y,w,h) in faces:
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        face_area = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(face_area)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
        cv2.imshow('frame2',img)

        
cap = cv2.VideoCapture('aoligei.mp4')
while(True):
    ret, i = cap.read()
# 增加视频结束就停止模块
    if not ret:
        break
    detect_F(i)
    k=cv2.waitKey(5)
    if k == 27:
        break
    elif k==ord('s'):
        cv2.imwrite("image2.jpg",img)
        break
cv2.destroyAllWindows()

本文链接http://metronic.net.cn/metronic/show-64476.html