File size: 4,523 Bytes
7199111
 
8ecf185
7199111
 
 
 
 
8ecf185
3fac891
4108613
 
 
8ecf185
4108613
f58a881
 
4108613
 
 
 
 
088b445
 
 
4108613
088b445
4108613
f58a881
 
4108613
 
 
 
 
 
 
 
 
f58a881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3657998
f58a881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4108613
9eb9696
4108613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
088b445
 
4108613
 
 
 
088b445
 
4108613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ecf185
9d9428d
bbcce29
 
4108613
 
9eb9696
bbcce29
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from flask import *
from PIL import Image

import face_recognition
import cv2
import numpy as np
import csv
from datetime import datetime

############################################
from flask import Flask,render_template
import face_recognition
import sqlite3

#################
from flask_socketio import SocketIO,emit
import base64
import numpy as np
import cv2 
import numpy as np
from keras.models import load_model
##################



app = Flask (__name__ )

#################
app.config['SECRET_KEY'] = 'secret!'
socket = SocketIO(app,async_mode="eventlet")
#######################


######################

# load model and labels
np.set_printoptions(suppress=True)
model = load_model(r"keras_model.h5", compile=False)
class_names = open(r"labels.txt", "r").readlines()

def base64_to_image(base64_string):
    # Extract the base64 encoded binary data from the input string
    base64_data = base64_string.split(",")[1]
    # Decode the base64 data to bytes
    image_bytes = base64.b64decode(base64_data)
    # Convert the bytes to numpy array
    image_array = np.frombuffer(image_bytes, dtype=np.uint8)
    # Decode the numpy array as an image using OpenCV
    image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
    return image

@socket.on("connect")
def test_connect():
    print("Connected")
    emit("my response", {"data": "Connected"})

@socket.on("image")
def receive_image(image):
    # Decode the base64-encoded image data
    image = base64_to_image(image)
    image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
    # emit("processed_image", image)
    # Make the image a numpy array and reshape it to the models input shape.
    image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)
    image = (image / 127.5) - 1
    # Predicts the model
    prediction = model.predict(image)
    index = np.argmax(prediction)
    class_name = class_names[index]
    confidence_score = prediction[0][index]
    emit("result",{"name":str(class_name),"score":str(confidence_score)})
    #######################
    
@app.route ("/")
def home():
  return render_template("index.html")

@app.route ("/storedata" , methods =[ 'GET' ] )
def storedata():
  # uname    =request.form.get("uname")
  # matching =request.form.get("matching")
  # rtime    =request.form.get("rtime")
  name    =request.args.get("class_name")
  score =request.args.get("confidence_score")
  # rtime    =request.args.get("rtime")
  
  con  = sqlite3.connect("facedb")    # connect  database
  con.row_factory = sqlite3.Row   # create object of Row
  cur = con.cursor()              # create cursor object, which will hold records 
                                  # being fetched from database. 
  cur.execute( "insert into userdata (username ,  matching , recordingtime) values ('%s',%s,'%s')"%(uname,matching,rtime))
  #cur.execute("select * from students where email=='%s' and pswd=='%s'"%(useremail,userpswd))
  con.commit()
  con.close()
   
  return  redirect(url_for( 'datafetch'))

@app.route ("/data" )
def datafetch():
  # connect to Sqlite database and fetch data
  con  = sqlite3.connect("facedb")  # connect sms database
  con.row_factory = sqlite3.Row  # create object of Row
  cur = con.cursor()             # create cursor object, which will hold records 
                        # being fetched from database. 
  cur.execute( "select id, username, matching, recordingtime from userdata order by id desc limit 10")  # execute a SQL query to get the data
  rows = cur.fetchall()          # all the data pulled from database is stored in rows object 
  con.close ()
  return render_template ('data.html', data = rows)




if __name__ == '__main__': 
  socket.run(app,host="0.0.0.0", port=7860)
    
###########################################################################
# @app.route('/table')
# def show_table():
#     # Get the current date
#     current_date = datetime.now().strftime("%Y-%m-%d")
#     # Read the CSV file to get attendance data
#     attendance=[]
#     try:
#         with open(f"{current_date}.csv", newline="") as csv_file:
#             csv_reader = csv.reader(csv_file)
#             attendance = list(csv_reader)
#     except FileNotFoundError:
#         pass
#     # Render the table.html template and pass the attendance data
#     return render_template('attendance.html', attendance=attendance)

# @app.route("/")
# def home():
#     return render_template('index.html')

   


# if __name__ == "__main__":
#     socket.run(app,host="0.0.0.0", port=7860)