-1

I am working with this example Python script from the article 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()

All works great, but I am trying to modify it to read an image file instead of grab the cap webcam stream.

I have tried reading in a URL instead but it is not liking it, anyone any suggestions?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

2 Answers2

2

It seems you're asking for the standard way of reading images in OpenCV.

Assuming you're running your script.py from the same folder where image.jpg is stored, simply type:

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

Of course, since you're reading the image only once, there's no need to have a while loop anymore.

Here below the full working code:

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()
Employee
  • 3,109
  • 5
  • 31
  • 50
0

A video is basically a stream of pictures which move faster than our eyes can detect. So for your query, the code remains pretty much the same except the while loop part.

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()
Shivam Chawla
  • 390
  • 5
  • 17
  • This does not provide an answer to the question. Plus it does not clarify to the OP how to actually read the image. – Employee Jan 19 '19 at 16:02