当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Python下应用opencv 实现人脸检测功能(2)

    栏目:Linux/apache问题 时间:2019-11-08 10:44

    最后串接所有代码如下:

    # USAGE 使用方法是:
    # python detect_faces.py --image images/family.jpg \
    # --detector haarcascade_frontalface_default.xml
    # import the necessary packages 输入包
    # import imutils 如果需要成比例缩放图形才需要,这里不需要
    import argparse
    import cv2
    # construct the argument parser and parse the arguments //构造命令行参数分析
    # 为了集成测试,或者命令行输入的简单,这里都有缺省参数
    #image 是 images/family.jpg
    #detector 是 haarcascade_frontalface_default.xml
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", default='images/family.jpg',
     help="path to the input image")
    ap.add_argument("-d", "--detector", default='haarcascade_frontalface_default.xml',
     help="path to Haar cacscade face detector")
    args = vars(ap.parse_args())
    # load our image and convert it to grayscale 导入图形文件,并灰度化
    image = cv2.imread(args["image"])
    #image =imutils.resize(image,width=800)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # load the face detector and detect faces in the image
    # 导入脸部检测文件
    detector = cv2.CascadeClassifier(args["detector"])
    #检测图形中的脸部
    rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
     minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
    #显示检测到的人脸数目
    print("[INFO] detected {} faces".format(len(rects)))
    # loop over the bounding boxes and draw a rectangle around each face
    # 循环rects,绘图每个检测到的人脸框
    for (x, y, w, h) in rects:
     cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    # show the detected faces
    cv2.imshow("Faces", image)
    cv2.waitKey(0)

    总结

    以上所述是小编给大家介绍的Python下应用opencv 实现人脸检测功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对IIS7站长之家网站的支持!
    如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!