Skip to content

Instantly share code, notes, and snippets.

@abhishekmishragithub
Created August 22, 2019 08:43
Show Gist options
  • Save abhishekmishragithub/a1a761590bc7fda1d7ad0f5442b9155d to your computer and use it in GitHub Desktop.
Save abhishekmishragithub/a1a761590bc7fda1d7ad0f5442b9155d to your computer and use it in GitHub Desktop.
Basic Flask API for face detection
from PIL import Image
import face_recognition
import cv2
import sys
import time
from flask import Flask, request, redirect, url_for, flash, jsonify
import logging
logging.basicConfig(filename="app.log",
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
app = Flask(__name__)
@app.route('/face',methods=['POST'])
def det_face():
'''
Function to identify number of faces in image
'''
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file attached')
logging.warning("no file attached")
return redirect(request.url)
else:
img_file = request.files['file']
# img_file = flask.request.files.get('imagefile', '')
# print(img_file)
logging.info(img_file)
image = face_recognition.load_image_file(img_file)
face_locations = face_recognition.face_locations(image, model="cnn")
output_msg = "I found {} face(s) in this photograph. \n".format(len(face_locations))
if len(face_locations) > 1 or len(face_locations) < 1 :
# sys.stdout.write(output_msg)
# print("Please Try again !")
logging.info("I found {} face(s) in this photograph. \n".format(len(face_locations)))
return jsonify(status=0,output=output_msg)
else:
# print("success !!")
logging.info("sucess!")
return jsonify(status=1,output=output_msg)
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment