Skip to content

Commit 8f60e75

Browse files
committed
urlformat added
1 parent 4b0ee9c commit 8f60e75

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
@@ -80,6 +80,53 @@ def predictface():
8080

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

84131
if __name__ == '__main__':
85132
app.run(debug=True)

0 commit comments

Comments
 (0)