OpenCV完结篇——计算机视觉(人脸识别 || 车牌识别)

文章目录


Haar人脸识别方法



scaleFactor调整哈尔级联器的人脸选框使其能框住人脸

官方教程指路

每个特征都是通过从黑色矩形下的像素总和减去白色矩形下的像素总和获得的单个值

级联器模型文件位置

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('E:/pic/Pic/11.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行人脸识别
#[[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

Haar识别眼鼻口

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascade_eye.xml')
mouse = cv2.CascadeClassifier('./haarcascade_mcs_mouth.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('E:/pic/Pic/11.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行人脸识别
#[[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.1, 5)
eyes = eye.detectMultiScale(gray, 1.1, 5)
mouses = mouse.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

for (x, y, w, h) in eyes:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 3)

# for (x, y, w, h) in mouses:
#     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

识别嘴就会不精确了

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascade_eye.xml')
mouse = cv2.CascadeClassifier('./haarcascade_mcs_mouth.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('E:/pic/Pic/11.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行人脸识别
#[[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.1, 5)
eyes = eye.detectMultiScale(gray, 1.1, 5)
mouses = mouse.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

for (x, y, w, h) in eyes:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 3)

for (x, y, w, h) in mouses:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

识别鼻子

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascade_eye.xml')
mouse = cv2.CascadeClassifier('./haarcascade_mcs_mouth.xml')
nose = cv2.CascadeClassifier('./haarcascade_mcs_nose.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('E:/pic/Pic/11.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行人脸识别
#[[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.1, 5)
eyes = eye.detectMultiScale(gray, 1.1, 5)
mouses = mouse.detectMultiScale(gray, 1.1, 5)
noses = nose.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

for (x, y, w, h) in eyes:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 3)

for (x, y, w, h) in mouses:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)

for (x, y, w, h) in noses:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

