代码之家  ›  专栏  ›  技术社区  ›  fightstarr20

python dlib-读取图像而不是网络摄像头

  •  -1
  • fightstarr20  · 技术社区  · 6 年前

    我正在使用本文中的这个示例python脚本 enter link description here

    from imutils import face_utils
    import dlib
    import cv2
    
    # Vamos inicializar um detector de faces (HOG) para então
    # let's go code an faces detector(HOG) and after detect the 
    # landmarks on this detected face
    
    # p = our pre-treined model directory, on my case, it's on the same script's diretory.
    p = "shape_predictor_68_face_landmarks.dat"
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(p)
    
    cap = cv2.VideoCapture(0)
    
    while True:
        # Getting out image by webcam 
        _, image = cap.read()
        # Converting the image to gray scale
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
        # Get faces into webcam's image
        rects = detector(gray, 0)
    
        # For each detected face, find the landmark.
        for (i, rect) in enumerate(rects):
            # Make the prediction and transfom it to numpy array
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
    
            # Draw on our image, all the finded cordinate points (x,y) 
            for (x, y) in shape:
                cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
    
        # Show the image
        cv2.imshow("Output", image)
    
        k = cv2.waitKey(5) & 0xFF
        if k == 27:
            break
    
    cv2.destroyAllWindows()
    cap.release()
    

    一切都很好,但我正在尝试修改它以读取图像文件,而不是 cap 网络摄像头流。

    我试过在一个网址上阅读,但它不喜欢,有人建议吗?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Employee Juan Ledesma    6 年前

    似乎你在要求 在OpenCV中读取图像 .

    假设你正在运行 脚本语言 从同一文件夹 文件段 存储,只需键入:

    img = cv2.imread("image.jpg")
    

    当然,因为你只读了一次图像,就不需要再做一个while循环了。

    以下是完整的工作代码:

    from imutils import face_utils
    import dlib
    import cv2
    
    p = "shape_predictor_68_face_landmarks.dat"
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(p)
    
    image = cv2.imread("image.jpg")    
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)    
    rects = detector(gray, 0)
    
    for (i, rect) in enumerate(rects):
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)
        for (x, y) in shape:
            cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
    
    cv2.imshow("Output", image)
    cv2.waitKey(0)
    
    cv2.destroyAllWindows()
    
        2
  •  0
  •   Shivam Chawla    6 年前

    视频基本上是一组图像流,它们的移动速度比我们的眼睛所能检测到的要快。因此,对于您的查询,除了while循环部分,代码几乎保持不变。

    from imutils import face_utils
    import dlib
    import cv2
    
    # Vamos inicializar um detector de faces (HOG) para então
    # let's go code an faces detector(HOG) and after detect the 
    # landmarks on this detected face
    
    # p = our pre-treined model directory, on my case, it's on the same script's diretory.
    p = "shape_predictor_68_face_landmarks.dat"
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(p)
    
    cap = cv2.VideoCapture(0)
    
    #while True:
    # Getting out image by webcam 
    image = #load your image here
    # Converting the image to gray scale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Get faces into webcam's image
    rects = detector(gray, 0)
    
    # For each detected face, find the landmark.
    for (i, rect) in enumerate(rects):
    # Make the prediction and transfom it to numpy array
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)
    
        # Draw on our image, all the finded cordinate points (x,y) 
        for (x, y) in shape:
            cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
    
    # Show the image
    cv2.imshow("Output", image)
    
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break
    
    #cv2.destroyAllWindows()
    #cap.release()
    
    推荐文章