Face_recognition: Detecting faces in an image read out of a unix socket

Created on 11 Sep 2017  路  2Comments  路  Source: ageitgey/face_recognition

  • face_recognition version: 1.0.0
  • Python version: 3.5.2
  • Operating System: Ubuntu 16.04.3

Description

I'm trying to detect faces in an image received from a unix socket

What I Did

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect('/path/to/socket')
data = False

while True:
    buf = sock.recv(4096)

    if not buf:
        break

    if not data:
        data = buf
    else:
        data = data + buf

face_locations = face_recognition.face_locations(data)
face_encodings = face_recognition.face_encodings(data, face_locations)

But I get the error Expected writable numpy.ndarray with shape set.

What do I need to do to get it to work?

Most helpful comment

I forgot to load the image first. I tried it like so:

image = face_recognition.load_image_file(data)

But that gave an embedded null byte error. I then wrapped the data variable in a BytesIO call, and that did it

from io import BytesIO

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect('/path/to/socket')
data = False

while True:
    buf = sock.recv(4096)

    if not buf:
        break

    if not data:
        data = buf
    else:
        data = data + buf

image = face_recognition.load_image_file(BytesIO(data))
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)

All 2 comments

I forgot to load the image first. I tried it like so:

image = face_recognition.load_image_file(data)

But that gave an embedded null byte error. I then wrapped the data variable in a BytesIO call, and that did it

from io import BytesIO

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect('/path/to/socket')
data = False

while True:
    buf = sock.recv(4096)

    if not buf:
        break

    if not data:
        data = buf
    else:
        data = data + buf

image = face_recognition.load_image_file(BytesIO(data))
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)

Great, thanks for sharing the answer. This should be helpful to anyone trying to figure this out later.

Was this page helpful?
0 / 5 - 0 ratings