AldinWil10 commited on
Commit
0f9763b
1 Parent(s): df0d55b

Upload 4 files

Browse files
app_streamlit.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import streamlit as st
3
+ import numpy as np
4
+ from keras.models import load_model
5
+ # import tempfile
6
+
7
+ # Load the pre-trained model
8
+ model = load_model('keras_model.h5')
9
+
10
+ # Load the class labels
11
+ with open('labels.txt', 'r') as f:
12
+ class_names = [line.strip() for line in f.readlines()]
13
+
14
+ # Load the Haar cascade classifier for face detection
15
+ face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
16
+
17
+ font = cv2.FONT_HERSHEY_SIMPLEX
18
+
19
+ # Use this line to capture video from the webcam
20
+ cap = cv2.VideoCapture(0)
21
+
22
+ # Set the title for the Streamlit app
23
+ st.title("Video Capture with OpenCV")
24
+
25
+ frame_placeholder = st.empty()
26
+
27
+ # Add a "Stop" button and store its state in a variable
28
+ stop_button_pressed = st.button("Stop")
29
+
30
+ while cap.isOpened() and not stop_button_pressed:
31
+ ret, frame = cap.read()
32
+ # Detect faces in the frame
33
+ faces = face_cascade.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=5)
34
+ if not ret:
35
+ st.write("The video capture has ended.")
36
+ break
37
+
38
+ # You can process the frame here if needed
39
+ # e.g., apply filters, transformations, or object detection
40
+ for (x, y, w, h) in faces:
41
+ # Extract the face region
42
+ face_img = frame[y:y+h, x:x+w]
43
+
44
+ # Preprocess the face image
45
+ face_img = cv2.resize(face_img, (224, 224))
46
+ face_img = np.expand_dims(face_img, axis=0)
47
+ face_img = face_img / 255.0
48
+
49
+ # Predict the class probabilities
50
+ pred_probs = model.predict(face_img)[0]
51
+ class_idx = np.argmax(pred_probs)
52
+ class_prob = pred_probs[class_idx]
53
+
54
+ # Get the class name and display it on the image
55
+ class_name = class_names[class_idx]
56
+ if class_prob*100 < 70:
57
+ cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
58
+ text = '{}: {:.2f}%'.format('Unknown', class_prob * 100)
59
+ cv2.putText(frame, text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
60
+ else:
61
+ cv2.putText(frame, class_name, (x, y - 10), font, 1, (0, 255, 0), 2)
62
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
63
+ text = '{}: {:.2f}%'.format(class_name, class_prob * 100)
64
+ cv2.putText(frame, text, (x, y + h + 30), font, 0.5, (0, 255, 0), 1)
65
+ # Convert the frame from BGR to RGB format
66
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
67
+
68
+ # Display the frame using Streamlit's st.image
69
+ frame_placeholder.image(frame, channels="RGB")
70
+
71
+ # Break the loop if the 'q' key is pressed or the user clicks the "Stop" button
72
+ if cv2.waitKey(1) & 0xFF == ord("q") or stop_button_pressed:
73
+ break
74
+
75
+ cap.release()
76
+ cv2.destroyAllWindows()
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff
 
keras_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f8e70aa553db5a361cb8fa0adea3f593741674374590fed2b250cd344782493
3
+ size 2453432
labels.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ 0 Aldin
2
+ 1 Rowan