Update app.py
Browse files
app.py
CHANGED
@@ -9,17 +9,13 @@ from tensorflow.keras.models import load_model # Importing load_model
|
|
9 |
from datetime import datetime # Importing datetime
|
10 |
|
11 |
# Constants
|
12 |
-
HOME_DIR = os.getcwd() # Home directory
|
13 |
DATABASE = "students.db" # SQLite database to store student information
|
14 |
REPO_NAME = "face-and-emotion-detection"
|
15 |
REPO_ID = f"LovnishVerma/{REPO_NAME}" # Hugging Face Repo
|
16 |
EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5" # Emotion detection model file
|
17 |
EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
18 |
|
19 |
-
# Ensure home directory exists
|
20 |
-
if not os.path.exists(HOME_DIR):
|
21 |
-
os.makedirs(HOME_DIR)
|
22 |
-
|
23 |
# Retrieve Hugging Face token from environment variable
|
24 |
hf_token = os.getenv("upload")
|
25 |
if not hf_token:
|
@@ -58,22 +54,22 @@ def initialize_database():
|
|
58 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
59 |
name TEXT NOT NULL,
|
60 |
roll_no TEXT NOT NULL UNIQUE,
|
61 |
-
|
62 |
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
63 |
)
|
64 |
""")
|
65 |
conn.commit()
|
66 |
conn.close()
|
67 |
|
68 |
-
def save_to_database(name, roll_no,
|
69 |
""" Saves the student's data to the database. """
|
70 |
conn = sqlite3.connect(DATABASE)
|
71 |
cursor = conn.cursor()
|
72 |
try:
|
73 |
cursor.execute("""
|
74 |
-
INSERT INTO students (name, roll_no,
|
75 |
VALUES (?, ?, ?)
|
76 |
-
""", (name, roll_no,
|
77 |
conn.commit()
|
78 |
st.success("Data saved successfully!")
|
79 |
except sqlite3.IntegrityError:
|
@@ -103,11 +99,15 @@ def save_image_to_hugging_face(image, name, roll_no):
|
|
103 |
repo_type="space",
|
104 |
token=hf_token,
|
105 |
)
|
106 |
-
|
|
|
|
|
|
|
|
|
107 |
except Exception as e:
|
108 |
st.error(f"Error saving or uploading image: {e}")
|
109 |
|
110 |
-
return
|
111 |
|
112 |
# Initialize the database when the app starts
|
113 |
initialize_database()
|
@@ -143,8 +143,8 @@ if st.button("Register"):
|
|
143 |
image = Image.open(picture)
|
144 |
|
145 |
# Save the image locally and upload it to Hugging Face
|
146 |
-
|
147 |
-
save_to_database(name, roll_no,
|
148 |
except Exception as e:
|
149 |
st.error(f"An error occurred: {e}")
|
150 |
|
@@ -152,15 +152,15 @@ if st.button("Register"):
|
|
152 |
if st.checkbox("Show registered students"):
|
153 |
conn = sqlite3.connect(DATABASE)
|
154 |
cursor = conn.cursor()
|
155 |
-
cursor.execute("SELECT name, roll_no,
|
156 |
rows = cursor.fetchall()
|
157 |
conn.close()
|
158 |
|
159 |
st.write("### Registered Students")
|
160 |
for row in rows:
|
161 |
-
name, roll_no,
|
162 |
st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
|
163 |
-
st.image(
|
164 |
|
165 |
# Face and Emotion Detection Function
|
166 |
def detect_faces_and_emotions(image):
|
|
|
9 |
from datetime import datetime # Importing datetime
|
10 |
|
11 |
# Constants
|
12 |
+
HOME_DIR = os.getcwd() # Home directory (root directory)
|
13 |
DATABASE = "students.db" # SQLite database to store student information
|
14 |
REPO_NAME = "face-and-emotion-detection"
|
15 |
REPO_ID = f"LovnishVerma/{REPO_NAME}" # Hugging Face Repo
|
16 |
EMOTION_MODEL_FILE = "CNN_Model_acc_75.h5" # Emotion detection model file
|
17 |
EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
18 |
|
|
|
|
|
|
|
|
|
19 |
# Retrieve Hugging Face token from environment variable
|
20 |
hf_token = os.getenv("upload")
|
21 |
if not hf_token:
|
|
|
54 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
55 |
name TEXT NOT NULL,
|
56 |
roll_no TEXT NOT NULL UNIQUE,
|
57 |
+
image_url TEXT NOT NULL,
|
58 |
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
59 |
)
|
60 |
""")
|
61 |
conn.commit()
|
62 |
conn.close()
|
63 |
|
64 |
+
def save_to_database(name, roll_no, image_url):
|
65 |
""" Saves the student's data to the database. """
|
66 |
conn = sqlite3.connect(DATABASE)
|
67 |
cursor = conn.cursor()
|
68 |
try:
|
69 |
cursor.execute("""
|
70 |
+
INSERT INTO students (name, roll_no, image_url)
|
71 |
VALUES (?, ?, ?)
|
72 |
+
""", (name, roll_no, image_url))
|
73 |
conn.commit()
|
74 |
st.success("Data saved successfully!")
|
75 |
except sqlite3.IntegrityError:
|
|
|
99 |
repo_type="space",
|
100 |
token=hf_token,
|
101 |
)
|
102 |
+
|
103 |
+
# Construct the image URL for Hugging Face
|
104 |
+
image_url = f"https://{REPO_NAME}.hf.space/media/{filename}"
|
105 |
+
st.success(f"Image saved to Hugging Face as {filename}. URL: {image_url}")
|
106 |
+
|
107 |
except Exception as e:
|
108 |
st.error(f"Error saving or uploading image: {e}")
|
109 |
|
110 |
+
return image_url
|
111 |
|
112 |
# Initialize the database when the app starts
|
113 |
initialize_database()
|
|
|
143 |
image = Image.open(picture)
|
144 |
|
145 |
# Save the image locally and upload it to Hugging Face
|
146 |
+
image_url = save_image_to_hugging_face(image, name, roll_no)
|
147 |
+
save_to_database(name, roll_no, image_url)
|
148 |
except Exception as e:
|
149 |
st.error(f"An error occurred: {e}")
|
150 |
|
|
|
152 |
if st.checkbox("Show registered students"):
|
153 |
conn = sqlite3.connect(DATABASE)
|
154 |
cursor = conn.cursor()
|
155 |
+
cursor.execute("SELECT name, roll_no, image_url, timestamp FROM students")
|
156 |
rows = cursor.fetchall()
|
157 |
conn.close()
|
158 |
|
159 |
st.write("### Registered Students")
|
160 |
for row in rows:
|
161 |
+
name, roll_no, image_url, timestamp = row
|
162 |
st.write(f"**Name:** {name}, **Roll No:** {roll_no}, **Timestamp:** {timestamp}")
|
163 |
+
st.image(image_url, caption=f"{name} ({roll_no})", use_column_width=True)
|
164 |
|
165 |
# Face and Emotion Detection Function
|
166 |
def detect_faces_and_emotions(image):
|