Prathamesh1420/Streamlit_camera_live
2
1import cv22import numpy as np3import streamlit as st4from camera_input_live import camera_input_live5 6# Load Haarcascade for face detection7cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")8 9# Streamlit app title10st.title("Live Object Detection with Camera")11st.subheader("Hold your face in front of the webcam to see real-time detection.")12 13# Capture live camera input14image = camera_input_live()15 16if image is not None:17 # Display the captured image18 st.image(image, caption="Live Camera Input", use_column_width=True)19 20 # Convert the image to OpenCV format21 bytes_data = image.getvalue()22 cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)23 24 # Convert to grayscale for face detection25 gray = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY)26 27 # Detect faces in the image28 faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)29 30 # Draw rectangles around detected faces31 for (x, y, w, h) in faces:32 cv2.rectangle(cv2_img, (x, y), (x + w, y + h), (0, 255, 0), 3)33 cv2.putText(cv2_img, "Face", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)34 35 # Display the annotated image36 st.image(cv2_img, channels="BGR", caption="Detected Faces", use_column_width=True)37 