Exemple #1
0
 def mock_servers(self):
     server_board = OneUIMockServerBoard()
     bottom_sheet_menu = MDCustomBottomSheet(screen=server_board)
     server_board.bind(close=lambda: bottom_sheet_menu.dismiss())
     server_board.bind(current=lambda _, x: self.selected_server(
         x, lambda: bottom_sheet_menu.dismiss()))
     bottom_sheet_menu.open()
Exemple #2
0
class MainPage(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        global app
        app = MDApp.get_running_app()
        self.app = app
        self.song = self.app.song
        self.playlist_menu = None

    def edit_ui_for_song(self, song=None, playing=False):
        if app.song:
            self.ids.track_length.text = str(
                timedelta(seconds=app.song.length))[3:7]
            self.ids.playback_slider.max = app.song.length
        # app.play_button.update_track_current(current=0)
        if playing:
            theme = app.theme_cls.theme_style.lower()
            app.play_button.source = f'images/stop_{theme}.png'
        if song in app.favorites:
            self.favorite_button.favorited = True
        else:
            self.favorite_button.favorited = False
        if song != self.song:
            self.update_playlist_menu(song=song)
            self.update_cover_art(song)
            self.update_song_info(song)
            self.song = song

    def update_playlist_menu(self, *args, song=None):
        from kivymd.uix.bottomsheet import MDCustomBottomSheet
        self.app.playlist = self.app.db.get_playlist()
        self.playlist_menu = MDCustomBottomSheet(screen=Factory.PlaylistLayout(
            height=dp(65 * len(app.playlist.tracks))), )
        for i in app.playlist.tracks:
            item = PlaylistSongItem(
                text=i.name,
                secondary_text=i.artist,
                on_release=lambda *args, song=i: self.play_from_playlist(song),
            )
            item.song = i
            item.open_song_menu = self.open_song_menu
            removable = (is_removable(i) or i != app.playlist.current_track
                         or (song is not None and i != song))
            # different background for current song
            if not removable:
                item.bg_color = app.theme_cls.primary_color  # rgba('#cbcbcb')
                item.theme_text_color = 'Custom'
                item.text_color = (1, 1, 1, 1)
            self.playlist_menu.screen.songs_grid.add_widget(item)

    def open_song_menu(self, i):
        # adding right-side icons
        from kivymd.uix.bottomsheet import MDListBottomSheet
        song_menu = MDListBottomSheet(radius_from='top')
        # Spotify
        if i.id_spotify:
            from utils import spotify_installed
            msg = "Spotify isn't installed on your device."
            song_menu.add_item(text="Listen on Spotify",
                               callback=lambda x, song=i:
                               (self.open_spotify(i)
                                if spotify_installed() else toast(msg)),
                               icon="spotify")

        # Favorited
        favorited = i in app.favorites
        song_menu.add_item(text="Favorite" if not favorited else "Unfavorite",
                           callback=lambda *args, song=i: app.main_page.
                           favorite_playlist_item(self, song),
                           icon='heart' if favorited else 'heart-outline')

        # Remove
        removable = (is_removable(i) or i != app.playlist.current_track)
        if removable:
            song_menu.add_item(text="Remove from playlist",
                               callback=lambda *args, song=i: app.main_page.
                               remove_playlist_item(self, song),
                               icon='close')
        song_menu.open()

    def open_spotify(self, song):
        from jnius import autoclass
        Intent = autoclass("android.content.Intent")
        Uri = autoclass("android.net.Uri")
        mActivity = autoclass('org.kivy.android.PythonActivity').mActivity

        intent = Intent(Intent.ACTION_VIEW)
        intent.setData(Uri.parse(f"spotify:track:{song.id_spotify}"))
        intent.putExtra(
            Intent.EXTRA_REFERRER,
            Uri.parse(
                "android-app://org.allerter.geniustmusicplayer").toString())
        self.app.play_button.stop_song(release=True)
        mActivity.startActivity(intent)

    def play_from_playlist(self, track):
        # track = app.playlist.get_track(name=selected_item.text)
        if track == app.playlist.current_track:  # track is already playing
            return
        self.playlist_menu.dismiss()
        app.play_button.play_track(track)

    def remove_playlist_item(self, instance, song):
        self.playlist_menu.screen.songs_grid.remove_widget(
            instance.parent.parent)
        app.playlist.remove(song)
        app.db.remove_playlist_track(song)
        self.playlist_menu.dismiss()
        toast('Removed from playlist')

    def favorite_playlist_item(self, instance, song):
        if song in app.favorites:
            favorited = False
            song.date_favorited = None
            app.favorites.remove(song)
            app.db.remove_favorites_track(song)
            msg = 'Song unfavorited'
        else:
            favorited = True
            song.date_favorited = time()
            app.favorites.append(song)
            app.db.add_favorites_track(song)
            msg = 'Song favorited'

        # Correct main favorite button if user (un)favorited item == current song
        if app.song.song_object == song:
            app.main_page.favorite_button.favorited = favorited

        self.playlist_menu.dismiss()
        toast(msg)

    def _get_cover_art_path(self, song):
        filename = str(song.id) + ".png"
        return os.path.join(self.app.images_path, filename)

    def _get_cover_art(self, song):
        cover_art = self._get_cover_art_path(song)
        if not os.path.isfile(cover_art):
            if song.cover_art is not None:
                cover_art = song.cover_art
            else:
                cover_art = 'images/empty_coverart.png'
        return cover_art

    def update_cover_art(self, song):
        cover_art = self._get_cover_art(song)
        self.ids.cover_art.source = cover_art

    def save_cover_art(self, image):
        if (image.texture != Loader.loading_image.texture
                and image.source != 'images/empty_coverart'):
            cover_art = self._get_cover_art_path(self.app.song.song_object)
            if not os.path.isfile(cover_art):
                image.texture.save(cover_art, flipped=False)
                Logger.debug('CACHE: Saved %s', cover_art)

    def download_next_coverart(self):
        song = self.app.playlist.preview_next()
        if song.cover_art and not os.path.isfile(
                self._get_cover_art_path(song)):
            image = Loader.image(song.cover_art)
            image.source = None
            image.bind(on_load=self.save_cover_art)

    def update_song_info(self, song):
        self.ids.title.text = song.name
        self.ids.artist.text = song.artist
Exemple #3
0
class RoadGANGUI(MDApp):
    title = "RoadGAN"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Window.bind(on_keyboard=self.events)
        self.main_win = Builder.load_string(KV)
        self.manager_open = False
        self.manager = None
        self.settings_open = False
        self.scaner = False
        self.input_path = None
        self.output_path = './'
        self.weather = "clear"
        self.day_time = "daylight"
        self.urban_style = "Munster"
        self.weather_conditions = [{
            "icon": "weather-sunny",
            "text": "clear"
        }, {
            "icon": 'weather-fog',
            "text": "fog"
        }, {
            "icon": "weather-pouring",
            "text": 'rain'
        }, {
            "icon": "weather-snowy",
            "text": 'snow'
        }, {
            "icon": "weather-cloudy",
            "text": 'clouds'
        }]
        self.urban_styles = [{
            "icon": "home-city",
            "text": 'Munster'
        }, {
            "icon": "home-city",
            "text": 'Los Angeles'
        }, {
            "icon": "home-city",
            "text": 'Paris'
        }, {
            "icon": "home-city",
            "text": 'Boston'
        }, {
            "icon": "home-city",
            "text": 'Beijing'
        }]
        self.day_times = [{
            "icon": "weather-sunset-up",
            "text": 'dawn'
        }, {
            "icon": "weather-sunny",
            "text": 'daylight'
        }, {
            "icon": "weather-sunset",
            "text": 'dusk'
        }, {
            "icon": "weather-night",
            "text": 'night'
        }]
        self.inference_thread = Thread(target=self.launch_conversion)

    def build(self):
        self.theme_cls.primary_palette = "Amber"
        return self.main_win

    def open_settings(self):
        '''Create and open the settings bottom panel. This will add dropdown menus for selecting 
        weather conditions, daylight conditions and the urban style.'''
        self.settings = MDCustomBottomSheet(screen=Factory.SettingsScenario())
        self.weather_menu = MDDropdownMenu(
            caller=self.settings.screen.ids.weather_cond,
            items=self.weather_conditions,
            position="auto",
            callback=self.update_weather_condition,
            width_mult=3,
        )
        self.urban_style_menu = MDDropdownMenu(
            caller=self.settings.screen.ids.urban_style,
            items=self.urban_styles,
            position="auto",
            callback=self.update_urban_style,
            width_mult=3,
        )
        self.day_time_menu = MDDropdownMenu(
            caller=self.settings.screen.ids.day_time,
            items=self.day_times,
            position="auto",
            callback=self.update_day_time,
            width_mult=3,
        )
        self.settings.screen.ids.weather_cond.set_item(self.weather)
        self.settings.screen.ids.day_time.set_item(self.day_time)
        self.settings.screen.ids.urban_style.set_item(self.urban_style)
        self.settings.open()

    def close_settings(self):
        '''Close the settings bottom panel.'''
        if self.settings:
            self.settings.dismiss()

    def on_checkbox_active(self, checkbox, value):
        '''Called when clicking on the segmentation checkbox.
        
        type: value: bool;
        param: value: active state of the checkbox;
        type: checkbox: MDCheckbox;
        param: checkbox: clicked checkbox instance;
        '''
        self.scaner = value

    def update_weather_condition(self, instance):
        '''Set weather conditions according to the chosen item in the GUI.
        
        type: instance: MDDropDownMenuItem;
        param: instance: The chosen menu item;'''
        if instance.text != 'clear':
            self.weather = instance.text
        self.settings.screen.ids.weather_cond.set_item(instance.text)

    def update_day_time(self, instance):
        '''Set daylight conditions according to the chosen item in the GUI.
        
        type: instance: MDDropDownMenuItem;
        param: instance: The chosen menu item;'''
        self.day_time = instance.text
        self.settings.screen.ids.day_time.set_item(self.day_time)

    def update_urban_style(self, instance):
        '''Set urban style according to the chosen item in the GUI.
        
        type: instance: MDDropDownMenuItem;
        param: instance: The chosen menu item;'''
        self.urban_style = instance.text
        self.settings.screen.ids.urban_style.set_item(self.urban_style)

    def file_manager_open(self, output=False):
        '''Open a new file manager for selecting either the input segmentation video or 
        the output location for the synthesized video.
        
        :type output: bool;
        :param output: If set to True, will open the file manager for selecting the output location;'''
        if output:
            func = self.select_output_path
            path = self.output_path
        else:
            func = self.select_input_path
            path = self.input_path
        if not self.manager_open:
            self.manager = MDFileManager(exit_manager=self.exit_manager,
                                         select_path=func)
            self.manager.ext = ['.mp4', '.avi']
            self.manager.show(os.path.dirname(path)
                              if path else '/')  # output manager to the screen
            self.manager_open = True

    def select_input_path(self, path):
        '''It will be called when you click on the file name
        or the catalog selection button.

        :type path: str;
        :param path: path to the selected directory or file;
        '''
        self.exit_manager()
        self.input_path = path
        toast(path)

    def select_output_path(self, path):
        '''It will be called when you click on the file name
        or the catalog selection button.

        :type path: str;
        :param path: path to the selected directory or file;
        '''
        self.exit_manager()
        self.output_path = path
        toast(path)

    def exit_manager(self, *args):
        '''Called when the user reaches the root of the directory tree.'''
        self.manager.close()
        self.manager_open = False

    def process_inference(self):
        '''Called when the user clicks on the floating play button. 
        Launches the conversion in a separated thread using vid2vid and HAL.'''
        if self.inference_thread.is_alive():
            self.inference_thread.join()
        self.main_win.ids.spinner.active = True
        self.inference_thread.start()

    def launch_conversion(self):
        '''Converts the segmentation video selected in the GUI using vid2vid and HAL.
        It also takes into account the selected reference image and weather/daylight conditions.'''
        video_name = decompose_video(self.input_path,
                                     process_pal=not (self.scaner))
        frame_dir_path = os.path.join(os.path.dirname(self.input_path),
                                      video_name)
        os.makedirs('./tmp')
        save_path = os.path.join('./tmp', video_name + "_converted")
        infer_images(frame_dir_path, os.path.abspath(self.select_style_img()),
                     save_path, self.scaner)
        weather = "" if self.weather == "clear" else self.weather
        os.chdir('attribute_hallucination/')
        os.system(
            "export MKL_SERVICE_FORCE_INTEL=1 && python generate_style.py --video_path "
            + os.path.join('..', save_path) + " --attributes " +
            self.day_time + " " + weather)
        os.system(
            "export MKL_SERVICE_FORCE_INTEL=1 && python style_transfer.py --video_folder "
            + os.path.join('..', save_path))
        os.chdir('..')
        recompose_video(
            './tmp/' + video_name + "_converted_stylized/",
            os.path.join(self.output_path, video_name + "_converted.mp4"))
        shutil.rmtree('./tmp')
        self.main_win.ids.spinner.active = False
        toast('Inference finished!')

    def select_style_img(self):
        '''Return the path to the style image corresponding to the scenario chosen
        by the user.'''
        style = self.urban_style.lower().replace(" ", "_")
        return os.path.join("inference/refs_img/images/", f"{style}_clear")

    def events(self, instance, keyboard, keycode, text, modifiers):
        '''Called when buttons are pressed on the keyboard while the file manager is open.'''
        if keyboard in (1001, 27):
            if self.manager_open:
                self.manager.back()
        return True