Skip to content

Commit f9f13e9

Browse files
Merge pull request #2451 from theshubh007/master
new Api endpoint added which supports url format parameter
2 parents e52dbdb + 8f60e75 commit f9f13e9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Flask_Apis/Image_recognition_from_File_format.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,53 @@ def predictface():
8989

9090
else:
9191
return jsonify({'similarity_percentage': 'Could not detect faces in both images.'})
92+
93+
@app.route('/face_recognize_from_Urls',methods=['POST'])
94+
def predictface():
95+
data=request.json
96+
url1=data['url1']
97+
# get URL of first image from form data
98+
url2 = data['url2']
99+
100+
# Read the first image from URL using requests library
101+
img1 = cv2.imdecode(np.frombuffer(requests.get(url1).content, np.uint8), cv2.IMREAD_COLOR)
102+
103+
# Download the second image from the URL
104+
with urllib.request.urlopen(url2) as url:
105+
s = url.read()
106+
img2 = cv2.imdecode(np.frombuffer(s, np.uint8), cv2.IMREAD_COLOR)
107+
108+
# Convert the images to grayscale
109+
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
110+
gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
111+
112+
# Detect faces in the images
113+
faces1 = face_cascade.detectMultiScale(gray_img1, scaleFactor=1.1, minNeighbors=5)
114+
faces2 = face_cascade.detectMultiScale(gray_img2, scaleFactor=1.1, minNeighbors=5)
115+
116+
# Compare only the first detected face in each image
117+
if len(faces1) > 0 and len(faces2) > 0:
118+
x1, y1, w1, h1 = faces1[0]
119+
x2, y2, w2, h2 = faces2[0]
120+
121+
# Extract the face regions from the images
122+
face1 = gray_img1[y1:y1+h1, x1:x1+w1]
123+
face2 = gray_img2[y2:y2+h2, x2:x2+w2]
124+
125+
# Resize the face regions to the same dimensions
126+
resized_face1 = cv2.resize(face1, (face2.shape[1], face2.shape[0]))
127+
128+
# Calculate the structural similarity index between the face regions
129+
score = ssim(resized_face1, face2, full=True)[0]
130+
131+
# Convert the similarity score to a percentage
132+
similarity_percentage = score * 100
133+
134+
# Return the similarity percentage in a JSON response
135+
return jsonify({'similarity_percentage': similarity_percentage})
136+
137+
else:
138+
return jsonify({'similarity_percentage': 'Could not detect faces in both images.'})
92139

93140

94141
if __name__ == '__main__':

0 commit comments

Comments
 (0)