Python - читать изображение с URL, а затем использовать для face_recognition? - PullRequest
0 голосов
/ 26 сентября 2019

Я пытаюсь передать изображение с URL-адреса в библиотеку face_recognition , которую я использую, но, похоже, она не работает.

Я попробовал предложение здесь:https://github.com/ageitgey/face_recognition/issues/442 но у меня это не сработало.Я думаю, что моя проблема связана с методом, который я использую для получения изображения, а не с библиотекой face_recognition, поэтому я решил опубликовать вопрос здесь.

Ниже - мой код:

from PIL import Image
import face_recognition
import urllib.request

url = "https://carlofontanos.com/wp-content/themes/carlo-fontanos/img/carlofontanos.jpg"
img = Image.open(urllib.request.urlopen(url))
image = face_recognition.load_image_file(img)

# Find all the faces in the image using the default HOG-based model.
face_locations = face_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:

    # Print the location of each face in this image
    top, right, bottom, left = face_location
    print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    # You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()

Я получаю следующий ответ при запуске приведенного выше кода:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    image = face_recognition.load_image_file(img)
  File "C:\Users\Carl\AppData\Local\Programs\Python\Python37-32\lib\site-packages\face_recognition\api.py", line 83, in load_image_file
    im = PIL.Image.open(file)
  File "C:\Users\Carl\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py", line 2643, in open
    prefix = fp.read(16)
AttributeError: 'JpegImageFile' object has no attribute 'read'

Я думаю, что проблема со строкойAttributeError: 'JpegImageFile' object has no attribute 'read'

1 Ответ

0 голосов
/ 26 сентября 2019

urllib.request.urlopen(url) возвращает HTTP-ответ, а не файл изображения.я думаю, что вы должны загрузить изображение и указать путь к файлам в качестве входных данных для load_image_file ().

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...