image = cv2.imread('./image.jpg')
cv2.namedWindow("camera", 0)#设置图片手动调节大小
cv2.imshow("camera", image)
cv2.waitKey(0)#设置任意键退出程序
cv2.destroyAllWindows()
cv2.namedWindow("camera", 0)
用来手动调节图片窗口的大小.xml
的文件下也可以的。import cv2
image = cv2.imread('./image.jpg')
#声明算法,给什么样的特征,识别什么样的事物:脸,眼睛等
face_detect = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
#人脸检查代码
face_zone = face_detect.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)#按1.1倍放大
print ('识别人脸的信息:',face_zone)
for x, y, w, h in face_zone:
#绘制人脸区域
cv2.rectangle(image, pt1=(x, y), pt2=(x+w, y+h),color=[0,0,255], thickness=2)#thickness表示线的粗细,矩形
#绘制圆
cv2.circle(image, center=(x+w//2, y+h//2), radius=w//2, color=[0,255,0], thickness=2)#radius表示半径
cv2.namedWindow("camera", 0)#设置图片手动调节大小
cv2.imshow("camera", image)
cv2.waitKey(0)#设置任意键退出程序
cv2.destroyAllWindows()
import cv2
face_detect = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
img = cv2.imread('./NBA5.jpg')
cv2.imshow('NBA', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#img是彩色图片
img = cv2.imread('./NBA5.jpg')
cv2.imshow('NBA', img)
#gray是黑白图片
gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
cv2.imshow('GRAY', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
face_zone = face_detect.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 4, maxSize = (55,55))
for x, y, w, h in face_zone:
cv2.rectangle(img, pt1 = (x, y), pt2 = (x+w, y+h), color = [0,0,255], thickness=2)
cv2.circle(img, center = (x + w//2, y + h//2), radius = w//2, color = [0,255,0], thickness = 2)
cv2.namedWindow("window", 0)#设置图片手动调节大小
#img是彩色图片
img = cv2.imread('./NBA5.jpg')
cv2.imshow('NBA', img)
#gray是黑白图片
gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
# cv2.imshow('GRAY', gray)
#检测的时候,我们使用的是黑白图片。单词detect是发现;查明;侦察出。
face_zone = face_detect.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 4, maxSize = (55,55))
print (face_zone)
for x, y, w, h in face_zone:
cv2.rectangle(img, pt1 = (x, y), pt2 = (x+w, y+h), color = [0,0,255], thickness=2)#rectangle是矩形,thickness是厚度
cv2.circle(img, center = (x + w//2, y + h//2), radius = w//2, color = [0,255,0], thickness = 2)
cv2.imshow('NBA1', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np
import cv2
face_detect =cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')#人脸识别算法
cap = cv2.VideoCapture('./shipin.mp4')#导入视频
flag, frame = cap.read()
参数 | 说明 |
---|---|
flag | 返回值为True和False,True和图片一起返回,当读完视频后返回False |
frame | 接受返回来的图片 |
gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
参数 | 说明 |
---|---|
scaleFactor | 检测像素点扩散的面积 |
minNeighbors | 像素点邻近值 |
for x, y, w, h in face_zone:
#矩形
cv2.rectangle(frame, pt1 = (x, y), pt2 = (x+w, y+h), color = [0,0,255], thickness=2)
#圆形
cv2.circle(frame, center = (x + w//2, y + h//2), radius = w//2, color = [0,255,0], thickness = 2)
问题: 怎么设置视频的展示速度和原视频一致?
(1)视频的帧数
cap.get(propId = cv2.CAP_PROP_FPS)#结果等于25,意思是每秒25帧
import numpy as np
import cv2
#调用人脸识别算法
face_detect =cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture('./shipin.mp4')#加载视频
while True:
flag, frame = cap.read()#读取视频片段
if flag == False:
break
gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)#灰色处理
#识别人脸像素
face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
#圈出人脸位置
for x, y, w, h in face_zone:
cv2.rectangle(frame, pt1 = (x, y), pt2 = (x+w, y+h), color = [0,0,255], thickness=2)
cv2.circle(frame, center = (x + w//2, y + h//2), radius = w//2, color = [0,255,0], thickness = 2)
cv2.imshow('video', frame)
#设置退出键和展示频率
if ord('q') == cv2.waitKey(40):
break
#释放资源
cv2.destroyAllWindows()
cap.release()
import cv2
# 识别电脑摄像头,并打开
cap = cv2.VideoCapture(0)
while True:
flag, frame = cap.read()
if flag == False:
break
gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
for x, y, w, h in face_zone:
cv2.rectangle(frame, pt1 = (x, y), pt2 = (x+w, y+h), color = [0,0,255], thickness=2)
cv2.circle(frame, center = (x + w//2, y + h//2), radius = w//2, color = [0,255,0], thickness = 2)
cv2.imshow('video', frame)
if ord('q') == cv2.waitKey(40):
break
cv2.destroyAllWindows()
cap.release()