Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

web_service_example.py 5.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  1. # This is a _very simple_ example of a web service that recognizes faces in uploaded images.
  2. # Upload an image file and it will check if the image contains a picture of Barack Obama.
  3. # The result is returned as json. For example:
  4. #
  5. # $ curl -XPOST -F "file=@obama2.jpg" http://127.0.0.1:5001
  6. #
  7. # Returns:
  8. #
  9. # {
  10. # "face_found_in_image": true,
  11. # "is_picture_of_obama": true
  12. # }
  13. #
  14. # This example is based on the Flask file upload example: http://flask.pocoo.org/docs/0.12/patterns/fileuploads/
  15. # NOTE: This example requires flask to be installed! You can install it with pip:
  16. # $ pip3 install flask
  17. import face_recognition
  18. from flask import Flask, jsonify, request, redirect
  19. # You can change this to any folder on your system
  20. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
  21. app = Flask(__name__)
  22. def allowed_file(filename):
  23. return '.' in filename and \
  24. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  25. @app.route('/', methods=['GET', 'POST'])
  26. def upload_image():
  27. # Check if a valid image file was uploaded
  28. if request.method == 'POST':
  29. if 'file' not in request.files:
  30. return redirect(request.url)
  31. file = request.files['file']
  32. if file.filename == '':
  33. return redirect(request.url)
  34. if file and allowed_file(file.filename):
  35. # The image file seems valid! Detect faces and return the result.
  36. return detect_faces_in_image(file)
  37. # If no valid image file was uploaded, show the file upload form:
  38. return '''
  39. <!doctype html>
  40. <title>Is this a picture of Obama?</title>
  41. <h1>Upload a picture and see if it's a picture of Obama!</h1>
  42. <form method="POST" enctype="multipart/form-data">
  43. <input type="file" name="file">
  44. <input type="submit" value="Upload">
  45. </form>
  46. '''
  47. def detect_faces_in_image(file_stream):
  48. # Pre-calculated face encoding of Obama generated with face_recognition.face_encodings(img)
  49. known_face_encoding = [-0.09634063, 0.12095481, -0.00436332, -0.07643753, 0.0080383,
  50. 0.01902981, -0.07184699, -0.09383309, 0.18518871, -0.09588896,
  51. 0.23951106, 0.0986533 , -0.22114635, -0.1363683 , 0.04405268,
  52. 0.11574756, -0.19899382, -0.09597053, -0.11969153, -0.12277931,
  53. 0.03416885, -0.00267565, 0.09203379, 0.04713435, -0.12731361,
  54. -0.35371891, -0.0503444 , -0.17841317, -0.00310897, -0.09844551,
  55. -0.06910533, -0.00503746, -0.18466514, -0.09851682, 0.02903969,
  56. -0.02174894, 0.02261871, 0.0032102 , 0.20312519, 0.02999607,
  57. -0.11646006, 0.09432904, 0.02774341, 0.22102901, 0.26725179,
  58. 0.06896867, -0.00490024, -0.09441824, 0.11115381, -0.22592428,
  59. 0.06230862, 0.16559327, 0.06232892, 0.03458837, 0.09459756,
  60. -0.18777156, 0.00654241, 0.08582542, -0.13578284, 0.0150229 ,
  61. 0.00670836, -0.08195844, -0.04346499, 0.03347827, 0.20310158,
  62. 0.09987706, -0.12370517, -0.06683611, 0.12704916, -0.02160804,
  63. 0.00984683, 0.00766284, -0.18980607, -0.19641446, -0.22800779,
  64. 0.09010898, 0.39178532, 0.18818057, -0.20875394, 0.03097027,
  65. -0.21300618, 0.02532415, 0.07938635, 0.01000703, -0.07719778,
  66. -0.12651891, -0.04318593, 0.06219772, 0.09163868, 0.05039065,
  67. -0.04922386, 0.21839413, -0.02394437, 0.06173781, 0.0292527 ,
  68. 0.06160797, -0.15553983, -0.02440624, -0.17509389, -0.0630486 ,
  69. 0.01428208, -0.03637431, 0.03971229, 0.13983178, -0.23006812,
  70. 0.04999552, 0.0108454 , -0.03970895, 0.02501768, 0.08157793,
  71. -0.03224047, -0.04502571, 0.0556995 , -0.24374914, 0.25514284,
  72. 0.24795187, 0.04060191, 0.17597422, 0.07966681, 0.01920104,
  73. -0.01194376, -0.02300822, -0.17204897, -0.0596558 , 0.05307484,
  74. 0.07417042, 0.07126575, 0.00209804]
  75. # Load the uploaded image file
  76. img = face_recognition.load_image_file(file_stream)
  77. # Get face encodings for any faces in the uploaded image
  78. unknown_face_encodings = face_recognition.face_encodings(img)
  79. face_found = False
  80. is_obama = False
  81. if len(unknown_face_encodings) > 0:
  82. face_found = True
  83. # See if the first face in the uploaded image matches the known face of Obama
  84. match_results = face_recognition.compare_faces([known_face_encoding], unknown_face_encodings[0])
  85. if match_results[0]:
  86. is_obama = True
  87. # Return the result as json
  88. result = {
  89. "face_found_in_image": face_found,
  90. "is_picture_of_obama": is_obama
  91. }
  92. return jsonify(result)
  93. if __name__ == "__main__":
  94. app.run(host='0.0.0.0', port=5001, debug=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...