Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion Flask_Apis/Image_recognition_from_File_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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)