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