Skip to content

Instantly share code, notes, and snippets.

@JoppeSchwartz
Created December 20, 2014 22:22
Show Gist options
  • Save JoppeSchwartz/297cfb4297717ef2d1d8 to your computer and use it in GitHub Desktop.
Save JoppeSchwartz/297cfb4297717ef2d1d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# (C) 2014 Joe Schwartz & Square Design, Inc.
#
# Uses the image from a webcam to control a grid of 32 Neopixel strips with 48
# pixels on each strip.
#
import signal
import sys
import numpy as np
import math
import cv2
import opc
import time
print "Starting ledmirror - press CTRL-C to exit"
cap = cv2.VideoCapture(0)
def signalHandler(signal, frame):
print "Closing camera"
cap.release()
sys.exit(0)
signal.signal(signal.SIGINT, signalHandler)
# Initialize the client connection to the local fcserver.
client = opc.Client('localhost:7890')
# For transforming the captured frame to a reduced resolution frame.
# TODO: get camera resolution from API
capCols = 640
capRows = 360
MIRROR_COLS = 32
MIRROR_ROWS = 48
colStep = math.floor(float(capCols) / MIRROR_COLS)
rowStep = math.floor(float(capRows) / MIRROR_ROWS)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Process the frame. For the frame pixel array [i, j], i refers to rows and
# j refers to columns. Use fast splicing to grab a reduced resolution array
# corresponding to the Neopixel grid.
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
reducedFrame = cv2.resize(frame, (MIRROR_COLS, MIRROR_ROWS))
reducedFrame = reducedFrame[:, ::-1]
reducedFrame = np.roll(reducedFrame, 2, axis=2)
#print "Frame size: {0}".format(frame.shape)
#reducedFrame = frame[0::rowStep, 0::colStep]
#print "Reduced frame: {0}".format(reducedFrame.shape)
#if reducedFrame.shape[0] > MIRROR_ROWS:
# reducedFrame = reducedFrame[0:MIRROR_ROWS]
#print "Re-Reduced frame: {0}".format(reducedFrame.shape)
# The Neopixel grid is addressed as a 2-dmensional array.
ledArray = np.reshape(reducedFrame, (reducedFrame.shape[0] * reducedFrame.shape[1], 3), 'F')
client.put_pixels(ledArray)
time.sleep(0.1)
# cv2.imshow('frame', frame) #gray)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# When all is done, release the capture
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment