This program uses Spotify API and websockets to control Raspberry Pi and display song album art on LED strip lights.
- Node
- Express.js
- PostgreSQL
- Liquid.js
- socket.io
- Spotify API
- Raspberry Pi
A K-Means Clustering algorithm was implemented to determine the k dominant colors for a given image.
Given a collection of points in D-dimensional space (music preferences, birthday, etc.), natural clusters form. Such is the case with dominant colors in an image. Clustering can be applied to a weighted graph G=(V,E) where each cluster has a distance from another cluster
- Convert the image into a 2D array of LAB values.
- At random, select k points from the 2D array which are the k centroids.
- For each point p in the array:
- Determine the Euclidean distance from p to each centroid.
- Add the point p to the centroid cluster with the minimum Euclidean distance.
- If more than 2 of the resulting clusters have length 0 (i.e. when the album art has very little color variation), restart kmeans to pick new random centroids. Else, replace clusters of length 0 with the cluster of max length to avoid recursing for a long period of time.
- Determine the mean of each of the k clusters, these are the new centroids.
- If the new centroids = old centroids, return the resulting cluster. Else, repeat from step 3.
- Create an account and login to https://developer.spotify.com/
- Create the SpotiPi app
- Note the app Client ID and Client Secret
- Set the redirect URI to
http://[RASPBERRY PI IP]:8888/
This database stores the results of the kmeans algorithm for a given Spotify albumID to avoid recalculations in the future.
- Download PostgreSQL from https://www.postgresql.org/download/
- Open Postgres. There should be 3 default databases, [root name], postgres, and template0. Double click
[root name]
or runpsql -p5432 "[root name]"
in the terminal. - Create the
spotipi
database.
CREATE DATABASE spotipi;
- Exit out of
[root name]
by running\quit
. Open the newly create database by double clickingspotipi
in Postgres or runningpsql -p5432 spotipi
in the terminal. - Create the table
colors
.
CREATE TABLE colors (
albumID VARCHAR(255) UNIQUE,
colors VARCHAR(255)[]
);
- Exit using
\quit
-
ssh [RASPBERRY PI IP]
-
git clone https://github.com/ajtadeo/Spotipi.git
-
cd Spotipi
-
To use
node-canvas
and perform server-side image analysis, the following requirements must be met.- Node version 16.16.0. Check your node version with
node -v
. - Install the following packages:
- MacOS:
brew install pkg-config cairo pango libpng jpeg giflib librsvg
- Windows:
sudo apt-get pkg-config cairo pango libpng jpeg giflib librsvg
- MacOS:
NOTE: If you get the following error
gyp: Call to 'node -e "require('nan')"' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
, you must install the package nan vianpm i nan
before installing node-canvas. - Node version 16.16.0. Check your node version with
-
npm i
-
touch .env
-
Edit
.env
to include the following variables:
SPOTIFY_CLIENT_ID="s3cret" /* Generated from https://developer.spotify.com/ */
SPOTIFY_CLIENT_SECRET="s3cret" /* Generated from https://developer.spotify.com/ */
SPOTIFY_REDIRECT_URI="http://[HOSTNAME]:[PORT]/auth/callback/"
SESSION_SECRET="s3cret"
PGUSER="root" /* machine's currently logged in user */
PGPASSWORD="root password" /* machine's currently logged in user password */
PGDATABASE="spotipi"
PGHOST="localhost"
PGPORT="5432"
- Open Postgres.
pm2 start app.js
- Open
[RASPBERRY PI IP]:8888
in a web browser
- https://dev.to/bogdaaamn/run-your-nodejs-application-on-a-headless-raspberry-pi-4jnn
- https://sonyarouje.com/2010/12/17/approach-to-count-dominant-colors-in-a-image/
- https://tatasz.github.io/dominant_colors/
- https://dordnung.de/raspberrypi-ledstrip/
- https://towardsdatascience.com/extracting-colours-from-an-image-using-k-means-clustering-9616348712be