-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbanshee_control.py
116 lines (99 loc) · 3.72 KB
/
banshee_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
This is an example of how SimpleCV can be used so easily to do anything.
I am controlling volume and changing songs in banshee media player by
detecting optical flow.
Banshee has a bug, so a banshee window should be opened before running
this program.
I am using multiprocessing and multithreading to control banshee media
player.
Requirements:
SimpleCV, and its dependencies
alsaaudio library to control volume
Banshee Media Player
Tested on Ubuntu 11.10
"""
from SimpleCV import Camera, Display
from os import system
from time import sleep,time
import threading
from multiprocessing import Process
import alsaaudio
def banshee():
"""
Start a banshee window if it is not opened
"""
system('banshee --play')
sleep(5)
return
class do(threading.Thread):
def __init__(self):
"""
starting a thread
"""
threading.Thread.__init__(self)
def run(self):
m = alsaaudio.Mixer() # defined alsaaudio.Mixer to change volume
scale = (300,250) # increased from (200,150). works well
d = Display(scale)
cam = Camera()
prev = cam.getImage().scale(scale[0],scale[1])
sleep(0.5)
buffer = 20
count = 0
prev_t = time() # Note initial time
while d.isNotDone():
current = cam.getImage()
current = current.scale(scale[0],scale[1])
if( count < buffer ):
count = count + 1
else:
fs = current.findMotion(prev, method="LK") # find motion
# Tried BM, and LK, LK is better. need to learn more about LK
if fs: # if featureset found
dx = 0
dy = 0
for f in fs:
dx = dx + f.dx # add all the optical flow detected
dy = dy + f.dy
dx = (dx / len(fs)) # Taking average
dy = (dy / len(fs))
prev = current
sleep(0.01)
current.save(d)
if dy > 2 or dy < -2:
vol = int(m.getvolume()[0]) # getting master volume
if dy < 0:
vol = vol + (-dy*3)
else:
vol = vol + (-dy*3)
if vol > 100:
vol = 100
elif vol < 0:
vol = 0
print vol
m.setvolume(int(vol)) # setting master volume
if dx > 3:
cur_t = time()
if cur_t > 5 + prev_t: # adding some time delay
self.play("next") # changing next
prev_t = cur_t
if dx < -3:
cur_t = time()
if cur_t > 5 + prev_t:
prev_t = cur_t
self.play("previous") # changing previous
def play(self,command):
"""
change, next or prev
"""
system('banshee --'+command) # giving command to change
def main():
p = Process(target = banshee) #Create a process which will initiate banshee window
p.start() # starting the process
command = do() # Create a thread
command.start() # start the thread
command.join() # wait for the thread to complete
p.terminate() # After thread ends, terminate the main process
print 'terminate'
if __name__ == '__main__':
main()