File size: 1,351 Bytes
cef1078
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import cv2, os
import mediapipe as mp

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)

# ensure slides folder exists
if not os.path.exists("slides"):
    os.makedirs("slides")

slides = sorted([f"slides/{img}" for img in os.listdir("slides")])
index = 0

def detect(frame):
    global index, slides
    slides = sorted([f"slides/{img}" for img in os.listdir("slides")])
    if not slides:
        import numpy as np
        blank = np.zeros((600,800,3),dtype=np.uint8)
        cv2.putText(blank, "Upload slide images in /slides", (50,300),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
        return blank

    h,w,c = frame.shape
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(frame_rgb)

    if result.multi_hand_landmarks:
        lm = result.multi_hand_landmarks[0]
        x = lm.landmark[mp_hands.HandLandmark.WRIST].x * w
        if x < w*0.3:
            index = max(0, index-1)
        elif x > w*0.7:
            index = min(len(slides)-1, index+1)

    slide = cv2.imread(slides[index])
    slide = cv2.cvtColor(slide, cv2.COLOR_BGR2RGB)
    return slide

demo = gr.Interface(fn=detect,
                    inputs=gr.Image(source="webcam", streaming=True),
                    outputs="image",
                    live=True)

demo.launch()