![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8d0a947fd2988c7f1068080efcd1ddaf.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f670029fefa1056a905719cfebd6eba9.png)
Haar人脸识别方法
scaleFactor调整哈尔级联器的人脸选框使其能框住人脸
官方教程指路
每个特征都是通过从黑色矩形下的像素总和减去白色矩形下的像素总和获得的单个值
级联器模型文件位置
# -*- 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识别眼鼻口
# -*- 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()
识别嘴就会不精确了
# -*- 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()
识别鼻子
# -*- 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()
只要不测口,还是比较准确的
# -*- 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()
测口准确度太低!!!
# -*- 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进行车牌识别
安装很简单,这里贴一个安装教程
配置出现问题的,可以看看这篇博客
测试一下,识别文字还是很准的!!!
# -*- 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()
# -*- 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()
# -*- 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实现图像分类
# -*- 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])
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!