orb算法结合了Fast和Brief算法,提出了构造金字塔,为Fast特征点添加了方向,从而使得关键点具有了尺度不变性和旋转不变性。
# orb算法结合了Fast和Brief算法,提出了构造金字塔,为Fast特征点添加了方向,从而使得关键点具有了尺度不变性和旋转不变性。
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt;
img=cv.imread('opencv_4.png')
orb=cv.ORB_create(nfeatures=5000)
kp,des=orb.detectAndCompute(img,None)
img2=cv.drawKeypoints(img,kp,None,color=(0,0,255),flags=0)
plt.figure(figsize=(10,8),dpi=100)
plt.imshow(img2[:,:,::-1])
plt.xticks([]),plt.yticks([])
plt.show()
import numpy as np
import cv2 as cv
cap=cv.VideoCapture('opencv_5.wmv')
while(cap.isOpened()):
ret,frame=cap.read()
if ret ==True:
cv.imshow('frame',frame)
if cv.waitKey(25)&0xFF==ord('q'):
break
cap.release()
cv.destroyWindow()
第一个写的,但是debug还是没有调试出来
import cv2 as cv
import matplotlib.pyplot as plt
img=cv.imread('img.png')
gray=cv.cvtColor(img,cv.COLOR_RGB2GRAY)
# 实例化Openncv人脸和眼睛识别的分类器
face_cas=cv.CascadeClassifier("D:/anaconda3/anaconda3/envs/MachineLearning/Library/etc/haarcascades/haarcascade_frontalface_default.xml")
face_cas.load('haarcascade_frontalface_default.xml')
eye_cas=cv.CascadeClassifier("D:/anaconda3/anaconda3/envs/MachineLearning/Library/etc/haarcascades/haarcascade_eye.xml")
eye_cas.load("haarcascade_eye.xml")
# 调用识别人脸
face_rects=face_cas.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(50,50))
for faceRects in face_rects:
x,y,w,h =faceRects
#框出人脸
cv.rectangle(img,(x,y),(x+h,y+w),(0,255,0),3)
# 在识别出的人脸中进行眼睛的检测
roi_color=img[y:y+h,x:x+w]
roi_gray=gray[y:y+h,x:x+w]
eyes=eye_cas.detectMultiScale(roi_gray)
for(ex,ey,ew,eh) in eyes:
cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
# 检测结果绘制
plt.figure(figsize=(8,6),dpi=100)
cv.imshow(img,"img")
cv.waitKey(0)
第二个:
import cv2 as cv
import mediapipe as mp
# 进度条库
import tqdm
import matplotlib.pyplot as plt
# 定义可视化函数
def look_img(img):
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
# 导入BlazeFace人脸检测模型
mp_face_detection=mp.solutions.face_detection
# help(mp_face_detection.FaceDetection)
model=mp_face_detection.FaceDetection(
min_detection_confidence=0.5,#置信度阈值,过滤掉小于置信度 的预测框
model_selection=0,#选择模型,0适用于人脸距离镜头近(2m以内),1适用于距离远(5m以内)
)
img=cv.imread('img.png')
look_img(img)
# 将图像输入模型,获取预测结果
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
result=model.process(img_RGB)
print(result.detections)
# 上面可以看到所有人脸检测效果坐标
'''
relative_bounding_box {
xmin: 0.23513855040073395
ymin: 0.3091171085834503
width: 0.5619093179702759
height: 0.5618590116500854
}
以上是输出结果,,证明只有一个人脸框的相对信息,的确图片中也只有一个人像
'''
# 可视化人脸检测结果
mp_drawing=mp.solutions.drawing_utils
keypoint_style=mp_drawing.DrawingSpec(thickness=5,circle_radius=5,color=(0,255,0))
# 人脸检测框样式
box_style=mp_drawing.DrawingSpec(thickness=5,circle_radius=5,color=(255,0,0))
# 可视化人脸关键点和人脸框
annotated_image=img.copy()
for detection in result.detections:
mp_drawing.draw_detection(
annotated_image,
detection,
keypoint_drawing_spec=keypoint_style,
bbox_drawing_spec=box_style
)
look_img(annotated_image)
cv.imwrite('TestA',annotated_image)
主要运用了mediapipe包,可以调用摄像头并且显示多个人脸的时候都可以进行识别,测试时还可以识别出图片和动物的头像
import mediapipe as mp
import cv2 as cv
from tqdm import tqdm
import time
# 导入模型
mp_face_detection=mp.solutions.face_detection
model=mp_face_detection.FaceDetection(
min_detection_confidence=0.5,
model_selection=0,
)
# 导入可视化样式
# 可视化人脸检测结果
mp_drawing=mp.solutions.drawing_utils
keypoint_style=mp_drawing.DrawingSpec(thickness=5,circle_radius=5,color=(0,255,0))
# 人脸检测框样式
box_style=mp_drawing.DrawingSpec(thickness=5,circle_radius=5,color=(255,0,0))
# 处理单帧的函数
def process_frame(img):
#记录该帧处理的开始时间
start_time=time.time()
img_RGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
result=model.process(img_RGB)
if result.detections:
for detection in result.detections:
mp_drawing.draw_detection(
img,
detection,
keypoint_drawing_spec=keypoint_style,
bbox_drawing_spec=box_style
)
#记录该帧处理完毕的时间
end_time=time.time()
#计算每秒处理图像的帧数FPS
FPS=1/(end_time-start_time)
scaler=1
img=cv.putText(img,'FPS'+str(int(FPS)),(25*scaler,50*scaler),cv.FONT_HERSHEY_SIMPLEX,1.25*scaler,(255,0,0),1,8)
return img
#调用摄像获取每帧
cap=cv.VideoCapture(0)
cap.open(0)
# 无限循环,直到break被触发
while cap.isOpened():
success,frame=cap.read()
# if not success:
# print('ERROR')
# break
frame=process_frame(frame)
#展示处理后的三通道图像
cv.imshow('my_window',frame)
if cv.waitKey(1) &0xff==ord('q'):
break
cap.release()
cv.destroyAllWindows()