/
ControlUnit.py
145 lines (122 loc) · 4.61 KB
/
ControlUnit.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
import cv2
import cv
from osc_utils import *
from Blob import Blob
from Region import Region
from Patch import Patch
import utils
import threading
import time
from Gen import *
from Oscillators import *
from MiscGens import *
from GenMap import GenMap
class TextOutput(object):
def __init__(self, text, loc, scale=1.0,
font=cv2.FONT_HERSHEY_TRIPLEX, color=(0,0,0)):
self.text = text
self.loc = loc
self.scale = scale
self.font = font
self.color = color
class CameraOutThread(threading.Thread):
def __init__(self, cam, scale=1.0, font=cv2.FONT_HERSHEY_TRIPLEX, color=(0,0,0)):
threading.Thread.__init__(self)
self.cam = cam
self.text_outputs = []
self.gen_maps = {}
self.font = font
self.color = color
self.scale = scale
def run(self):
num_round = 0
while(1):
_,f = self.cam.read()
if self.gen_maps:
for gm_id in self.gen_maps:
self.gen_maps[gm_id].update_attrs(utils.copy_image(f))
Patch.osc_update_all_attrs()
for output in self.text_outputs:
cv2.putText(f, output.text, output.loc,
output.font, output.scale, output.color)
if num_round % 30 == 0:
for gm_id in self.gen_maps:
gm = self.gen_maps[gm_id]
cv2.putText(f, '%s-%s' % (gm.region.id, gm.gen.gen_type),
(gm.region.centroid_x, gm.region.centroid_y),
self.font, self.scale, self.color)
gm.region.draw_outline(f, utils.BGR_RED)
gm.region.draw_blobs(f, utils.BGR_GREEN)
cv2.imshow('e2',f)
if cv2.waitKey(10)==27:
break
cv2.destroyAllWindows()
def add_text(self, text_output):
self.text_outputs.append(text_output)
def start():
# init camera
cam = cv2.VideoCapture(1)
# init osc communication with chuck
initOSCClient()
cam_thread = CameraOutThread(cam)
cam_thread.start()
time.sleep(0.5)
raw_input("Press Enter when camera is positioned")
# display numbers on regions
image = cam.read()
regions = Region.find_regions_by_border_color(image[1])
for region in regions:
cam_thread.add_text(TextOutput(str(region.id), (region.centroid_x, region.centroid_y)))
# ask for Gen type for each region
dac = DAC()
gen_maps = {}
for i, region in enumerate(regions):
while (True):
try:
gen_type = raw_input("Please enter UGen type for region %s: " % region.id)
gen = eval(gen_type)()
attr_range = [0.0, 1.0]
if isinstance(gen, Osc):
attr_range[0] = float(raw_input(" What is the low frequency on this oscillator? "))
attr_range[1] = float(raw_input(" What is the high frequency on this oscillator? "))
gen_maps[region.id] = GenMap(gen, region, attr_range=attr_range)
break
except:
print 'Invalid UGen, try again.'
# ask for gen links
# dac special to_where
while (raw_input("Add a patch? (y/n): ") == 'y'):
from_where = None
while (True):
try:
# NOTE: no range checking
from_where = int(raw_input('from Region #: '))
from_where = gen_maps[from_where]
break
except ValueError:
print 'Please enter an integer.'
to_where = None
while (True):
try:
to_where = raw_input('to Region #: ')
if to_where == 'dac':
to_where = dac
break
to_where = int(to_where)
to_where = gen_maps[to_where]
break
except ValueError:
print 'Please enter an integer.'
# will handle dac later
# for now, all patch paths assumed end in dac
if not isinstance(to_where, DAC):
(from_where.gen).add_patch_to(to_where.gen)
Patch.find_patches([gen_maps[gm_id].gen for gm_id in gen_maps])
Patch.osc_send_all_patches()
cam_thread.text_outputs = []
cam_thread.gen_maps = gen_maps
# start system updating the blob in the regions
# sending attr data to chuck
if __name__ == "__main__":
start()