Python图像识别-电脑默认摄像头

2022-12-10  本文已影响0人  ssttIsme

检查下haarcascade_frontalface_default.xml的目录,如果像我直接在D盘安装opencv就是下面这种路径

# 导入cv模块
import cv2 as cv


def face_detect_demo(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    face_detect = cv.CascadeClassifier('D:/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
    face = face_detect.detectMultiScale(gray)
    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 = cv.VideoCapture(0)

cap.read()

# 等待
while True:
    flag, frame = cap.read()
    if not flag:
        break
    face_detect_demo(frame)
    if ord('q') == cv.waitKey(0):
        break
# 释放内存
cv.destroyAllWindows()
# 释放摄像头
cap.release()

q键退出

上一篇下一篇

猜你喜欢

热点阅读