0

I have a Python(2.7) script which detects faces in an image which works fine.

from PIL import Image
import face_recognition


# Load the jpg file into a numpy array
image = face_recognition.load_image_file("/PATH_TO_IMAGE/IMAGE.jpg")

# Find all the faces in the image
face_locations = face_recognition.face_locations(image)

# a = print("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()

Now, I want to make it as a http://localhost:8080/detect so that user can provide an image and clicking on Submit button will display the output. I think there are few ways to do this, either using Flask, Django or Web server. But not sure which is the simplest way to do this and how to go about it. How to take this code and convert into a webservice? What code changes are required? Any code sample will be of great help.

tech_a_break
  • 173
  • 3
  • 11

1 Answers1

1

I believe you're asking what a good web framework for python is. Before I was competent, I wrote a Django web server and I found it a bit daunting. But then everything was daunting at that point. I've used Flask in few projects since becoming an OK programmer. It's fairly straight forward, easy to setup, and well documented, as is Django. I used this tutorial when I was learning Flask. As I understand it, this is really an opinion question, so IMHO I would go with Flask, uWSGI, and Nginx as is in the tutorial. Though, I would encourage you to do your own research and see if the other options suit you and your project better.

EDIT: To use this code Flask you either place your code directly in a route's function or even better put it into a function or class and import it. EVEN BETTER create a model, view, and controller(MVC). From a cursory glance, it looks like you'll have to upload the file and save it, and then open it with face_recognition. The face_recognition module doesn't seem to be able to read data from memory, so saving the file and opening it with face_recognition.load_image_file("/PATH_TO_IMAGE/IMAGE.jpg") seems to be necessary. Just to be sure to delete the file after you load it to clear space. Then once you have your PIL.Image object, you would return that from your code and then send it with the flask.send_file function as demonstrated here. Lots of links and no code, but these links along with the tutorial I linked to initially should get you where you need to go.

Note: In case it wasn't clear, your code should return Image.fromarray(face_image) and not use the show function.

Community
  • 1
  • 1
Allie Fitter
  • 1,153
  • 8
  • 17
  • Thanks @allie-fitter. The question is how do I take this code and make it as a webservice? I don't have much experience with creating a web service. Not looking for pros and cons of web frameworks. Hope this clarifies. – tech_a_break Mar 29 '17 at 22:46