概述
安装PyFaceDet库:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple PyFaceDet==0.2.0
安装opencv-python:
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
from PyFaceDet import facedetectcnn
import cv2
path = r'hezhao4.jpg'
img = cv2.imread(path)
faces = facedetectcnn.facedetect_cnn(path)
print(faces)
for face in faces:
x = face[0] # 横坐标
y = face[1] # 纵坐标
l = face[2] # 长度
w = face[3] # 宽度
confidence = face[4] # 置信度
angle = face[5] # 角度
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.rectangle(img, (x, y), (x + l, y + w), (255, 0, 0), 2)
roi_color = img[y:y + w, x:x + l]
cv2.putText(img, str(confidence), (x + 5, y - 5), font, 1, (0, 0, 255), 1)
# img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
cv2.imshow('camera', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
from PyFaceDet import facedetectcnn
import cv2
video_capture = cv2.VideoCapture(r"D:Data7.MP4") # 读视频
# video_capture = cv2.VideoCapture(0) # 捕获摄像头
video_capture.set(cv2.CAP_PROP_POS_FRAMES, 3000)
def img_resize(image):
height, width = image.shape[0], image.shape[1]
# 设置新的图片分辨率框架
width_new = 640
height_new = 480
# 判断图片的长宽比率
if width / height >= width_new / height_new:
img_new = cv2.resize(image, (width_new, int(height * width_new / width)))
else:
img_new = cv2.resize(image, (int(width * height_new / height), height_new))
return img_new
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
frame = img_resize(frame)
print(frame.shape)
# Resize frame of video to 1/4 size for faster face recognition processing
# small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
faces = facedetectcnn.facedetect_cnn(frame)
for face in faces:
x = face[0] # 横坐标
y = face[1] # 纵坐标
l = face[2] # 长度
w = face[3] # 宽度
confidence = face[4] # 置信度
angle = face[5] # 角度
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.rectangle(frame, (x, y), (x + l, y + w), (255, 0, 0), 2)
roi_color = frame[y:y + w, x:x + l]
cv2.putText(frame, str(confidence), (x + 5, y - 5), font, 1, (0, 0, 255), 1)
cv2.imshow('camera', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
最后
以上就是无限招牌为你收集整理的python实现人脸检测的全部内容,希望文章能够帮你解决python实现人脸检测所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复