EternalEchoes / app.py
taechasith's picture
Update app.py
cf0334c verified
Raw
History Blame Contribute Delete
2.58 kB
import gradio as gr
import numpy as np
import cv2
import mediapipe as mp
import torch
import torchaudio
from scipy.signal import chirp
import plotly.graph_objects as go
# Load MediaPipe Hand Tracking
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
# Generate Quantum Wave Sound
def generate_wave(frequency=220, duration=3):
sample_rate = 44100
t = np.linspace(0, duration, int(sample_rate * duration))
waveform = chirp(t, f0=frequency, f1=frequency*2, t1=duration, method='quadratic')
return waveform, sample_rate
# Process Hand Tracking
def process_frame(frame):
try:
if frame is None:
return np.zeros((480, 640, 3), dtype=np.uint8), 0 # Return empty image if no frame
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
rotation = 0
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
x = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x
rotation = (x - 0.5) * 360 # Rotate black hole based on hand movement
return frame, rotation # Return processed frame + rotation angle
except Exception as e:
print(f"Error processing frame: {e}")
return np.zeros((480, 640, 3), dtype=np.uint8), 0 # Return blank image if error occurs
# Create Black Hole Visualization
def black_hole_visualization(rotation):
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X**2 + Y**2 - rotation * 0.01) # Distorted wave effect
fig = go.Figure(data=[go.Surface(z=Z)])
fig.update_layout(title="Black Hole Distortion", autosize=True)
return fig
# Main function integrating webcam, black hole, and sound
def run_app(webcam):
frame, rotation = process_frame(webcam)
black_hole_figure = black_hole_visualization(rotation)
wave_data, sr = generate_wave(frequency=200 + rotation)
return frame, black_hole_figure, (wave_data, sr)
# Launch Gradio UI (Fixed Input for New Gradio Versions)
iface = gr.Interface(
fn=run_app,
inputs=gr.Image(type="numpy", label="Webcam Feed"),
outputs=[gr.Image(type="numpy", label="Processed Frame"),
gr.Plot(label="Black Hole Distortion"),
gr.Audio(label="Quantum Sound Wave")],
live=True
)
iface.launch()