Rohit001 commited on
Commit
a88a0b7
·
1 Parent(s): 8e05bf3

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +58 -0
  2. keras_model.h5 +3 -0
  3. labels.txt +5 -0
  4. requirements.txt +12 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask,render_template
2
+ from flask_socketio import SocketIO,emit
3
+ import base64
4
+ import numpy as np
5
+ import cv2
6
+ import numpy as np
7
+ from keras.models import load_model
8
+
9
+
10
+ app = Flask(__name__)
11
+ app.config['SECRET_KEY'] = 'secret!'
12
+ socket = SocketIO(app,async_mode="eventlet")
13
+
14
+
15
+ # load model and labels
16
+ np.set_printoptions(suppress=True)
17
+ model = load_model("keras_Model.h5", compile=False)
18
+ class_names = open("labels.txt", "r").readlines()
19
+
20
+ def base64_to_image(base64_string):
21
+ # Extract the base64 encoded binary data from the input string
22
+ base64_data = base64_string.split(",")[1]
23
+ # Decode the base64 data to bytes
24
+ image_bytes = base64.b64decode(base64_data)
25
+ # Convert the bytes to numpy array
26
+ image_array = np.frombuffer(image_bytes, dtype=np.uint8)
27
+ # Decode the numpy array as an image using OpenCV
28
+ image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
29
+ return image
30
+
31
+ @socket.on("connect")
32
+ def test_connect():
33
+ print("Connected")
34
+ emit("my response", {"data": "Connected"})
35
+
36
+ @socket.on("image")
37
+ def receive_image(image):
38
+ # Decode the base64-encoded image data
39
+ image = base64_to_image(image)
40
+ image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
41
+ # emit("processed_image", image)
42
+ # Make the image a numpy array and reshape it to the models input shape.
43
+ image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)
44
+ image = (image / 127.5) - 1
45
+ # Predicts the model
46
+ prediction = model.predict(image)
47
+ index = np.argmax(prediction)
48
+ class_name = class_names[index]
49
+ confidence_score = prediction[0][index]
50
+ emit("result",{"name":str(class_name),"score":str(confidence_score)})
51
+
52
+ @app.route("/")
53
+ def home():
54
+ return render_template("index.html")
55
+
56
+ if __name__ == '__main__':
57
+ # app.run(debug=True)
58
+ socket.run(app, debug=True,port=8080,host='0.0.0.0')
keras_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:039edc16f4f5b7d2b775c7d9882a75a27c22f23d5697019cfc3fe7b89bafe176
3
+ size 2456608
labels.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ 0 Tom_Cruise
2
+ 1 Will_Smith
3
+ 2 RDJ
4
+ 3 Rohit
5
+ 4 Jennifer_lawrence
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Flask-SocketIO
2
+ python-engineio
3
+ python-socketio
4
+ Flask
5
+ Werkzeug
6
+ opencv_python
7
+ numpy
8
+ gunicorn
9
+ eventlet
10
+ face-recognition
11
+ dlib
12
+ tensorflow