open-cv-pro-12 / app.py
Dorn4449's picture
Update app.py
c2cd154 verified
import gradio as gr
import tensorflow as tf
import cv2
import numpy as np
import pickle
import mediapipe as mp
import os
# βœ… Load Face Detector
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# βœ… Load Model and Custom Dropout Layer
class MCDropout(tf.keras.layers.Dropout):
def call(self, inputs):
return super().call(inputs, training=True)
# βœ… Load the Saved Model
MODEL_PATH = "facial_recogn_model.keras"
model = tf.keras.models.load_model(MODEL_PATH, custom_objects={"MCDropout": MCDropout}, compile=False)
# βœ… Load Registered Users
USER_DATABASE = "registered_users.pkl"
if os.path.exists(USER_DATABASE):
with open(USER_DATABASE, "rb") as f:
registered_users = pickle.load(f)
else:
registered_users = {}
# βœ… Function to Register a User
def register_user(username, image):
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert from RGB to BGR
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) == 0:
return "❌ No face detected! Try again."
x, y, w, h = faces[0]
face = gray[y:y+h, x:x+w]
face = cv2.resize(face, (160, 160)) / 255.0 # Normalize
face = face.reshape(1, 160, 160, 1)
registered_users[username] = model.predict(face)[0] # Store as a 1D array
# Save to file
with open(USER_DATABASE, "wb") as f:
pickle.dump(registered_users, f)
return f"βœ… {username} registered successfully!"
# βœ… Cosine Similarity Function
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# βœ… Function to Authenticate User
def authenticate_user(image):
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) == 0:
return "❌ No face detected! Try again."
x, y, w, h = faces[0]
face = gray[y:y+h, x:x+w]
face = cv2.resize(face, (160, 160)) / 255.0 # Normalize
face = face.reshape(1, 160, 160, 1)
user_embedding = model.predict(face)[0]
for username, saved_embedding in registered_users.items():
similarity = cosine_similarity(user_embedding, saved_embedding)
if similarity > 0.85: # Threshold for authentication
return f"βœ… Welcome, {username}!"
return "❌ Authentication Failed!"
# βœ… Gesture Control Function
def gesture_control(image):
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
mp_draw = mp.solutions.drawing_utils
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
result = hands.process(frame_rgb)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
mp_draw.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
index_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x
thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].x
wrist = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x
if thumb_tip > wrist + 0.1:
return "πŸ’‘ Lights ON"
elif thumb_tip < wrist - 0.1:
return "πŸ’‘ Lights OFF"
elif index_tip > wrist + 0.2:
return "πŸšͺ Door Opening"
elif index_tip < wrist - 0.2:
return "πŸšͺ Door Closing"
return "πŸ€– No gesture detected"
# βœ… Gradio Interface
iface = gr.Interface(
fn=lambda action, username, image: (
register_user(username, image) if action == "Register"
else authenticate_user(image) if action == "Authenticate"
else gesture_control(image)
),
inputs=[
gr.Radio(["Register", "Authenticate", "Gesture Control"], label="Select Action"),
gr.Textbox(label="Enter Username (For Registration Only)"),
gr.Image(type="numpy", label="Upload Image")
],
outputs=gr.Textbox(label="Result"),
title="Face Authentication & Gesture Control",
description="πŸ” Register, Authenticate Users & Control Devices via Hand Gestures."
)
# βœ… Launch the Gradio App
iface.launch()