|
import cv2 |
|
import numpy as np |
|
import streamlit as st |
|
from camera_input_live import camera_input_live |
|
|
|
|
|
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") |
|
|
|
|
|
st.title("Live Object Detection with Camera") |
|
st.subheader("Hold your face in front of the webcam to see real-time detection.") |
|
|
|
|
|
image = camera_input_live() |
|
|
|
if image is not None: |
|
|
|
|
|
bytes_data = image.getvalue() |
|
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR) |
|
|
|
|
|
gray = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3) |
|
|
|
|
|
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) |
|
|
|
|
|
st.image(cv2_img, channels="BGR", caption="Detected Faces", use_container_width=True) |
|
|