Spaces:
Build error
Build error
Upload 13 files
Browse files- .gitattributes +1 -0
- app.py +67 -0
- facenet_weights/.DS_Store +0 -0
- facenet_weights/20180402-114759/20180402-114759-weights.h5 +3 -0
- gradio/.DS_Store +0 -0
- gradio/demo.py +27 -0
- gradio/examples/.DS_Store +0 -0
- gradio/examples/Human.jpg +0 -0
- gradio/examples/NotHuman.jpg +0 -0
- model/.DS_Store +0 -0
- model/clf_model.sav +3 -0
- requirements.txt +8 -0
- run.sh +4 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
model/clf_model.sav filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
sys.path.append('.')
|
3 |
+
|
4 |
+
from flask import Flask, request, jsonify
|
5 |
+
from time import gmtime, strftime
|
6 |
+
import os
|
7 |
+
import base64
|
8 |
+
import json
|
9 |
+
import cv2
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
from tensorflow.keras.preprocessing import image
|
13 |
+
from keras_facenet import FaceNet
|
14 |
+
import pickle
|
15 |
+
|
16 |
+
# create a facenet model
|
17 |
+
embedding_model = FaceNet(key = '20180402-114759',
|
18 |
+
use_prebuilt=True,
|
19 |
+
cache_folder= os.path.abspath(os.path.dirname(__file__)) + '/facenet_weights').model
|
20 |
+
|
21 |
+
clf_model = pickle.load(open(os.path.abspath(os.path.dirname(__file__)) + '/model/clf_mode.sav', 'rb'))
|
22 |
+
|
23 |
+
target_shape = (160, 160)
|
24 |
+
|
25 |
+
app = Flask(__name__)
|
26 |
+
|
27 |
+
app.config['SITE'] = "http://0.0.0.0:8000/"
|
28 |
+
app.config['DEBUG'] = False
|
29 |
+
|
30 |
+
@app.route('/api/detect_human_face', methods=['POST'])
|
31 |
+
def detect_human_face():
|
32 |
+
file1 = request.files['image1']
|
33 |
+
image1 = cv2.imdecode(np.fromstring(file1.read(), np.uint8), cv2.IMREAD_COLOR)
|
34 |
+
if image1 is None:
|
35 |
+
result = "image1: is null!"
|
36 |
+
status = "ok"
|
37 |
+
response = jsonify({"status": status, "data": {"result": result}})
|
38 |
+
response.status_code = 200
|
39 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
40 |
+
return response
|
41 |
+
|
42 |
+
X = np.float32([(np.float32(image1) - 127.5) / 127.5])
|
43 |
+
X_ft = embedding_model.predict(X, batch_size=1)
|
44 |
+
anomaly_score = clf_model.decision_function(X_ft) * -1
|
45 |
+
|
46 |
+
if anomaly_score > 1:
|
47 |
+
result = "Not Human"
|
48 |
+
else:
|
49 |
+
result = "Human"
|
50 |
+
|
51 |
+
status = "ok"
|
52 |
+
response = jsonify(
|
53 |
+
{
|
54 |
+
"status": status,
|
55 |
+
"data": {
|
56 |
+
"result": result,
|
57 |
+
"anomaly_score": float(anomaly_score)
|
58 |
+
}
|
59 |
+
})
|
60 |
+
|
61 |
+
response.status_code = 200
|
62 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
63 |
+
return response
|
64 |
+
|
65 |
+
if __name__ == '__main__':
|
66 |
+
port = int(os.environ.get("PORT", 8000))
|
67 |
+
app.run(host='0.0.0.0', port=port)
|
facenet_weights/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
facenet_weights/20180402-114759/20180402-114759-weights.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b71e7045497e841c00ee568f031d1a4d30908fceadf6884aef2dec4d545202b
|
3 |
+
size 94952520
|
gradio/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
gradio/demo.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
def detect_human_face(frame1):
|
7 |
+
url = "http://127.0.0.1:8000/api/detect_human_face"
|
8 |
+
files = {'image1': open(frame1, 'rb')}
|
9 |
+
|
10 |
+
r = requests.post(url=url, files=files)
|
11 |
+
|
12 |
+
return [r.json()]
|
13 |
+
|
14 |
+
with gr.Blocks() as demo:
|
15 |
+
|
16 |
+
with gr.Row():
|
17 |
+
with gr.Column():
|
18 |
+
face_input = gr.Image(type='filepath', height=480)
|
19 |
+
gr.Examples(['gradio/examples/Human.jpg', 'gradio/examples/NotHuman.jpg'],
|
20 |
+
inputs=face_input)
|
21 |
+
detect_human_face_button = gr.Button("Detect Human Face")
|
22 |
+
with gr.Column():
|
23 |
+
detect_result_output = gr.JSON(label='Result')
|
24 |
+
|
25 |
+
detect_human_face_button.click(detect_human_face, inputs=[face_input], outputs=[detect_result_output])
|
26 |
+
|
27 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
gradio/examples/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
gradio/examples/Human.jpg
ADDED
![]() |
gradio/examples/NotHuman.jpg
ADDED
![]() |
model/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
model/clf_model.sav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:70ce774d1a9c411caf3d932bab867278df8e5e31b7753755dc3211e665086aae
|
3 |
+
size 8450201
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
flask-cors
|
3 |
+
gradio
|
4 |
+
opencv-python
|
5 |
+
numpy==1.20.3
|
6 |
+
pillow
|
7 |
+
pickle
|
8 |
+
keras-facenet
|
run.sh
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
exec python3 app.py &
|
4 |
+
exec python3 gradio/demo.py
|