只要不测口,还是比较准确的

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascade_eye.xml')
mouse = cv2.CascadeClassifier('./haarcascade_mcs_mouth.xml')
nose = cv2.CascadeClassifier('./haarcascade_mcs_nose.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('E:/pic/Pic/11.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行人脸识别
#[[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.1, 5)
# eyes = eye.detectMultiScale(gray, 1.1, 5)
# mouses = mouse.detectMultiScale(gray, 1.1, 5)
# noses = nose.detectMultiScale(gray, 1.1, 5)

i = 0

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
    roi_img = img[y: y+h, x:x+w]
    eyes = eye.detectMultiScale(roi_img, 1.1, 5)
    for (x, y, w, h) in eyes:
        cv2.rectangle(roi_img, (x, y), (x + w, y + h), (255, 255, 0), 3)
    noses = nose.detectMultiScale(roi_img, 1.1, 5)
    for (x, y, w, h) in noses:
        cv2.rectangle(roi_img, (x, y), (x + w, y + h), (0, 0, 255), 3)
    # mouses = mouse.detectMultiScale(roi_img, 1.1, 5)
    # for (x, y, w, h) in mouses:
    #     cv2.rectangle(roi_img, (x, y), (x + w, y + h), (0, 255, 255), 3)
    
    # i += 1
    # winname = 'face' + str(i)
    # cv2.imshow(winname, roi_img)

# for (x, y, w, h) in mouses:
#     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)

# for (x, y, w, h) in noses:
#     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

测口准确度太低!!!

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascade_eye.xml')
mouse = cv2.CascadeClassifier('./haarcascade_mcs_mouth.xml')
nose = cv2.CascadeClassifier('./haarcascade_mcs_nose.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('E:/pic/Pic/11.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行人脸识别
#[[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.1, 5)
# eyes = eye.detectMultiScale(gray, 1.1, 5)
# mouses = mouse.detectMultiScale(gray, 1.1, 5)
# noses = nose.detectMultiScale(gray, 1.1, 5)

i = 0

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
    roi_img = img[y: y+h, x:x+w]
    eyes = eye.detectMultiScale(roi_img, 1.1, 5)
    for (x, y, w, h) in eyes:
        cv2.rectangle(roi_img, (x, y), (x + w, y + h), (255, 255, 0), 3)
    noses = nose.detectMultiScale(roi_img, 1.1, 5)
    for (x, y, w, h) in noses:
        cv2.rectangle(roi_img, (x, y), (x + w, y + h), (0, 0, 255), 3)
    mouses = mouse.detectMultiScale(roi_img, 1.1, 5)
    for (x, y, w, h) in mouses:
        cv2.rectangle(roi_img, (x, y), (x + w, y + h), (0, 255, 255), 3)
    
    # i += 1
    # winname = 'face' + str(i)
    # cv2.imshow(winname, roi_img)

# for (x, y, w, h) in mouses:
#     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 3)

# for (x, y, w, h) in noses:
#     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

Haar+Tesseract进行车牌识别




安装很简单,这里贴一个安装教程

配置出现问题的,可以看看这篇博客

测试一下,识别文字还是很准的!!!

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
carplate = cv2.CascadeClassifier('./haarcascade_russian_plate_number.xml')

#第二步,导入带车牌的图片并将其灰度化
img = cv2.imread('./chinacar.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行车牌定位
#[[x,y,w,h]]
carplates = carplate.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in carplates:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

cv2.imshow('img', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()
python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
carplate = cv2.CascadeClassifier('./haarcascade_russian_plate_number.xml')

#第二步,导入带车牌的图片并将其灰度化
img = cv2.imread('./chinacar.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行车牌定位
#[[x,y,w,h]]
carplates = carplate.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in carplates:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

# 对获取到的车牌进行预处理
# 1.提取ROI
roi = gray[y: y+h, x:x+w]
# 2.进行二值化
ret, roi_bin = cv2.threshold(roi, 10, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

cv2.imshow('img', img)
cv2.imshow('roi_bin', roi_bin)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()



python 复制代码
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# 引入tesseract库
import pytesseract

# cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#第一步,创建Haar级联器
carplate = cv2.CascadeClassifier('./haarcascade_russian_plate_number.xml')

#第二步,导入带车牌的图片并将其灰度化
img = cv2.imread('./chinacar.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#第三步,进行车牌定位
#[[x,y,w,h]]
carplates = carplate.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in carplates:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

# 对获取到的车牌进行预处理
# 1.提取ROI
roi = gray[y: y+h, x:x+w]
# 2.进行二值化
ret, roi_bin = cv2.threshold(roi, 10, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

pytesseract.pytesseract.tesseract_cmd = r'D:\Program Files\Tesseract_OCR\tesseract.exe'
print(pytesseract.image_to_string(roi, lang='chi_sim+eng', config='--psm 8 --oem 3'))

cv2.imshow('img', img)
cv2.imshow('roi_bin', roi_bin)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

具体实现还需要进一步优化!!!

深度学习基础知识

深度学习是计算机视觉最为重要的方法














dnn实现图像分类





python 复制代码
# -*- coding: utf-8 -*-
import cv2
from cv2 import dnn
import numpy as np

# 1.导入模型,创建神经网络
# 2.读取图片,转成张量
# 3.将张量输入到网络中,并进行预测
# 4.得到结果,显示

# 导入模型,创建神经网络
config = "./bvlc_googlenet.prototxt"
model = "./bvlc_googlenet.caffemodel"
net = dnn.readNetFromCaffe(config, model)

# 读取图片,转成张量
img = cv2.imread('./smallcat.jpeg')
blob = dnn.blobFromImage(img, 1.0, (224, 224), (104, 117, 123))

# 将张量输入到网络中,并进行预测
net.setInput(blob)
r = net.forward()

# 读取类目
classes = []
path = './synset_words.txt'
with open(path, 'rt') as f:
    classes = [x [x.find(" ") + 1:] for x in f]

order = sorted(r[0], reverse=True)
z = list(range(3))

for i in list(range(0, 3)):
    z[i] = np.where(r[0] == order[i])[0][0]
    print('No.', i + 1, ' matches:', classes[z[i]], end='')
    print('category row is at:', z[i] + 1, ' ', 'posibility:', order[i])


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
qzhqbb24 分钟前
基于统计方法的语言模型
人工智能·语言模型·easyui
冷眼看人间恩怨1 小时前
【话题讨论】AI大模型重塑软件开发:定义、应用、优势与挑战
人工智能·ai编程·软件开发
2401_883041081 小时前
新锐品牌电商代运营公司都有哪些?
大数据·人工智能
pianmian11 小时前
python数据结构基础(7)
数据结构·算法
闲晨1 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
AI极客菌2 小时前
Controlnet作者新作IC-light V2:基于FLUX训练,支持处理风格化图像,细节远高于SD1.5。
人工智能·计算机视觉·ai作画·stable diffusion·aigc·flux·人工智能作画
阿_旭2 小时前
一文读懂| 自注意力与交叉注意力机制在计算机视觉中作用与基本原理
人工智能·深度学习·计算机视觉·cross-attention·self-attention
王哈哈^_^2 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
幼儿园老大*2 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
考试宝2 小时前
国家宠物美容师职业技能等级评价(高级)理论考试题
经验分享·笔记·职场和发展·学习方法·业界资讯·宠物