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

Python dlib - Read image instead of webcam

我正在使用文章 enter link description here

中的这个示例 Python 脚本
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 网络摄像头流。

我试过在 URL 中阅读,但不喜欢,有人有什么建议吗?

看来您要的是 在 OpenCV 中读取图像的标准方法

假设您 运行 您的 script.py 来自 image.jpg 所在的同一文件夹已存储,只需键入:

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()

视频基本上是图片流,其移动速度快于我们的眼睛检测不到的速度。因此,对于您的查询,除了 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()