Skip to content

Commit

Permalink
[PA-14] - Replacing config by yml (#15)
Browse files Browse the repository at this point in the history
* [PA-14]-Replacing config.py by config.yml
* [PA-14]-Updating documentation to reflect config.yml
  • Loading branch information
fortin-alex authored Apr 16, 2021
1 parent 01d133e commit a39ab5d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 50 deletions.
12 changes: 10 additions & 2 deletions code/peoples_anthem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions code/utils/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import vlc
from spotipy.oauth2 import SpotifyClientCredentials

from config import PLAYLIST, SECRET

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -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)
29 changes: 14 additions & 15 deletions code/config.py → conf/config.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
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.
#
# The key of the dictionary must match the person's name used for training the face
# 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
59 changes: 29 additions & 30 deletions doc/setting-up-spotify.md
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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:`.

0 comments on commit a39ab5d

Please sign in to comment.