Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,35 +19,45 @@ def generate_wave(frequency=220, duration=3):
|
|
| 19 |
waveform = chirp(t, f0=frequency, f1=frequency*2, t1=duration, method='quadratic')
|
| 20 |
return waveform, sample_rate
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def process_frame(frame):
|
| 24 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 25 |
results = hands.process(frame_rgb)
|
|
|
|
| 26 |
|
| 27 |
if results.multi_hand_landmarks:
|
| 28 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 29 |
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 30 |
x = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x
|
| 31 |
-
|
| 32 |
-
rotation = (x - 0.5) * 360 # Rotate black hole based on hand position
|
| 33 |
-
return frame, rotation
|
| 34 |
|
| 35 |
-
return frame,
|
| 36 |
|
|
|
|
| 37 |
def black_hole_visualization(rotation):
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return fig
|
| 41 |
|
|
|
|
| 42 |
def run_app(webcam):
|
| 43 |
frame, rotation = process_frame(webcam)
|
| 44 |
black_hole_figure = black_hole_visualization(rotation)
|
| 45 |
wave_data, sr = generate_wave(frequency=200 + rotation)
|
|
|
|
| 46 |
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), black_hole_figure, (wave_data, sr)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
iface.launch()
|
|
|
|
| 19 |
waveform = chirp(t, f0=frequency, f1=frequency*2, t1=duration, method='quadratic')
|
| 20 |
return waveform, sample_rate
|
| 21 |
|
| 22 |
+
# Process Hand Tracking
|
| 23 |
def process_frame(frame):
|
| 24 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 25 |
results = hands.process(frame_rgb)
|
| 26 |
+
rotation = 0
|
| 27 |
|
| 28 |
if results.multi_hand_landmarks:
|
| 29 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 30 |
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 31 |
x = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x
|
| 32 |
+
rotation = (x - 0.5) * 360 # Rotate black hole based on hand movement
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
return frame, rotation # Return frame with hand-tracking + rotation value
|
| 35 |
|
| 36 |
+
# Create Black Hole Visualization
|
| 37 |
def black_hole_visualization(rotation):
|
| 38 |
+
x = np.linspace(-5, 5, 100)
|
| 39 |
+
y = np.linspace(-5, 5, 100)
|
| 40 |
+
X, Y = np.meshgrid(x, y)
|
| 41 |
+
Z = np.sin(X**2 + Y**2 - rotation * 0.01) # Distorted wave effect
|
| 42 |
+
|
| 43 |
+
fig = go.Figure(data=[go.Surface(z=Z)])
|
| 44 |
+
fig.update_layout(title="Black Hole Distortion", autosize=True)
|
| 45 |
return fig
|
| 46 |
|
| 47 |
+
# Main function integrating webcam, black hole, and sound
|
| 48 |
def run_app(webcam):
|
| 49 |
frame, rotation = process_frame(webcam)
|
| 50 |
black_hole_figure = black_hole_visualization(rotation)
|
| 51 |
wave_data, sr = generate_wave(frequency=200 + rotation)
|
| 52 |
+
|
| 53 |
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), black_hole_figure, (wave_data, sr)
|
| 54 |
|
| 55 |
+
# Launch Gradio UI
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=run_app,
|
| 58 |
+
inputs=gr.Image(source="webcam", streaming=True),
|
| 59 |
+
outputs=["image", "plot", "audio"],
|
| 60 |
+
live=True
|
| 61 |
+
)
|
| 62 |
|
| 63 |
iface.launch()
|