From a39ab5d5f104f2dd1d1aba1fa59da93b4655f86e Mon Sep 17 00:00:00 2001 From: Alex Fortin Date: Fri, 16 Apr 2021 13:02:02 -0400 Subject: [PATCH] [PA-14] - Replacing config by yml (#15) * [PA-14]-Replacing config.py by config.yml * [PA-14]-Updating documentation to reflect config.yml --- code/peoples_anthem.py | 12 +++++-- code/utils/players.py | 18 ++++++++-- code/config.py => conf/config.yml | 29 ++++++++------- doc/setting-up-spotify.md | 59 +++++++++++++++---------------- 4 files changed, 68 insertions(+), 50 deletions(-) rename code/config.py => conf/config.yml (56%) diff --git a/code/peoples_anthem.py b/code/peoples_anthem.py index ec7b738..f74f7ae 100644 --- a/code/peoples_anthem.py +++ b/code/peoples_anthem.py @@ -9,9 +9,9 @@ import joblib import numpy as np import torch +import yaml from PIL import Image -from config import PLAYLIST, SECRET from utils.feature_extractor import FeatureExtractor from utils.frame_extractor import FrameExtractor from utils.players import SpotifyPlayer @@ -28,6 +28,12 @@ FACE_DETECTION_MODEL_FILEPATH = "/home/pi/models-cache/face-cascade/haarcascade_frontalface_default.xml" face_cascade = cv2.CascadeClassifier(FACE_DETECTION_MODEL_FILEPATH) +# Loading pointer to Spotify playlists +root_path = Path(__file__).parents[1] +config_path = root_path.joinpath("conf/config.yml") +with open(config_path) as f: + conf = yaml.safe_load(f) + class PeoplesAnthem(object): def __init__(self, model_path: str): @@ -177,8 +183,10 @@ def recognize_and_play_spotify(self): if face_id.lower() == "misc": continue else: + secret = conf.get("SECRET", None) + playlist = conf.get("PLAYLIST", None) SpotifyPlayer.get_and_play_tracks( - secret_dict=SECRET, playlist_uri_dict=PLAYLIST, user=face_id, n_tracks=N_TRACKS + secret_dict=secret, playlist_uri_dict=playlist, user=face_id, n_tracks=N_TRACKS ) cap = self.reset_video_capture(cap=cap) diff --git a/code/utils/players.py b/code/utils/players.py index d2a6f06..56eacf9 100644 --- a/code/utils/players.py +++ b/code/utils/players.py @@ -9,8 +9,6 @@ import vlc from spotipy.oauth2 import SpotifyClientCredentials -from config import PLAYLIST, SECRET - logger = logging.getLogger(__name__) @@ -125,4 +123,18 @@ def play_n_seconds(tracks: Union[str, List], n: int): if __name__ == "__main__": - SpotifyPlayer.get_and_play_tracks(secret_dict=SECRET, playlist_uri_dict=PLAYLIST, user="alex", n_tracks=2) + from pathlib import Path + + import yaml + + root_path = Path(__file__).parents[2] + config_path = root_path.joinpath("conf/config.yml") + + with open(config_path) as f: + conf = yaml.safe_load(f) + + secret = conf.get("SECRET", None) + playlist = conf.get("PLAYLIST", None) + user = list(playlist.keys())[0] + + SpotifyPlayer.get_and_play_tracks(secret_dict=secret, playlist_uri_dict=playlist, user=user, n_tracks=2) diff --git a/code/config.py b/conf/config.yml similarity index 56% rename from code/config.py rename to conf/config.yml index 31007e4..e9c6e07 100644 --- a/code/config.py +++ b/conf/config.yml @@ -1,5 +1,3 @@ -from pathlib import Path - # SPOTIFY CLIENT AND SECRET # This allows the program to use different spotify credential(s) to query spotify playlists using these credentials. # @@ -7,23 +5,24 @@ # recognition algorithm. The config below is valid if your model was trained to detect # "alice" and "bob". -SECRET = dict( - alice=dict(client_id="alice-spotify-client-id", client_secret="alice-spotify-client-secret"), - bob=dict(client_id="bob-spotify-client-id", client_secret="bob-spotify-client-secret"), -) +SECRET: + alice: + client_id: alice-spotify-client-id + client_secret: alice-spotify-client-secret + bob: + client_id: bob-spotify-client-id + client_secret: bob-spotify-client-secret # SPOTIFY PLAYLIST URI # Upon recognizing the face of these user, the program will play these spotify playlist. # You can obtain a spotify playlist's URI by right-clicking a playlist and selecting: "Share --> Copy spotify URI" -PLAYLIST = dict( - alice="spotify:playlist:alice", - bob="spotify:playlist:bob", -) +PLAYLIST: + alice: spotify:playlist:alice + bob: spotify:playlist:bob # PARAMETERS FOR PLAYING A LOCAL SONG -_music_path = Path("/home/pi/Music") -PEOPLE_SONG = dict( - alice=_music_path.joinpath("alice.mp3"), - bob=_music_path.joinpath("bob.mp3"), -) +# As an alternative to playing your spotify music, people's anthem can play a local song +PEOPLE_SONG: + alice: /home/pi/Music/alice.mp3 + bob: /home/pi/Music/bob.mp3 diff --git a/doc/setting-up-spotify.md b/doc/setting-up-spotify.md index c24466e..7b7d0f1 100644 --- a/doc/setting-up-spotify.md +++ b/doc/setting-up-spotify.md @@ -1,36 +1,39 @@ # Setting up Spotify with people's anthem In this documentation, we review how to get the appropriate Spotify credentials so that you can play music from your playlist with `People's anthem`. -We will see: -1. What file to edit +We will cover: +1. What files to edit 2. How to get your Spotify `client_id` 3. How to get your Spotify `client_secret` 4. How to get your Spotify playlist URI -## 1. What file to edit -Start by opening the file `peoples-anthem/code/config.py`. +## 1. What files to edit +Start by opening the file `peoples-anthem/conf/config.yml`. This file is used in `peoples-anthem/code/peoples_anthem.py` and its information is passed to the Spotify API's via the [Spotipy](https://spotipy.readthedocs.io/en/2.16.1/) python package. ## 2. How to get your Spotify `client_id` -In this section, we are going to get the `client_id` for each person that we will be recognizing. +In this section, we are going to get the `client_id` for each person that we will be recognized. The `client_id` is essentially the same as your username but it looks like a random string of characters. -In the example below, `config.py` is configured to work for 2 people: `alice` and `bob`. +In the example below, `peoples-anthem/conf/config.yml` is configured to work for 2 people: `alice` and `bob`. Note: The name used here (`alice` and `bob`) must match the name of the directories in `peoples-anthem/data/train/`. -We will see how to update this section of the code: -```python -SECRET = dict( - alice=dict(client_id="alice-spotify-client-id", client_secret="alice-spotify-client-secret"), - bob=dict(client_id="bob-spotify-client-id", client_secret="bob-spotify-client-secret"), -) +We will see how to update this section of the config file: +```yaml +SECRET: + alice: + client_id: alice-spotify-client-id + client_secret: alice-spotify-client-secret + bob: + client_id: bob-spotify-client-id + client_secret: bob-spotify-client-secret ``` ### 2.1 Go to https://www.spotify.com/, scroll down and click on "Developers" to get to the Developers menu ![developers-communities-hint](../assets/spotify-setup/developers-communities.png "Developers") -### 2.2 From there, click on _Dashboard_ and log in with your normal username and password +### 2.2 From there, click on _Dashboard_ and log in with your usual Spotify username and password ![Dashboard-hint](../assets/spotify-setup/dashboard.png "Dashboard") ![log-in-hint](../assets/spotify-setup/login.png "Log in") @@ -40,14 +43,14 @@ SECRET = dict( ![create-app-hint](../assets/spotify-setup/create-app.png "Create App") -### 2.4 In `config.py`, replace `alice-spotify-client-id` by your `Client ID` +### 2.4 In `peoples-anthem/conf/config.yml`, replace `alice-spotify-client-id` by your `Client ID` ![client-id-hint](../assets/spotify-setup/client-id.png "Client id") Note: If desired, `alice` and `bob` can share the same `client_id`. ## 3. How to get your Spotify `client_secret` -### 3.1 From step 2.4, click on `SHOW CLIENT SECRET` and replace `alice-spotify-client-secret`, in `config.py`, by your `Client Secret` +### 3.1 From step 2.4, click on `SHOW CLIENT SECRET` and replace `alice-spotify-client-secret`, in `config.yml`, by your `Client Secret` ![show-client-secret-hint](../assets/spotify-setup/show-client-secret.png "Show client secret") ![client-secret-hint](../assets/spotify-setup/client-secret.png "Client secret") @@ -60,13 +63,12 @@ Congrats! At this point you are set-up to play Spotify from `people's anthem`! Before you can start using `people's anthem`, there is one last thing to do. We need to tell `people's anthem` what playlist to play for each person. -For this, we need to modify this section of `peoples-anthem/code/config.py`: +For this, we need to modify this section of `peoples-anthem/conf/config.yml`: -```python -PLAYLIST = dict( - alice="spotify:playlist:alice", - bob="spotify:playlist:bob", -) +```yaml +PLAYLIST: + alice: spotify:playlist:alice + bob: spotify:playlist:bob ``` ### 4.1 Go to the Spotify app and find a good playlist @@ -76,17 +78,14 @@ PLAYLIST = dict( ![spotify-uri-hint](../assets/spotify-setup/spotify-uri.png "Spotify URI") -In this example, the Spotify playlist is this one: `spotify:playlist:37i9dQZF1DZ06evO24IA7u`. +In this example, the Spotify URI for this playlist is this one: `spotify:playlist:37i9dQZF1DZ06evO24IA7u` -### 4.3 In `config.py`, replace `spotify:playlist:alice` by your `Spotify URI` +### 4.3 In `peoples-anthem/conf/config.yml`, replace `spotify:playlist:alice` by the `Spotify URI` -```python -PLAYLIST = dict( - alice="spotify:playlist:37i9dQZF1DZ06evO24IA7u", - bob="spotify:playlist:34ymV2IwnxzWjILCycL0Ki", -) +```yaml +PLAYLIST: + alice: spotify:playlist:37i9dQZF1DZ06evO24IA7u + bob: spotify:playlist:34ymV2IwnxzWjILCycL0Ki ``` -Note: If desired, `alice` and `bob` can share the same `Spotify URI`. - Note: `People's anthem` is set up to play Spotify playlists. This is in contrast with Spotify artist radio, etc. Make sure that your `Spotify URI` starts with: `spotify:playlist:`.