From 4b0ee9c6efd8c8b4a26a912654554b3a080df2bb Mon Sep 17 00:00:00 2001 From: shubham Date: Fri, 7 Jul 2023 09:20:39 +0530 Subject: [PATCH] face_recognition_api_Added --- .../Image_recognition_from_File_format.py | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Flask_Apis/Image_recognition_from_File_format.py b/Flask_Apis/Image_recognition_from_File_format.py index 961536c626..6bda186205 100644 --- a/Flask_Apis/Image_recognition_from_File_format.py +++ b/Flask_Apis/Image_recognition_from_File_format.py @@ -7,11 +7,13 @@ app = Flask(__name__) +face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") + @app.route('/') def home(): return jsonify({'message':'Welcome to Flask Apis'}) -@app.route('/img',methods=['POST']) +@app.route('/image_Compare',methods=['POST']) def predict(): file1 = request.files['file1'] file2 = request.files['file2'] @@ -37,6 +39,47 @@ def predict(): # Return the similarity percentage in a JSON response return jsonify({'similarity_percentage': similarity_percentage}) +@app.route('/face_recognize',methods=['POST']) +def predictface(): + # Get the uploaded files from the request + file1 = request.files['file1'] + file2 = request.files['file2'] + + # Read the images using OpenCV directly from the request files + img1 = cv2.imdecode(np.frombuffer(file1.read(), np.uint8), cv2.IMREAD_COLOR) + img2 = cv2.imdecode(np.frombuffer(file2.read(), np.uint8), cv2.IMREAD_COLOR) + + # Convert the images to grayscale + gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) + gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) + + # Detect faces in the images + faces1 = face_cascade.detectMultiScale(gray_img1, scaleFactor=1.1, minNeighbors=5) + faces2 = face_cascade.detectMultiScale(gray_img2, scaleFactor=1.1, minNeighbors=5) + + # Compare only the first detected face in each image + if len(faces1) > 0 and len(faces2) > 0: + x1, y1, w1, h1 = faces1[0] + x2, y2, w2, h2 = faces2[0] + + # Extract the face regions from the images + face1 = gray_img1[y1:y1+h1, x1:x1+w1] + face2 = gray_img2[y2:y2+h2, x2:x2+w2] + + # Resize the face regions to the same dimensions + resized_face1 = cv2.resize(face1, (face2.shape[1], face2.shape[0])) + + # Calculate the structural similarity index between the face regions + score = ssim(resized_face1, face2, full=True)[0] + + # Convert the similarity score to a percentage + similarity_percentage = score * 100 + + # Return the similarity percentage in a JSON response + return jsonify({'similarity_percentage': similarity_percentage}) + + else: + return jsonify({'similarity_percentage': 'Could not detect faces in both images.'}) if __name__ == '__main__': app.run(debug=True) \ No newline at end of file