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

时间:2019-01-19 15:30:41

标签: python opencv dlib

我正在使用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,但是不喜欢它,有人有任何建议吗?

2 个答案:

答案 0 :(得分:2)

您似乎正在要求在OpenCV中读取图像的标准方法

假设您从存储 image.jpg 的同一文件夹中运行 script.py ,只需键入:

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

答案 1 :(得分:0)

基本上,视频是一连串的图片,其移动速度比我们的眼睛所能察觉的快。因此,对于您的查询,除了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()