def on_frame(self, b): ret = inp = numpy.fromstring(b.data, dtype=numpy.uint8).reshape(480, 720, 3) if not self.video_tracker: self.video_tracker = VideoTracker(initial_frame=inp, on_cx_cy=self.on_cx_cy) else: ret = self.video_tracker.on_frame(inp) return gst.Buffer(ret)
class Controller(object): def __init__(self): motor = get_motor(motor_debug) trigger = get_trigger(debug) self.motor = motor self.video_tracker = None print "CONNECTING Motor" motor.connect() print "CONNECTING Trigger" self.trigger = trigger.Trigger() print "CONNECTING Joystick" self.init_joystick() self.manual = False self.x = None self.y = None self.fire_duration = 200 self.video_tracker = None def _unused_init_detect_process(self): self.video_in = Queue() self.video_out = Queue() self.detect_process = DetectProcess(inq=self.video_in, outq=self.video_out) self.detect_process.start() def on_frame(self, b): ret = inp = numpy.fromstring(b.data, dtype=numpy.uint8).reshape(480, 720, 3) if not self.video_tracker: self.video_tracker = VideoTracker(initial_frame=inp, on_cx_cy=self.on_cx_cy) else: ret = self.video_tracker.on_frame(inp) return gst.Buffer(ret) def on_frame_process(self, b): ret = inp = numpy.fromstring(b.data, dtype=numpy.uint8).reshape(480, 720, 3) print "pushing" self.video_in.push("test") print "popping" ret_unused = self.video_out.pop() assert ret_unused == "testout" print "popped" return gst.Buffer(ret) def init_joystick(self): self.j = joystick.Joystick() self.j.connect("axis", self.on_axis) self.j.connect("button", self.on_button) def on_axis(self, signal, code, value): print "on_axis %s %s" % (code, value) # print "on_axis %d, %d" % (code, value) if code not in [0, 1, 2]: return if code == 0: self.x = value if self.manual: self.motor.theta.set(state.x - 127) elif code == 1: self.y = value if self.manual: self.motor.pitch.set(state.y - 127) else: fire_duration = 100 + (float(value) / 255.0) * 900 print "joy: %d => fire dur %d" % (value, fire_duration) self.fire_duration = int(fire_duration) def on_button(self, signal, code, value): print "on_button: %d, %d" % (code, value) if value != 1 or code != 0: return print " **** FIRE ****" self.trigger.fire(self.fire_duration) def on_cx_cy(self, cx, cy): # print "controller: got (%d, %d)" % (cx, cy) cx -= 720 / 2 cy -= 480 / 2 for axis, val in [(self.motor.theta, cx), (self.motor.pitch, cy)]: if abs(val) > 10: print "action %r" % axis axis.set(200 if cx > 0 else -200)