Create 2_π·_Webcamera.py
Browse files- pages/2_π·_Webcamera.py +67 -0
pages/2_π·_Webcamera.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_webrtc import webrtc_streamer
|
3 |
+
import av
|
4 |
+
import cv2
|
5 |
+
import time
|
6 |
+
import mediapipe as mp
|
7 |
+
import numpy as np
|
8 |
+
import pandas as pd
|
9 |
+
from mediapipe_functions import *
|
10 |
+
from utils import *
|
11 |
+
import tensorflow as tf
|
12 |
+
|
13 |
+
st.title("Webcamera")
|
14 |
+
st.write("Steps to use: \n1. Click on Start button.\n2. To stop the video when done, press Stop. \n\n The output will be displayed in about 40 secs.")
|
15 |
+
|
16 |
+
class VideoProcessor:
|
17 |
+
def __init__(self) -> None:
|
18 |
+
self.threshold1 = 100
|
19 |
+
self.threshold2 = 200
|
20 |
+
self.my_list = []
|
21 |
+
|
22 |
+
def recv(self, frame):
|
23 |
+
img = frame.to_ndarray(format="bgr24")
|
24 |
+
self.my_list.append(img)
|
25 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
26 |
+
|
27 |
+
# Create the video processor instance
|
28 |
+
video_processor = VideoProcessor()
|
29 |
+
|
30 |
+
ctx = webrtc_streamer(key="sample", video_processor_factory=lambda: video_processor)
|
31 |
+
|
32 |
+
time.sleep(10)
|
33 |
+
st.write(len(ctx.video_processor.my_list))
|
34 |
+
|
35 |
+
# Access the frames list after the webrtc_streamer function has finished running
|
36 |
+
frames_list = ctx.video_processor.my_list
|
37 |
+
|
38 |
+
# # Display the last frame
|
39 |
+
# if frames_list:
|
40 |
+
# st.image(frames_list[-1], channels="BGR")
|
41 |
+
st.write("Running...")
|
42 |
+
|
43 |
+
# Continuing with the code for inference pipeline
|
44 |
+
final_landmarks = extract_landmarks(frames_list)
|
45 |
+
df1 = pd.DataFrame(final_landmarks,columns=['x','y','z'])
|
46 |
+
ROWS_PER_FRAME = 543
|
47 |
+
|
48 |
+
# Loading data
|
49 |
+
st.write(len(frames_list))
|
50 |
+
test_df = load_relevant_data_subset(df1, ROWS_PER_FRAME=ROWS_PER_FRAME)
|
51 |
+
test_df = tf.convert_to_tensor(test_df)
|
52 |
+
|
53 |
+
# Inference
|
54 |
+
interpreter = tf.lite.Interpreter("models/model.tflite")
|
55 |
+
prediction_fn = interpreter.get_signature_runner("serving_default")
|
56 |
+
output = prediction_fn(inputs=test_df)
|
57 |
+
sign = np.argmax(output["outputs"])
|
58 |
+
sign_json=pd.read_json("sign_to_prediction_index_map.json",typ='series')
|
59 |
+
sign_df=pd.DataFrame(sign_json)
|
60 |
+
sign_df.iloc[sign]
|
61 |
+
top_indices = np.argsort(output['outputs'])[::-1][:5]
|
62 |
+
top_values = output['outputs'][top_indices]
|
63 |
+
|
64 |
+
output_df = sign_df.iloc[top_indices]
|
65 |
+
output_df['Value'] = top_values
|
66 |
+
output_df.rename(columns = {0:'Index'}, inplace = True)
|
67 |
+
st.write(output_df)
|