Spaces:
Sleeping
Sleeping
File size: 8,602 Bytes
4bf06f9 |
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# import os
# os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
# import streamlit as st
# import cv2
# from tqdm import tqdm
# import numpy as np
# import tensorflow as tf
# import pandas as pd
# from tempfile import NamedTemporaryFile
# from functions import *
# threshold=[0.6827917,
# 0.7136434,
# 0.510756,
# 0.56771123,
# 0.49417764,
# 0.45892453,
# 0.32996163,
# 0.5038406,
# 0.44855,
# 0.32959282,
# 0.45619836,
# 0.4969851]
# au_to_movements= {
# 'au1': 'inner brow raiser',
# 'au2': 'outer brow raiser',
# 'au4': 'brow lowerer',
# 'au5': 'upper lid raiser',
# 'au6': 'cheek raiser',
# 'au9': 'nose wrinkler',
# 'au12': 'lip corner puller',
# 'au15': 'lip corner depressor',
# 'au17': 'chin raiser',
# 'au20': 'lip stretcher',
# 'au25': 'lips part',
# 'au26': 'jaw drop'
# }
# au_labels = [
# "au1",
# "au12",
# "au15",
# "au17",
# "au2",
# "au20",
# "au25",
# "au26",
# "au4",
# "au5",
# "au6",
# "au9"
# ]
# col=[au_to_movements[i] for i in au_labels]
# def binary_focal_loss(gamma=2.0, alpha=0.25):
# def focal_loss(y_true, y_pred):
# # Define epsilon to avoid log(0)
# epsilon = tf.keras.backend.epsilon()
# # Clip predictions to prevent log(0) and log(1 - 0)
# y_pred = tf.clip_by_value(y_pred, epsilon, 1.0 - epsilon)
# # Compute the focal loss
# fl = - alpha * (y_true * (1 - y_pred)**gamma * tf.math.log(y_pred)
# + (1 - y_true) * (y_pred**gamma) * tf.math.log(1 - y_pred))
# return tf.reduce_mean(fl, axis=-1)
# return focal_loss
# loss = binary_focal_loss(gamma=2.0, alpha=0.25)
# # Function to read video frames into a list
# def read_video_frames(video_path):
# cap = cv2.VideoCapture(video_path)
# frames = []
# while True:
# ret, frame = cap.read()
# if not ret:
# break
# frames.append(frame)
# cap.release()
# return frames
# # Function to process frames and make predictions
# def process_frames(frames, model):
# frames = [get_face(frame) for frame in tqdm(frames[:len(frames)-1])]
# st.text(f"face shape : {frames[0].shape}")
# frame_array = np.array(frames)
# preds = model.predict(frame_array)
# print(preds[0])
# predicted_labels = np.zeros_like(preds,dtype='int')
# for i in range(12):
# predicted_labels[:, i] = (preds[:, i] > threshold[i]).astype(int)
# return predicted_labels
# # Function to save predictions to a CSV file
# def save_predictions_to_csv(predictions, filename="predictions.csv"):
# df = pd.DataFrame(predictions,columns=col)
# df.to_csv(filename, index=False)
# return filename
# # Load your Keras model
# def load_model():
# model = tf.keras.models.load_model('incept_v3_10fps_full_0.4.keras',
# custom_objects={'binary_focal_loss': binary_focal_loss})
# return model
# # Streamlit app
# def main():
# st.title("Video Frame Prediction App")
# # Upload video file
# uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
# if uploaded_file is not None:
# with NamedTemporaryFile(delete=False) as tmp_file:
# tmp_file.write(uploaded_file.read())
# video_path = tmp_file.name
# # Load the model
# model = load_model()
# # Predict button
# if st.button("Predict"):
# # Read frames from video
# st.text("Reading video frames...")
# frames = read_video_frames(video_path)
# st.text(f"Total frames read: {len(frames)}")
# # Process frames and make predictions
# st.text("Processing frames and making predictions...")
# predictions = process_frames(frames, model)
# st.text("Predictions completed!")
# # Save predictions to CSV
# csv_file_path = save_predictions_to_csv(predictions)
# st.text("Predictions saved to CSV!")
# # Make CSV downloadable
# with open(csv_file_path, "rb") as f:
# st.download_button(
# label="Download CSV",
# data=f,
# file_name="predictions.csv",
# mime="text/csv"
# )
# # Clean up the temporary file
# os.remove(video_path)
# if __name__ == "__main__":
# main()
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
import streamlit as st
import cv2
from tqdm import tqdm
import numpy as np
import tensorflow as tf
import pandas as pd
from tempfile import NamedTemporaryFile
from functions import *
threshold = [
0.6827917, 0.7136434, 0.510756, 0.56771123, 0.49417764, 0.45892453,
0.32996163, 0.5038406, 0.44855, 0.32959282, 0.45619836, 0.4969851
]
au_to_movements = {
'au1': 'inner brow raiser',
'au2': 'outer brow raiser',
'au4': 'brow lowerer',
'au5': 'upper lid raiser',
'au6': 'cheek raiser',
'au9': 'nose wrinkler',
'au12': 'lip corner puller',
'au15': 'lip corner depressor',
'au17': 'chin raiser',
'au20': 'lip stretcher',
'au25': 'lips part',
'au26': 'jaw drop'
}
au_labels = [
"au1", "au12", "au15", "au17", "au2", "au20",
"au25", "au26", "au4", "au5", "au6", "au9"
]
col = [au_to_movements[i] for i in au_labels]
def binary_focal_loss(gamma=2.0, alpha=0.25):
def focal_loss(y_true, y_pred):
epsilon = tf.keras.backend.epsilon()
y_pred = tf.clip_by_value(y_pred, epsilon, 1.0 - epsilon)
fl = - alpha * (y_true * (1 - y_pred)**gamma * tf.math.log(y_pred)
+ (1 - y_true) * (y_pred**gamma) * tf.math.log(1 - y_pred))
return tf.reduce_mean(fl, axis=-1)
return focal_loss
loss = binary_focal_loss(gamma=2.0, alpha=0.25)
# Function to read video frames into a list and get timestamps
def read_video_frames(video_path):
cap = cv2.VideoCapture(video_path)
frames = []
faces=[]
timestamps = []
fps = cap.get(cv2.CAP_PROP_FPS)
while True:
ret, frame = cap.read()
if not ret:
break
face=get_face(frame)
if face is not None:
faces.append(face)
frames.append(frame)
timestamps.append(cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0) # Time in seconds
cap.release()
return frames,faces, timestamps
# Function to process frames and make predictions
def process_frames(frames, model):
frame_array = np.array(frames)
preds = model.predict(frame_array)
predicted_labels = np.zeros_like(preds, dtype='int')
for i in range(12):
predicted_labels[:, i] = (preds[:, i] > threshold[i]).astype(int)
return predicted_labels
# Function to save predictions to a CSV file with timestamps
def save_predictions_to_csv(predictions, timestamps, filename="predictions.csv"):
df = pd.DataFrame(predictions, columns=col)
df['timestamp'] = timestamps
df.set_index('timestamp', inplace=True)
df.to_csv(filename)
return filename
# Load your Keras model
def load_model():
model = tf.keras.models.load_model('incept_v3_10fps_full_0.4.keras',
custom_objects={'binary_focal_loss': binary_focal_loss})
return model
# Streamlit app
def main():
st.title("Facial action unit detection")
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
if uploaded_file is not None:
with NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(uploaded_file.read())
video_path = tmp_file.name
model = load_model()
if st.button("Predict"):
st.text("Reading video frames...")
frames,faces, timestamps = read_video_frames(video_path)
st.text(f"Total frames in which faces found: {len(faces)}")
st.text("Processing frames and making predictions...")
predictions = process_frames(faces, model)
st.text("Predictions completed!")
csv_file_path = save_predictions_to_csv(predictions, timestamps)
st.text("Predictions saved to CSV!")
with open(csv_file_path, "rb") as f:
st.download_button(
label="Download CSV",
data=f,
file_name="predictions.csv",
mime="text/csv"
)
os.remove(video_path)
if __name__ == "__main__":
main()
|