以前的
1.检测多个人脸
#导入cv模块
import cv2 as cv
#检测函数
def face_detect_demo():
gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier('D:/Junior second/shixun/OPENCV(WIN)/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
face = face_detect.detectMultiScale(gary)
for x,y,w,h in face:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
cv.imshow('result',img)
#读取图像
img = cv.imread('face2.jpg')
#检测函数
face_detect_demo()
#等待
while True:
if ord('q') == cv.waitKey(0):
break
#释放内存
cv.destroyAllWindows()
效果:识别图片中的所有人脸
2.视频检测
#导入cv模块
import cv2 as cv
#检测函数
def face_detect_demo(img):
gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier('D:/Junior second/shixun/OPENCV(WIN)/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
face = face_detect.detectMultiScale(gary)
for x,y,w,h in face:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
cv.imshow('result',img)
#读取摄像头
#cap = cv2.VideoCapture(0)
#读取视频
cap = cv.VideoCapture('3.mp4')
#循环
while cap.isOpened():
ret, frame = cap.read()
# 调整窗口大小
cv.namedWindow("result", 0) # 0可调大小,注意:窗口名必须imshow里面的一窗口名一直
cv.resizeWindow("result", 800, 450) # 设置长和宽
cv.imshow('result', frame)
face_detect_demo(frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
#释放内存
cv.destroyAllWindows()
#释放摄像头
cap.release()
效果:播放视频时人脸识别
3. 人脸录入&数据训练
import os
import cv2
import sys
from PIL import Image
import numpy as np
def getImageAndLabels(path):
facesSamples=[]
ids=[]
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
#检测人脸
face_detector = cv2.CascadeClassifier('D:/Junior second/shixun/OPENCV(WIN)/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
#打印数组imagePaths
print('数据排列:',imagePaths)
#遍历列表中的图片
for imagePath in imagePaths:
#打开图片,黑白化
PIL_img=Image.open(imagePath).convert('L')
#将图像转换为数组,以黑白深浅
#PIL_img = cv2.resize(PIL_img, dsize=(400, 400))
#人脸数组列表
img_numpy=np.array(PIL_img,'uint8')
#获取图片人脸特征
faces = face_detector.detectMultiScale(img_numpy)
#获取每张图片的id和姓名
id = int(os.path.split(imagePath)[1].split('.')[0])
#预防无面容照片
for x,y,w,h in faces:
ids.append(id)
facesSamples.append(img_numpy[y:y+h,x:x+w])
#打印脸部特征和id
#print('fs:', facesSamples)
print('id:', id)
#print('fs:', facesSamples[id])
print('fs:', facesSamples)
#print('脸部例子:',facesSamples[0])
#print('身份信息:',ids[0])
return facesSamples,ids
if __name__ == '__main__':
#图片路径
path='./data/jm/'
#获取图像数组和id标签数组和姓名
faces,ids=getImageAndLabels(path)
#获取训练对象
recognizer=cv2.face.LBPHFaceRecognizer_create()
#recognizer.train(faces,names)#np.array(ids)
recognizer.train(faces,np.array(ids))
#保存文件
recognizer.write('trainer/trainer.yml')
#save_to_file('names.txt',names)
获取人脸的灰度值(颜色越浅,值越小)
得到trainer.yml训练结果,用于后续识别人和id的对应关系。
注意:卸载原来的opencv-python,安装opencv-contrib-python
4.视频人脸识别
import cv2
import numpy as np
import os
# coding=utf-8
import urllib
import urllib.request
import hashlib
#加载训练数据集文件
recogizer=cv2.face.LBPHFaceRecognizer_create()
recogizer.read('trainer/trainer.yml')
names=[]
warningtime = 0
def md5(str):
import hashlib
m = hashlib.md5()
m.update(str.encode("utf8"))
return m.hexdigest()
data = urllib.parse.urlencode({'u': user, 'p': password, 'm': phone, 'c': content})
send_url = smsapi + 'sms?' + data
response = urllib.request.urlopen(send_url)
the_page = response.read().decode('utf-8')
print(statusStr[the_page])
#准备识别的图片
def face_detect_demo(img):
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#转换为灰度
face_detector=cv2.CascadeClassifier('D:/Junior second/shixun/OPENCV(WIN)//opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
face=face_detector.detectMultiScale(gray,1.1,5,cv2.CASCADE_SCALE_IMAGE,(100,100),(300,300))
#face=face_detector.detectMultiScale(gray)
for x,y,w,h in face:
cv2.rectangle(img,(x,y),(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=1)
# 人脸识别
ids, confidence = recogizer.predict(gray[y:y + h, x:x + w])
#print('标签id:',ids,'置信评分:', confidence)
if confidence > 80: #值越大,越不可信
global warningtime
warningtime += 1
if warningtime > 100:
warning()
warningtime = 0
cv2.putText(img, 'unkonw', (x + 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 1)
else:
cv2.putText(img,str(names[ids-1]), (x + 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 1)
cv2.imshow('result',img)
#print('bug:',ids)
def name():
path = './data/jm/'
#names = []
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
for imagePath in imagePaths:
name = str(os.path.split(imagePath)[1].split('.',2)[1])
names.append(name)
cap=cv2.VideoCapture('1.mp4')
name()
while True:
flag,frame=cap.read()
if not flag:
break
face_detect_demo(frame)
if ord(' ') == cv2.waitKey(10):
break
cv2.destroyAllWindows()
cap.release()
#print(names)
效果:根据人脸识别出人物名字id
对于trainer.yml没有存储的人脸会显示unknown
5.网页视频&RTMP协议
RTMP(Real-Time Messaging Protocol实时消息传送协议)的缩写,它是Adobe Systems公司为Flash播放器和服务器之间音频、视频和数据传输开发的协议。这是一个标准的,未加密的实时消息传递协议,默认端口是1935,如果未指定连接端口,那么flash客户端会尝试连接其他端口,其尝试连接顺序按照下列顺序依次连接:1935、443、80(RTMP), 80(RTMPT)。
电视节目rtmp推流地址
import cv2
class CaptureVideo(object):
def net_video(self):
# 获取网络视频流
cam = cv2.VideoCapture("rtmp://58.200.131.2:1935/livetv/cctv5")
while cam.isOpened():
sucess, frame = cam.read()
cv2.imshow("Network", frame)
cv2.waitKey(1)
if __name__ == "__main__":
capture_video = CaptureVideo()
capture_video.net_video()
可用于后续工程项目。
版权声明:本文为博主fangjiayou原创文章,版权归属原作者,如果侵权,请联系我们删除!
原文链接:https://blog.csdn.net/fangjiayou/article/details/123324487