Spaces:
Build error
Build error
File size: 2,959 Bytes
cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 0f8cea7 cf32084 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import cv2
import streamlit as st
import imutils
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# Streamlit page configuration
st.set_page_config(page_title="Real-time Face Detection", layout="wide")
# Title for the Streamlit app
st.title("Real-time Face Detection App")
# Function to display images using matplotlib
def plt_imshow(title, image):
# Convert the image frame from BGR to RGB color space
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.title(title)
plt.axis('off')
st.pyplot(plt)
# Upload the image
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
# Read the uploaded image
image = Image.open(uploaded_file)
image = np.array(image)
# Load the Haar Cascade face detector
cascade_path = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(cascade_path)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform face detection
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
# Draw bounding boxes around detected faces
for (x, y, w, h) in rects:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Display the result in Streamlit
st.image(image, caption="Face Detected", channels="BGR", use_column_width=True)
# Upload video file
uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi"])
if uploaded_video is not None:
# Open the uploaded video
video_file = uploaded_video.read()
# Use OpenCV to read the video
video_capture = cv2.VideoCapture(video_file)
# Load the Haar Cascade face detector
cascade_path = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(cascade_path)
# Create a video writer to save the output video
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
output_file = "output_video.avi"
writer = cv2.VideoWriter(output_file, fourcc, 20, (640, 480))
while video_capture.isOpened():
ret, frame = video_capture.read()
if not ret:
break
# Resize and convert the frame to grayscale
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Perform face detection
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
# Draw bounding boxes around detected faces
for (x, y, w, h) in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Write the processed frame to the output video
writer.write(frame)
# Release the video capture and writer objects
video_capture.release()
writer.release()
# Show the output video in Streamlit
st.video(output_file)
|