本文介绍: 每个特征都是通过从黑色矩形下的像素总和减去白色矩形下的像素总和获得的单个值。scaleFactor调整哈尔级联器的人脸选框使其能框住人脸。测试一下,识别文字还是很准的!深度学习是计算机视觉最为重要的方法。具体实现还需要进一步优化!配置出现问题的,可以看看这篇。只要不测口,还是比较准确的。安装很简单,这里贴一个。
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 FilesTesseract_OCRtesseract.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])
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!
原文地址:https://blog.csdn.net/qq_44631615/article/details/134582283
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_40342.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。