python实现人脸检测

 

安装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:\Data\7.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()



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值