zainabbbbbbbbbb commited on
Commit
92a9ac2
·
verified ·
1 Parent(s): 25a24ac

Create app.py

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