Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,68 @@
|
|
1 |
from flask import Flask,render_template
|
2 |
import face_recognition
|
3 |
import sqlite3
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
app = Flask (__name__ )
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
@app.route ("/" )
|
9 |
-
def
|
10 |
return render_template ('index.html')
|
11 |
|
12 |
@app.route ("/storedata" , methods =[ 'GET' ] )
|
@@ -41,5 +97,8 @@ def datafetch():
|
|
41 |
con.close ()
|
42 |
return render_template ('data.html', data = rows)
|
43 |
|
|
|
|
|
|
|
44 |
if __name__ == '__main__' :
|
45 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
from flask import Flask,render_template
|
2 |
import face_recognition
|
3 |
import sqlite3
|
4 |
+
|
5 |
+
#################
|
6 |
+
from flask_socketio import SocketIO,emit
|
7 |
+
import base64
|
8 |
+
import numpy as np
|
9 |
+
import cv2
|
10 |
+
import numpy as np
|
11 |
+
from keras.models import load_model
|
12 |
+
##################
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
app = Flask (__name__ )
|
17 |
|
18 |
+
#################
|
19 |
+
app.config['SECRET_KEY'] = 'secret!'
|
20 |
+
socket = SocketIO(app,async_mode="eventlet")
|
21 |
+
#######################
|
22 |
+
|
23 |
+
|
24 |
+
######################
|
25 |
|
26 |
+
# load model and labels
|
27 |
+
np.set_printoptions(suppress=True)
|
28 |
+
model = load_model(r"keras_model.h5", compile=False)
|
29 |
+
class_names = open(r"labels.txt", "r").readlines()
|
30 |
+
|
31 |
+
def base64_to_image(base64_string):
|
32 |
+
# Extract the base64 encoded binary data from the input string
|
33 |
+
base64_data = base64_string.split(",")[1]
|
34 |
+
# Decode the base64 data to bytes
|
35 |
+
image_bytes = base64.b64decode(base64_data)
|
36 |
+
# Convert the bytes to numpy array
|
37 |
+
image_array = np.frombuffer(image_bytes, dtype=np.uint8)
|
38 |
+
# Decode the numpy array as an image using OpenCV
|
39 |
+
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
|
40 |
+
return image
|
41 |
+
|
42 |
+
@socket.on("connect")
|
43 |
+
def test_connect():
|
44 |
+
print("Connected")
|
45 |
+
emit("my response", {"data": "Connected"})
|
46 |
+
|
47 |
+
@socket.on("image")
|
48 |
+
def receive_image(image):
|
49 |
+
# Decode the base64-encoded image data
|
50 |
+
image = base64_to_image(image)
|
51 |
+
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
|
52 |
+
# emit("processed_image", image)
|
53 |
+
# Make the image a numpy array and reshape it to the models input shape.
|
54 |
+
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)
|
55 |
+
image = (image / 127.5) - 1
|
56 |
+
# Predicts the model
|
57 |
+
prediction = model.predict(image)
|
58 |
+
index = np.argmax(prediction)
|
59 |
+
class_name = class_names[index]
|
60 |
+
confidence_score = prediction[0][index]
|
61 |
+
emit("result",{"name":str(class_name),"score":str(confidence_score)})
|
62 |
+
#######################
|
63 |
+
|
64 |
@app.route ("/" )
|
65 |
+
def home():
|
66 |
return render_template ('index.html')
|
67 |
|
68 |
@app.route ("/storedata" , methods =[ 'GET' ] )
|
|
|
97 |
con.close ()
|
98 |
return render_template ('data.html', data = rows)
|
99 |
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
if __name__ == '__main__' :
|
104 |
+
app.run(app,host="0.0.0.0", port=7860)
|