-
Notifications
You must be signed in to change notification settings - Fork 2
/
live.py
145 lines (116 loc) · 5.06 KB
/
live.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import streamlit as st
import cv2
import time
import numpy as np
from keras.models import load_model
from pipeline import main
import json
import pandas as pd
model = load_model('Trained_on_dataset.h5')
with open('Class.json') as f:
class_data = json.load(f)
st.set_page_config(page_title="Facial Reconstruction Dashboard", layout="wide")
st.title("Facial Reconstruction Dashboard")
if 'streaming' not in st.session_state:
st.session_state.streaming = False
if 'found_face_info' not in st.session_state:
st.session_state.found_face_info = None
def get_suspect_details():
return {
"Name": "Arjun Verma",
"ID": "230102125",
"Status": "ECE",
}
def preprocess_image(image):
image = cv2.resize(image, (224, 224))
if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
elif image.shape[2] == 1:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
image = image / 255.0
return image
def video_stream():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
st.error("Error: Cannot access the webcam.")
return
frame_placeholder = st.empty()
while st.session_state.streaming:
ret, frame = cap.read()
if not ret:
st.warning("No frame received.")
break
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml').detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=5)
face_detected = False
for (x, y, w, h) in faces:
face = frame[y:y + h, x:x + w]
processed_face = preprocess_image(face)
final = main(processed_face)
if isinstance(final, np.ndarray):
final = final
else:
final = np.array(final)
if final.ndim == 2:
final = np.expand_dims(final, axis=-1)
final = cv2.cvtColor(final, cv2.COLOR_RGB2BGR)
final = cv2.resize(final, (224, 224))
final = np.expand_dims(final, axis=0)
prediction = model.predict(final)
confidence = np.max(prediction)
class_id = np.argmax(prediction)
predicted_class_info = class_data.get(str(class_id), {"Name": "Unknown", "ID": "N/A", "Status": "N/A"})
face_detected = True
st.session_state.found_face_info = (face, class_id, confidence)
cv2.rectangle(frame_rgb, (x, y), (x + w, y + h), (0, 255, 0), 2)
frame_placeholder.image(frame_rgb, channels="RGB", use_column_width=True)
# If face detected, break the loop
if face_detected:
break
time.sleep(0.03)
cap.release()
left_col, right_col = st.columns(2)
with left_col:
st.header("Live Webcam Stream")
if not st.session_state.streaming:
if st.button("Start Webcam Stream", key="start_button"):
st.session_state.streaming = True
video_stream()
else:
if st.button("Stop Stream", key="stop_button"):
st.session_state.streaming = False
if not st.session_state.streaming:
st.write("Press 'Start Webcam Stream' to begin.")
st.subheader("Terminal Output")
if st.button("Show Logs", key="logs_button"):
st.text_area("Logs", "[INFO] Streaming from webcam...", height=200)
with right_col:
st.header("Suspect Details")
suspect_info = get_suspect_details()
suspect_df = pd.DataFrame(suspect_info.items(), columns=["Field", "Value"])
st.table(suspect_df)
if st.session_state.found_face_info:
found_face, class_id, confidence = st.session_state.found_face_info
found_face_rgb = cv2.cvtColor(found_face, cv2.COLOR_BGR2RGB)
predicted_class_info = class_data.get(str(class_id), {"Name": "Unknown", "ID": "N/A", "Status": "N/A"})
st.write(f"Name: {predicted_class_info['Name']}")
st.write(f"ID: {predicted_class_info['ID']}")
st.write(f"Status: {predicted_class_info['Status']}")
if predicted_class_info["Name"] == suspect_info["Name"]:
st.header("Match Found!")
st.header("Comparison of Found and Suspect Images")
col1, col2 = st.columns(2)
with col1:
st.subheader("Found Image")
st.image(found_face_rgb, caption=f"Image from Video (Confidence: {confidence:.2f})", channels="RGB", width=200)
with col2:
suspect_image_path = f"Headsets/{suspect_info['Name']}/1.jpg"
suspect_face = cv2.imread(suspect_image_path)
if suspect_face is not None:
suspect_face_rgb = cv2.cvtColor(suspect_face, cv2.COLOR_BGR2RGB)
st.subheader("Suspect Image")
st.image(suspect_face_rgb, caption="Actual Suspect", channels="RGB", width=200)
else:
st.error("Error loading suspect image.")