Spaces:
Runtime error
Runtime error
Commit
·
c8b960f
1
Parent(s):
b8e1551
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
|
2 |
+
import streamlit as st
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import av
|
6 |
+
import mediapipe as mp
|
7 |
+
import base64
|
8 |
+
|
9 |
+
|
10 |
+
###################################### Helper functions ##############################
|
11 |
+
# Read the image file and encode it as base64
|
12 |
+
|
13 |
+
with open('/mount/src/rock_paper_scissors/Resources/ai_face.jpg', 'rb') as aiface:
|
14 |
+
image_data = base64.b64encode(aiface.read()).decode('utf-8')
|
15 |
+
|
16 |
+
# Set up MediaPipe Hands
|
17 |
+
mp_drawing = mp.solutions.drawing_utils
|
18 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
19 |
+
mp_hands = mp.solutions.hands
|
20 |
+
hands = mp_hands.Hands(
|
21 |
+
model_complexity=0,
|
22 |
+
min_detection_confidence=0.5,
|
23 |
+
min_tracking_confidence=0.5
|
24 |
+
)
|
25 |
+
|
26 |
+
# Function to process video frames
|
27 |
+
def process(image):
|
28 |
+
image.flags.writeable = False
|
29 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
30 |
+
results = hands.process(image)
|
31 |
+
|
32 |
+
# Draw hand landmarks on the image
|
33 |
+
image.flags.writeable = True
|
34 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
35 |
+
if results.multi_hand_landmarks:
|
36 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
37 |
+
mp_drawing.draw_landmarks(
|
38 |
+
image,
|
39 |
+
hand_landmarks,
|
40 |
+
mp_hands.HAND_CONNECTIONS,
|
41 |
+
mp_drawing_styles.get_default_hand_landmarks_style(),
|
42 |
+
mp_drawing_styles.get_default_hand_connections_style()
|
43 |
+
)
|
44 |
+
|
45 |
+
return cv2.flip(image, 1)
|
46 |
+
|
47 |
+
# Define RTC Configuration
|
48 |
+
RTC_CONFIGURATION = RTCConfiguration(
|
49 |
+
{"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
|
50 |
+
)
|
51 |
+
|
52 |
+
|
53 |
+
# Create Streamlit web app
|
54 |
+
scores = [0, 0] # [AI, Player]
|
55 |
+
|
56 |
+
st.set_page_config(page_title="RPS", page_icon="🤖", layout="wide",)
|
57 |
+
|
58 |
+
col1, col2 = st.columns(2)
|
59 |
+
|
60 |
+
# Add content to the right column (video stream)
|
61 |
+
with col1:
|
62 |
+
st.info(f"Player **{scores[1]}**")
|
63 |
+
# Define a video processor class
|
64 |
+
class VideoProcessor:
|
65 |
+
def recv(self, frame):
|
66 |
+
img = frame.to_ndarray(format="bgr24")
|
67 |
+
img = process(img)
|
68 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
69 |
+
|
70 |
+
# Create the WebRTC streamer
|
71 |
+
webrtc_ctx = webrtc_streamer(
|
72 |
+
key="hand-tracking",
|
73 |
+
mode=WebRtcMode.SENDRECV,
|
74 |
+
rtc_configuration=RTC_CONFIGURATION,
|
75 |
+
media_stream_constraints={"video": True, "audio": False},
|
76 |
+
video_processor_factory=VideoProcessor,
|
77 |
+
async_processing=True,
|
78 |
+
)
|
79 |
+
|
80 |
+
# Add content to the left column (app description)
|
81 |
+
with col2:
|
82 |
+
st.info(f"AI **{scores[0]}**")
|
83 |
+
img_tag = f'<img src="data:image/png;base64,{image_data}" style="border: 2px solid green; border-radius: 15px;">'
|
84 |
+
# Create a Streamlit component to render the HTML
|
85 |
+
st.components.v1.html(img_tag, height=400)
|