Prathamesh1420's picture
Update app.py
3aa9f84 verified
import cv2
import numpy as np
import streamlit as st
from camera_input_live import camera_input_live
# Load Haarcascade for face detection
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Streamlit app title
st.title("Live Object Detection with Camera")
st.subheader("Hold your face in front of the webcam to see real-time detection.")
# Capture live camera input
image = camera_input_live()
if image is not None:
# Convert the image to OpenCV format
bytes_data = image.getvalue()
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
# Convert to grayscale for face detection
gray = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
# Draw rectangles around detected faces
for (x, y, w, h) in faces:
cv2.rectangle(cv2_img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.putText(cv2_img, "Face", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the annotated image
st.image(cv2_img, channels="BGR", caption="Detected Faces", use_container_width=True)