zainabbbbbbbbbb commited on
Commit
cf32084
·
verified ·
1 Parent(s): 499eb86

Update app.py

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