tahirsiddique commited on
Commit
2f10d08
·
1 Parent(s): dc5c209

Update model/face_verification_script.py

Browse files
Files changed (1) hide show
  1. model/face_verification_script.py +33 -34
model/face_verification_script.py CHANGED
@@ -1,37 +1,36 @@
1
  from deepface import DeepFace
2
- from flask import Flask, request, jsonify
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- app = Flask(__name__)
5
-
6
- def compare_images(image1_path, image2_path):
7
- result = DeepFace.verify(image1_path, image2_path, model_name='VGG-Face', distance_metric='euclidean_l2')
8
- return result['distance']
9
-
10
- @app.route('/predict', methods=['POST'])
11
- def predict():
12
- if 'file1' not in request.files or 'file2' not in request.files:
13
- return jsonify({'error': 'Please provide two image files.'}), 400
14
-
15
- file1 = request.files['file1']
16
- file2 = request.files['file2']
17
-
18
- # Save uploaded files
19
- file1_path = 'temp/file1.jpg'
20
- file2_path = 'temp/file2.jpg'
21
- file1.save(file1_path)
22
- file2.save(file2_path)
23
-
24
- # Compare images
25
- distance = compare_images(file1_path, file2_path)
26
-
27
- # You can set a threshold to decide if the faces are considered similar or not
28
- threshold = 0.6 # Adjust as needed
29
- if distance < threshold:
30
- result = {'prediction': 'Similar faces', 'distance': distance}
31
  else:
32
- result = {'prediction': 'Dissimilar faces', 'distance': distance}
33
-
34
- return jsonify(result)
35
-
36
- if __name__ == "__main__":
37
- app.run(host='0.0.0.0', port=5000)
 
1
  from deepface import DeepFace
2
+ from PIL import Image
3
+ import requests
4
+ import tempfile
5
+ import os
6
+
7
+ def verify_faces(img1_url, img2_url):
8
+ # Download the images from the URLs
9
+ response1 = requests.get(img1_url)
10
+ response2 = requests.get(img2_url)
11
+
12
+ # Check if the requests were successful
13
+ if response1.status_code == 200 and response2.status_code == 200:
14
+ # Create temporary files to store the downloaded images
15
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as img1_tempfile:
16
+ img1_tempfile.write(response1.content)
17
+ img1_path = img1_tempfile.name
18
+
19
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as img2_tempfile:
20
+ img2_tempfile.write(response2.content)
21
+ img2_path = img2_tempfile.name
22
+
23
+ # Perform facial recognition with deep_face
24
+ result = DeepFace.verify(img1_path=img1_path, img2_path=img2_path, model_name='VGG-Face')
25
+
26
+ # Delete the temporary files when done
27
+ os.remove(img1_path)
28
+ os.remove(img2_path)
29
+
30
+ if result["verified"]:
31
+ return "RESULT: Faces Matched!"
32
+ else:
33
+ return "RESULT: Faces Don't Match"
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  else:
36
+ return "Failed to download one or both of the images from the URLs."