Spaces:
Build error
Build error
| 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) | |