File size: 3,573 Bytes
3af34ae
c4e17d6
3af34ae
c4e17d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a04f9a
c4e17d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a1e9dd
c4e17d6
 
 
3a1e9dd
3af34ae
 
 
3a1e9dd
 
 
3af34ae
3a1e9dd
 
 
 
 
c4e17d6
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from interactive_pipe import Control, interactive_pipeline, interactive
from interactive_pipe.data_objects.image import Image
import argparse
from pathlib import Path
import cv2

root = Path(__file__).parent
img_folder = root/"images"
audio_folder = root/"audio"
TRACK = "track"
IMAGE = ICON = "image"
CAPTION = "caption"
PROMPT = "prompt"
PROMPT_STYLE = "a cute crayon drawing of "
PROMPT_EXTRA = " on a white background, using a style from a cartoon book for baby kids"

# Audio generated by https://huggingface.co/spaces/facebook/MusicGen
# Elephant : a short savanah jungle music like the lion's king akuna matata with elephants barking
# Snail: a short instrumental country song with banjo without lyrics
# Rabbit: we will rock you by queens but played with ukulele  without lyrics
TRACK_DICT = {
    "elephant": {
        TRACK: audio_folder/"elephant.mp4",
        CAPTION: "ELEPHANT",
        PROMPT: "a smiling elephant walking in the sunny yellow savana",
    },
    "snail": {
        TRACK: audio_folder/"snail.mp4",
        CAPTION: "SNAIL",
        PROMPT: "a cute yellow and orange snail with two eyes slowly walking on the green grass"
    },
    "rabbit": {
        TRACK: audio_folder/"rabbit.mp4",
        CAPTION: "RABBIT",
        PROMPT: "a smiling funny little rabbit in the green grass",
    },
    "pause": {
        TRACK: None,
        PROMPT: "a cute sleeping cat",
        CAPTION: "...zzzz"
    }
}

for item_name, element in TRACK_DICT.items():
    TRACK_DICT[item_name][IMAGE] = img_folder/(item_name+".png")
ICONS = [it[ICON] for key, it in TRACK_DICT.items()]


@interactive(
    song=Control("elephant", list(TRACK_DICT.keys()), icons=ICONS)
)
def song_choice(global_params={}, song="elephant"):
    global_params[TRACK] = song


def play_song(global_params={}):
    song = global_params.get(TRACK, None)
    first_exec = global_params.get("first_exec", True)
    if not first_exec:
        audio_track = TRACK_DICT[song][TRACK]
        if audio_track is None:
            global_params["__stop"]()
        else:
            global_params["__set_audio"](audio_track)
            global_params["__play"]()
    else:
        global_params["first_exec"] = False


def image_choice(global_params={}):
    song = global_params.get(TRACK, list(TRACK_DICT.keys())[0])
    img = Image.from_file(TRACK_DICT[song][IMAGE]).data
    max_height = 300  # Raspberry pi with a 7" touchscreen
    h, w, _c = img.shape
    if h > max_height:
        img = cv2.resize(img, (w*max_height//h, max_height))
        h, w, _c = img.shape
    caption = TRACK_DICT[song][CAPTION]
    global_params["__output_styles"]["img_out"] = {
        "title": caption
    }  # discard auto titling
    return img


def sample_pipeline():
    song_choice()
    play_song()
    img_out = image_choice()
    return img_out


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Music and image player')
    parser.add_argument('-b', '--backend', type=str,
                        default='gradio', choices=['gradio', 'qt'])
    args = parser.parse_args()
    markdown_description = "# πŸ” READ TUTORIAL HERE\n"
    markdown_description += 'THIS INTERACTIVE PIPE IS INTENDED FOR THE QT BACKEND AND TO BE DEPLOYED ON A RASPBERRY PI FOR KIDS (touchscreen + full screen)\n\n'
    markdown_description += "```python\n"+open(__file__, 'r').read()+"```\n"
    app = interactive_pipeline(
        gui=args.backend,
        cache=False,
        audio=True,
        size="fullscreen",
        markdown_description=markdown_description)(sample_pipeline)
    app()