Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import imutils
|
| 4 |
+
from imutils.video import VideoStream
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Load Haar Cascade
|
| 8 |
+
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
|
| 9 |
+
|
| 10 |
+
# Function to display images in Streamlit
|
| 11 |
+
def plt_imshow(title, image):
|
| 12 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 13 |
+
st.image(image, caption=title, use_column_width=True)
|
| 14 |
+
|
| 15 |
+
# File upload for video input
|
| 16 |
+
st.title("Real-time Face Detection")
|
| 17 |
+
st.write("Upload a video to perform face detection.")
|
| 18 |
+
|
| 19 |
+
video_file = st.file_uploader("Choose a video...", type=["mp4", "mov", "avi"])
|
| 20 |
+
|
| 21 |
+
if video_file is not None:
|
| 22 |
+
# Open the video file uploaded by the user
|
| 23 |
+
vs = cv2.VideoCapture(video_file)
|
| 24 |
+
writer = None
|
| 25 |
+
|
| 26 |
+
while True:
|
| 27 |
+
ret, frame = vs.read()
|
| 28 |
+
if not ret:
|
| 29 |
+
break
|
| 30 |
+
|
| 31 |
+
# Resize the frame
|
| 32 |
+
frame = imutils.resize(frame, width=500)
|
| 33 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 34 |
+
|
| 35 |
+
# Detect faces
|
| 36 |
+
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(30, 30))
|
| 37 |
+
|
| 38 |
+
# Draw bounding boxes
|
| 39 |
+
for (x, y, w, h) in rects:
|
| 40 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 41 |
+
|
| 42 |
+
# Display the frame in Streamlit
|
| 43 |
+
plt_imshow("Face Detection", frame)
|
| 44 |
+
|
| 45 |
+
vs.release()
|