Git Product home page Git Product logo

colorfy's Introduction

Colorfy

A small program written in Python 3 that sets the color of your LED-strip to the background color that Spotify sets when playing on a Chromecast. It analyzes the album artwork and computes the correct background color (same as Spotify sets) in about 80 % of the cases.

The program uses k-means clustering to find distinct colors in the artwork and then computes a colorfulness index as defined by Hasler and Süsstrunk (2003) for each of the colors. The color with the highest index is then set to the LED-strip if it is greater than or equal to a given colorfulness tolerance.

Images

Spotify background color

Web app

Simple web app built with Flask to easily switch between setting your LED-strip to the color based on your current Spotify playback, set manually using a color picker or if it should be turned off.

Running locally

Connect LED-strip to Raspberry Pi

Many different LED-strips can be used with this program. The only requirement is that it needs to connect with three data pins to the Raspberry Pi, i.e. one data pin for each of the color channels R, G and B.

I used the IKEA Dioder LED-strip which I connected to my Raspberry Pi 3 model B+ by following this guide.

For information about how to wire and set up WS281X leds you can refer to this guide but don't run the setup commands there as the required library is now installed by using pip rather than the manual way in the guide. You can also find the library used here.

Clone/download repository

When the hardware is set up, you will need to download or clone the repository, the latter can be done by typing

git clone https://github.com/davidkrantz/Colorfy.git

in your terminal.

Create a Spotify application

  1. Go to your Spotify dashboard.
  2. Create a new application.
  3. Press EDIT SETTINGS and set the redirect URI to for example http://localhost/ and save it.

Set environment variables

These are found inside your Spotify application and are preferably added to ~/.profile (or something similar like ~/.bash_login or ~/.bashrc) on your Raspberry Pi.

export SPOTIPY_CLIENT_ID=''
export SPOTIPY_CLIENT_SECRET=''
export SPOTIPY_REDIRECT_URI=''

Get refresh token

  1. Run the included spotify_setup.py script with a string as argument. This can be any string, but preferably something your will remember (for example your Spotify username to which you wish to connect the Spotify application to) since this will make it easy to find the generated refresh token if you lose it. So for example:
python spotify_setup.py spotify_username
  1. A page will open. Accept the terms and then copy the URL you are redirected to into the terminal and hit enter.

  2. Your refresh token will then be printed in the terminal. It will also be saved in a file called .cache-{spotify_username} inside the active directory.

  3. Set the refresh token as an environment variable just as the ones before with export SPOTIPY_REFRESH_TOKEN=''.

Set up config file

  1. Copy and rename config.ini.default to config.ini.

  2. Edit config.ini such that the used GPIO pins and Chromecast name matches yours, for example:

[GPIO PINS]
red_pin = 17
green_pin = 22
blue_pin = 24

[CHROMECAST]
name = Chromecast Krantz

To use a WS281X led strip (Neopixels) you need to set the is_active value in config.ini to True, the led_count value to the number of leds in your strip and the led_pin to the GPIO pin you connected the data input of your led strip to. The other values under [WS281X] are optional and set as default.

To use any WLED device you just need to set is_active to True in config.ini and provide the IP address of that device. Currently, just one device at a time is supported.

Run it

  1. First you will have to install the needed packages. These are listed in the requirements.txt file and should be easily installed using pip with
pip3 install -r /path/to/requirements.txt

NOTE: The Spotipy package needs to be installed directly from GitHub, otherwise an old version will be installed

pip3 install git+https://github.com/plamere/spotipy.git --upgrade
  1. Initialize pigpiod on your Raspberry Pi with sudo pigpiod

  2. Run main.py together with some suitable arguments, for example

python3 main.py -k 8 -t 10 -s 100 100

which will resize the album artworks to 100x100, find 8 distinct colors and return the most colorful color if it is greater than or equal to the colorfulness tolerance 10. If no arguments are inputted python3 main.py, the default values will be used. The default values are the arguments which gave me the best result with regards to accuracy and computational time, which is why I recommend using them. But feel free to experiment with these to try to improve the accuracy!

Starting and updating on reboot

The two previous steps can be automated by doing the following:

  1. Run sudo systemctl enable pigpiod once on your Raspberry Pi.

  2. Copy and rename start.sh.default to start.sh.

  3. Edit start.sh to your liking and such that it works on your Raspberry Pi.

  4. Add @reboot . $HOME/path/to/env/var; sh /path/to/project/start.sh > /path/to/project/logs/log.txt 2>&1 to crontab -e. For the logging to work you will have create a directory with mkdir logs inside the project folder. A file called log.txt in /path/to/project/logs will then contain the logs which can be used for debugging.

If you choose to run the web server, it can later be found on the address IPTOYOURRPI:5000.

colorfy's People

Contributors

davidkrantz avatar pawker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

colorfy's Issues

Change clustering method used to find distinct colors in album artwork

Right now the K-means clustering method is used to find K distinct colors from the album artwork. This method has its flaws, more specifically:

  • Need to know K (how many distinct colors the image have)
  • Always learns convex clusters
  • Sensitive to outliers

Therefore, the accuracy of the system could potentially be significantly increased by changing the clustering method. Potential clustering methods include DBSCAN and HDBSCAN, where the latter probably is the better choice as it allows clusters of different densities, is efficient and only requires one parameter, namely the minimum cluster size.

The clustering takes place in the file spotify_background_color.py in the method best_color. When evaluating different clustering methods, the plotting feature of this method might be of use. This can be activated by simply setting the argument plot=True.

licence?

Hi David,

Thank you for publishing this code. However, I wish to slightly adapt spotify_background_color.py into my own use-case (for use with AppDaemon in Home-Assistant).

I noticed there was no LICENSE file in the root, so I would like to clarify if the code is available under an MIT (or similar) license so I can re-purpose it for my use-case for my smart home?

Inconsistent variable name

Great job with this project, I be using it for understanding the spotify method to define the background color. Thanks!!

Reading your code, I believe that you wanted define this variable name as rg_mean, rg_std and not rb_mean, rb_std, right?

rb_mean, rb_std = (np.mean(rg), np.std(rg))

Thanks for great work

Hi

Just want to thank you for this great project. It works wonderful.
I've played with k values for kmeans clusters count, and for my 4 works better. I've changed implementation from sklearn to scipy for less dependencies, so maybe thats the difference. Also I've extracted logic and put it inside Home Assistant custom component ( https://community.home-assistant.io/t/change-your-rgb-lights-color-according-to-spotify-album-background/94885 ), added link in code comment. I hope that's ok. Thanks again for great project !

Error runing main.py

Gdk-CRITICAL **: 21:19:07.749: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed

after do all the following instructions, I'm getting this error when I run the:
$ sudo python3 main.py -k 8 -t 10 -s 100 100

Any guess?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.