cctvbackend / app.py
neerajkalyank's picture
Update app.py
252c8e5 verified
import gradio as gr
import cv2
import numpy as np
from detect_people import detect_people_from_image
from table_occupancy import is_table_occupied
from face_utils import recognize_face
from db import get_table_status, log_customer_visit, get_alerts
# Gradio interface function to analyze uploaded image
def analyze_image(image):
try:
# Ensure image is in correct format (NumPy array, BGR for OpenCV)
if not isinstance(image, np.ndarray):
image = np.array(image)
if image.shape[-1] == 4: # Convert RGBA to BGR if needed
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
elif image.shape[-1] == 3: # Ensure RGB is converted to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Run analysis functions
people = detect_people_from_image(image)
seated = is_table_occupied(image)
role = recognize_face(image)
# Log a visit to the database (adjust timestamp as needed)
import datetime
log_customer_visit(role, datetime.datetime.now(), table_id=1)
# Get table status and alerts
table_status = get_table_status()
alerts = get_alerts()
# Format output
output = (
f"People detected: {people}\n"
f"Table occupied: {seated}\n"
f"Face match: {role}\n"
f"Table status: {table_status}\n"
f"Active alerts: {alerts}"
)
return output
except Exception as e:
return f"Error processing image: {e}"
# Define Gradio interface
demo = gr.Interface(
fn=analyze_image,
inputs=gr.Image(type="numpy"), # Expect NumPy array input
outputs="text",
title="Table Occupancy and Face Detection",
description="Upload an image to detect people, table occupancy, and recognize faces."
)
# Start the Gradio app
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)