File size: 2,575 Bytes
5f3e574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c25b53
5f3e574
cf0334c
 
 
 
 
 
 
5f3e574
cf0334c
 
 
 
 
5f3e574
cf0334c
 
 
 
 
5f3e574
1c25b53
5f3e574
1c25b53
 
 
 
 
 
 
5f3e574
 
1c25b53
5f3e574
 
 
 
1c25b53
f84c3c4
5f3e574
e64f337
1c25b53
 
cf0334c
 
 
 
1c25b53
 
5f3e574
 
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
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()