Skip to content

Instantly share code, notes, and snippets.

@agrimrules
Last active January 26, 2017 05:28
Show Gist options
  • Save agrimrules/0529760855d9b8c9676f513d922fac63 to your computer and use it in GitHub Desktop.
Save agrimrules/0529760855d9b8c9676f513d922fac63 to your computer and use it in GitHub Desktop.
import cv2
import urllib
import numpy as np
from sklearn.cluster import KMeans
import utils
import requests
import time
url = 'http://AgrimRPI3.local:5000/setpixel' //The Avahi Daemon is a beautiful thing
stream = urllib.urlopen('http://AgrimRPI2:8080/?action=stream')
bytes = ''
while True:
bytes += stream.read(1024)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a != -1 and b != -1: // Compute bounds for a frame in MJPEG
jpg = bytes[a:b + 2]
bytes = bytes[b + 2:]
i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.COLOR_RGB2BGR)
image = i.reshape((i.shape[0] * i.shape[1], 3)) // Resize image for easy computation
clt = KMeans(2)
clt.fit(image) //Generate Clusters
histogram = utils.centroid_histogram(clt) //Compute Histogarm
prominent_color_percentage = max(histogram)
prominent_color = clt.cluster_centers_[histogram.tolist().index(prominent_color_percentage)]
prominent_color_rgb = [int(x) for x in prominent_color]
rgb = prominent_color_rgb[::-1] // get RGB value of the color
print rgb
r = requests.post(url, json={"pxls": rgb}) // Posting to this endpoint light up the LED board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment