Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
added .wav files that work with the peak recognition
  • Loading branch information
tjthejuggler authored Mar 22, 2018
1 parent 4016722 commit 7827707
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
108 changes: 76 additions & 32 deletions miugCom.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# import the necessary packages
from __future__ import print_function
from imutils.video import WebcamVideoStream
from imutils.video import FPS
import argparse
import imutils
import cv2
import colorsys
import math
from math import hypot
Expand All @@ -15,29 +22,36 @@
from tkinter import messagebox
import pyautogui
import time
import pyHook
import pythoncom
import win32com.client
from tkinter.filedialog import askopenfilename
from scipy import ndimage
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import pygame as pg

pg.mixer.init()
pg.init()
pg.mixer.set_num_channels(19)
sound = pg.mixer.Sound(pg.mixer.Sound("/Users/Thursday/..miugCom/tada.wav"))


import librosa
import librosa.display
midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()
if available_ports:
midiout.open_port(1)
else:
midiout.open_virtual_port("My virtual output")

root = Tk()
root.title("Miug")

def run_camera():
pg.mixer.init()
pg.init()
pg.mixer.set_num_channels(19)
sounds = []
sounds.append(pg.mixer.Sound(pg.mixer.Sound("/Users/Thursday/..miugCom/notes01.wav")))
sounds.append(pg.mixer.Sound(pg.mixer.Sound("/Users/Thursday/..miugCom/notes02.wav")))
sounds.append(pg.mixer.Sound(pg.mixer.Sound("/Users/Thursday/..miugCom/notes03.wav")))
sounds.append(pg.mixer.Sound(pg.mixer.Sound("/Users/Thursday/..miugCom/notes04.wav")))

sound_num = 0

PY3 = sys.version_info[0] == 3
if PY3:
xrange = range
Expand All @@ -49,6 +63,11 @@ def run_camera():
args = vars(ap.parse_args())
pts = deque(maxlen=args["buffer"])
camera = cv2.VideoCapture(0)
print("[INFO] sampling THREADED frames from webcam...")

vs = WebcamVideoStream(src=0).start()
fps = FPS().start()

all_cx = []
all_cy = []
frames = 0
Expand All @@ -58,8 +77,11 @@ def run_camera():
lastCy = 0
secondLastY = 0
num_high = 0
while True and frames < 200:
(grabbed, frame) = camera.read()
windowX = []
windowY = []
while True and frames < 2000:
#(grabbed, frame) = camera.read()
frame = vs.read()
frames = frames + 1
#print("frame:" + str(frames))
if args.get("video") and not grabbed:
Expand All @@ -72,55 +94,77 @@ def run_camera():
ret,thresh = cv2.threshold(framegray,127,255,0)
#thresh = cv2.adaptiveThreshold(framegray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

if contours:
cnt = contours[0]
M = cv2.moments(cnt)
#print M
if M['m00'] > 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
all_cx.append(cx)
all_cy.append(cy)
all_cx.append(float(cx))
all_cy.append(float(cy))
#print("cx : "+str(cx)+",cy :"+str(cy))
lastDifY = thisDifY
thisDifY = cy-lastCy
#secondLastY = lastCy
lastCy = cy
#print(str(lastDifY) + " " + str(thisDifY) )
if (lastDifY < 0 and (thisDifY > 0 or abs(thisDifY) < .5)) and (abs(thisDifY) > 20 or abs(lastDifY) > 20):
num_high = num_high + 1
print("hi " + str(num_high))
note_on = [0x90, 60, 127] # channel 1, middle C, velocity 112
midiout.send_message(note_on)
note_on = [0x80, 60, 127] # channel 1, middle C, velocity 112
midiout.send_message(note_on)
#sound.play()
if windowX < 10:
windowX.append(cx)
else:
windowX.push(cx)

if windowY < 10:
windowY.append(cy)
else:
windowY.push(cy)


if (lastDifY < 0 and (thisDifY > 0 or abs(thisDifY) < .5)) and (abs(thisDifY) > 10 or abs(lastDifY) > 10):
num_high = num_high + 1
print("hi " + str(num_high))
sounds[sound_num].play()
sound_num = sound_num + 1
if sound_num == 4:
sound_num = 0


for i in xrange(1, len(pts)):
if pts[i - 1] is None or pts[i] is None:
continue
cv2.imshow("Frame", frame)
#cv2.imshow("Frame", frame)
fps.update()
key = cv2.waitKey(1) & 0xFF


#if cv2.getWindowProperty('Frame', 0) == -1:
#print("b1")
#break
if key == ord("q"):
print("b2")
break
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
end = time.time()
fps = 200/(end-start)
fps = 1200/(end-start)
print("fps: "+str(fps))
camera.release()
cv2.destroyAllWindows()
line1 = plt.plot(all_cx, label="x")
line2 = plt.plot(all_cy, label="y")

#plt.legend([line1, line2],['X', 'Y'])
plt.show()

vs.stop()

#cv2.destroyAllWindows()
camera.release()
spectrogram = np.abs(librosa.stft(np.array(all_cy)))
plt.imshow(spectrogram)
print(spectrogram)
#librosa.display.specshow(librosa.amplitude_to_db(spectrogram))
run_camera()


#LEARNINGS:
# not having this line ' frame = imutils.resize(frame, width=600) ' gives us wildly different fps readings
#
#THE PLAN:
# get the numpy corrcoef between my 2 windows, it should give me a list of %s as long as the number of frames
# next up is figuring out how to seperate my screen into 2 sides and then doing the whole
# process on either side so that I end up x1,y1 and x2,y2, from there i get 6 correlations
Binary file added notes01.wav
Binary file not shown.
Binary file added notes02.wav
Binary file not shown.
Binary file added notes03.wav
Binary file not shown.
Binary file added notes04.wav
Binary file not shown.

0 comments on commit 7827707

Please sign in to comment.