srivatsavdamaraju commited on
Commit
a82c6ba
·
verified ·
1 Parent(s): 0d76610

Create face_recog.py

Browse files
Files changed (1) hide show
  1. face_recog.py +88 -0
face_recog.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import face_recognition
3
+ import cv2
4
+ import numpy as np
5
+ import os
6
+ from pathlib import Path
7
+ import base64
8
+
9
+ # Initialize Flask app
10
+ app = Flask(__name__)
11
+
12
+ # Set the maximum file upload size (50MB)
13
+ app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB
14
+
15
+ # Define the path to dataset and test image
16
+ dataset_folder_path = '/content/sample_data/dataset'
17
+
18
+ # Load and encode faces from the dataset folder
19
+ def load_face_encodings(dataset_folder_path):
20
+ known_face_encodings = []
21
+ known_face_names = []
22
+
23
+ for img_path in Path(dataset_folder_path).glob("*.jpg"):
24
+ image = face_recognition.load_image_file(img_path)
25
+ encodings = face_recognition.face_encodings(image)
26
+
27
+ if encodings:
28
+ known_face_encodings.append(encodings[0])
29
+ known_face_names.append(img_path.stem)
30
+
31
+ return known_face_encodings, known_face_names
32
+
33
+ # Load and process the uploaded test image
34
+ def load_test_image(test_image):
35
+ test_image_rgb = cv2.imdecode(np.fromstring(test_image.read(), np.uint8), cv2.IMREAD_COLOR)
36
+ test_image_rgb = cv2.cvtColor(test_image_rgb, cv2.COLOR_BGR2RGB)
37
+ face_locations = face_recognition.face_locations(test_image_rgb)
38
+ face_encodings = face_recognition.face_encodings(test_image_rgb, face_locations)
39
+ return test_image_rgb, face_locations, face_encodings
40
+
41
+ # Compare faces from the test image with the dataset faces
42
+ def compare_faces(known_face_encodings, known_face_names, test_image_encodings):
43
+ matches = []
44
+ for test_encoding in test_image_encodings:
45
+ results = face_recognition.compare_faces(known_face_encodings, test_encoding)
46
+ if True in results:
47
+ first_match_index = results.index(True)
48
+ matches.append(known_face_names[first_match_index])
49
+ else:
50
+ matches.append("Unknown")
51
+ return matches
52
+
53
+ # Convert image to base64
54
+ def convert_image_to_base64(image):
55
+ _, buffer = cv2.imencode('.jpg', image)
56
+ img_bytes = buffer.tobytes()
57
+ img_base64 = base64.b64encode(img_bytes).decode('utf-8')
58
+ return img_base64
59
+
60
+ # Flask route for face recognition
61
+ @app.route('/recognize_faces', methods=['POST'])
62
+ def recognize_faces():
63
+ if 'image' not in request.files:
64
+ return jsonify({"error": "No image file provided!"}), 400
65
+
66
+ # Load known face encodings
67
+ known_face_encodings, known_face_names = load_face_encodings(dataset_folder_path)
68
+
69
+ image_file = request.files['image']
70
+ test_image_rgb, face_locations, test_image_encodings = load_test_image(image_file)
71
+
72
+ matches = compare_faces(known_face_encodings, known_face_names, test_image_encodings)
73
+
74
+ for (top, right, bottom, left), name in zip(face_locations, matches):
75
+ cv2.rectangle(test_image_rgb, (left, top), (right, bottom), (0, 0, 255), 2)
76
+ font = cv2.FONT_HERSHEY_DUPLEX
77
+ cv2.putText(test_image_rgb, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
78
+
79
+ result_image_base64 = convert_image_to_base64(test_image_rgb)
80
+ return jsonify({'recognized_faces': matches, 'image': result_image_base64})
81
+
82
+ @app.route('/')
83
+ def hello_world():
84
+ return 'Welcome to the Face Recognition API! Upload an image for recognition.'
85
+
86
+ # Run Flask app
87
+ if __name__ == '__main__':
88
+ app.run()