简体   繁体   English

Python - 从 URL 读取图像然后用于面部识别?

[英]Python - Read image from a URL then use for face_recognition?

I am trying to feed an image from URL to a face_recognition library that I'm using, but it does not seem to be working.我正在尝试将 URL 中的图像提供给我正在使用的face_recognition库,但它似乎不起作用。

I have tried the suggestion here: https://github.com/ageitgey/face_recognition/issues/442 but it did not work for me.我已经尝试过这里的建议: https://github.com/ageitgey/face_recognition/issues/442但它对我不起作用。 I'm thinking that my problem is with the method that I'm using for fetching the image, and not the face_recognition library, that's why I decided to post the question here.我在想我的问题是我用来获取图像的方法,而不是 face_recognition 库,这就是我决定在这里发布问题的原因。

Bellow is my code:下面是我的代码:

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

I'm getting the following response when running the above code:运行上面的代码时,我得到以下响应:

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'

I think the problem is with the line AttributeError: 'JpegImageFile' object has no attribute 'read'我认为问题出在行AttributeError: 'JpegImageFile' object has no attribute 'read'

urllib.request.urlopen(url) returns a http response and not an image file. urllib.request.urlopen(url)返回 http 响应而不是图像文件。 i think you are supposed to download the image and give the path of the files as input to load_image_file().我认为您应该下载图像并将文件的路径作为 load_image_file() 的输入。

You don't need Image to load it您不需要Image来加载它

response = urllib.request.urlopen(url)
image = face_recognition.load_image_file(response)

urlopen() gives object which has methods read() , seek() so it is treated as file-like object . urlopen()给出 object ,它具有方法read()seek()因此它被视为类似file-like object And load_image_file() needs filename or file-like object并且load_image_file()需要filenamefile-like object

Change this:改变这个:

img = Image.open(urllib.request.urlopen(url))
image = face_recognition.load_image_file(img)

To:至:

img = urllib.request.urlopen(url)
image = face_recognition.load_image_file(img)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM