Ejemplo n.º 1
0
 def testRunContext(self):
     r = Random()
     n = r.randint(1, 999)
     c = RunContext(n)
     t = Task(c.execute)
     t.run(None)
     self.assertEqual(n, t.result, "The result and generated random number must be equal")
Ejemplo n.º 2
0
 def deactivate(self):
   if self.active:
     Task.deactivate(self)
     self.cursor.visible = False
     if self.outFile is not None and not self.outFile.closed:
       self.outFile.close()
     if self.showPoseWindow:
       cv2.destroyWindow(self.windowName)
Ejemplo n.º 3
0
 def __init__(self):
   Task.__init__(self)
   
   # * Initialize variables
   self.pointer = HapticPointer()  # TODO create tools directly in Main, just like tasks, and find them here?
   
   # * Load scene fragments
   self.context.scene.readXML(self.context.getResourcePath('data', 'PointerScene.xml'))
Ejemplo n.º 4
0
 def deactivate(self):
     if self.active:
         Task.deactivate(self)
         self.cursor.visible = False
         if self.outFile is not None and not self.outFile.closed:
             self.outFile.close()
         if self.showPoseWindow:
             cv2.destroyWindow(self.windowName)
Ejemplo n.º 5
0
 def activate(self):
   # * Find pointer actor in scene, reposition it and make it visible
   self.pointerActor = self.context.scene.findActorById('pointer')
   if self.pointerActor is None:
     self.logger.warn("[HapticSelectTask] Pointer object not found in scene; task could not be initialized")
     return
   self.pointerActor.components['Transform'].translation = np.float32([0, 0, 80])
   self.pointerActor.visible = True
   Task.activate(self)
Ejemplo n.º 6
0
 def testErrorFieldSet(self):
     global errorFunction, result
     t = Task(errorFunction)
     try:
         t.run(None)
         self.fail("Exception not raised")
     except:
         pass
     self.assertTrue(result, "Function not called")
     self.assertEqual("Failed", "%s" % t.error, "Error field not set")
Ejemplo n.º 7
0
 def testResultFieldUnset(self):
     global errorFunction, result
     t = Task(errorFunction)
     try:
         t.run(None)
         self.fail("Exception not raised")
     except:
         pass
     self.assertTrue(result, "Function not called")
     self.assertEqual(None, t.result, "Result field set")
Ejemplo n.º 8
0
 def testCallbackFail(self):
     r = Random()
     n = r.randint(1, 999)
     c = FailCallback(n)
     t = Task(c.execute, c.callback)
     try:
         t.run(None)
         self.fail("The expected exception was not thrown")
     except CallbackExecutionException as e:
         self.assertEqual("Failure! %i" % n, "%s" % e, "Exception message not correct")
Ejemplo n.º 9
0
 def testSuccessfulFlagFalse(self):
     global errorFunction, result
     t = Task(errorFunction)
     try:
         t.run(None)
         self.fail("Exception not raised")
     except:
         pass
     self.assertTrue(result, "Function not called")
     self.assertFalse(t.successful, "Run flag not set")
Ejemplo n.º 10
0
    async def test_dependency(self):

        t1 = Task('t1')
        dep_ = Dependency(t1)
        assert t1 == dep_.get_custodian_task

        t2 = Task('t1-1')
        t3 = Task('t1-2')
        t4 = Task('t1-3')

        await dep_.add_tasks([t2, t3, t4])
        assert len(dep_) == 3
Ejemplo n.º 11
0
 def testCallbackSuccess(self):
     r = Random()
     n = r.randint(1, 999)
     c = SuccessfulCallback(n)
     t = Task(c.execute, c.callback)
     try:
         t.run(None)
         self.assertEqual(n, t.result, "The result and generated random number must be equal")
         self.assertEqual(n, c.result, "The callback-stored result and random number must be equal")
         self.assertTrue(c.successful, "The callback must set the successful flag")
     except Exception as e:
         self.fail("Threw exception: %s" % e)
Ejemplo n.º 12
0
 def __init__(self):
   # NOTE Scene must be initialized at this point
   Task.__init__(self)
   self.cursor = None
   self.outFile = None
   self.poseRecordHeader = "frame\ttime\ttrans_x\ttrans_y\ttrans_z\trot_x\trot_y\trot_z\n"
   self.poseRecordFormat = "{frameCount}\t{timeNow}\t{tvec[0]}\t{tvec[1]}\t{tvec[2]}\t{rvec[0]}\t{rvec[1]}\t{rvec[2]}\n"
   self.lastFrameCount = -1
   self.showPoseWindow = False  # to be treated as a constant flag; may slow down recording
   if self.showPoseWindow:
     self.imageOut = np.zeros((50, 300, 3), dtype=np.uint8)
     self.windowName = "Cursor pose"
Ejemplo n.º 13
0
 def __init__(self):
     # NOTE Scene must be initialized at this point
     Task.__init__(self)
     self.cursor = None
     self.outFile = None
     self.poseRecordHeader = "frame\ttime\ttrans_x\ttrans_y\ttrans_z\trot_x\trot_y\trot_z\n"
     self.poseRecordFormat = "{frameCount}\t{timeNow}\t{tvec[0]}\t{tvec[1]}\t{tvec[2]}\t{rvec[0]}\t{rvec[1]}\t{rvec[2]}\n"
     self.lastFrameCount = -1
     self.showPoseWindow = False  # to be treated as a constant flag; may slow down recording
     if self.showPoseWindow:
         self.imageOut = np.zeros((50, 300, 3), dtype=np.uint8)
         self.windowName = "Cursor pose"
Ejemplo n.º 14
0
 def activate(self):
   self.cursor = self.context.scene.findActorById('cursor')
   self.target = self.context.scene.findActorById('target')
   if self.cursor is None or self.target is None:
     self.logger.warn("[MatchTargetTask] Cursor or target object not found; task could not be initialized")
     return
   self.cursor.visible = True
   self.target.visible = True
   if self.showMatchWindow:
     self.imageOut.fill(255)
     cv2.imshow(self.windowName, self.imageOut)
     cv2.waitKey(1)
   Task.activate(self)
Ejemplo n.º 15
0
 def testAlreadyRun(self):
     r = Runner(100)
     t = Task(r.execute)
     try:
         t.run(None)
     except:
         self.fail("Exception thrown where unexpected")
         
     try:
         t.run(None)
         self.fail("Did not cause any exceptions")
     except AlreadyRunException:
         pass
Ejemplo n.º 16
0
 def import_(cls, data_id, type_,*args, **kwargs):
     """
     data import
     including generate config, register a task, and fire a task
     :param data_id:  data id
     :param type_:  type of data import task, support database, hdfs, text-file
     :param detail: configuration detail
     :return: None
     """
     data_path = cls.my_data_path(data_id)
     config = cls.generate_config(data_path,*args,**kwargs)
     task_id = Task.register(Task.TYPE.DATA_IMPORT)
     DataImport(data_id= data_id, task_id= task_id, type_l= type_, config= config).save()
     Task.fire(task_id)
Ejemplo n.º 17
0
 def activate(self):
   self.cursor = self.context.scene.findActorById('cube')  # NOTE cube itself is the cursor object
   if self.cursor is None:
     self.logger.warn("[RecordPoseTask] Cursor object not found; task could not be initialized")
     return
   self.cursor.visible = True
   self.outFile = open(self.context.getResourcePath("../out", "pose.dat"), "w")  # TODO change to getOutputPath()?
   self.outFile.write(self.poseRecordHeader)
   self.lastFrameCount = -1
   if self.showPoseWindow:
     self.imageOut.fill(255)
     cv2.imshow(self.windowName, self.imageOut)
     cv2.waitKey(1)
   Task.activate(self)
Ejemplo n.º 18
0
 def __init__(self):
   # NOTE Scene must be initialized at this point
   Task.__init__(self)
   
   # * Initialize variables
   self.cursor = None
   self.target = None
   self.showMatchWindow = True  # to be treated as a constant flag
   if self.showMatchWindow:
     self.imageOut = np.zeros((64, 128, 3), dtype=np.uint8)
     self.windowName = "Match error"
   
   # * Load scene fragments
   self.context.scene.readXML(self.context.getResourcePath('data', 'DragonScene.xml'))  # cursor
   self.context.scene.readXML(self.context.getResourcePath('data', 'StaticDragonScene.xml'))  # target
Ejemplo n.º 19
0
 def activate(self):
     self.cursor = self.context.scene.findActorById('cursor')
     self.target = self.context.scene.findActorById('target')
     if self.cursor is None or self.target is None:
         self.logger.warn(
             "[MatchTargetTask] Cursor or target object not found; task could not be initialized"
         )
         return
     self.cursor.visible = True
     self.target.visible = True
     if self.showMatchWindow:
         self.imageOut.fill(255)
         cv2.imshow(self.windowName, self.imageOut)
         cv2.waitKey(1)
     Task.activate(self)
Ejemplo n.º 20
0
    def __init__(self):
        # NOTE Scene must be initialized at this point
        Task.__init__(self)

        # * Initialize variables
        self.cursor = None
        self.target = None
        self.showMatchWindow = True  # to be treated as a constant flag
        if self.showMatchWindow:
            self.imageOut = np.zeros((64, 128, 3), dtype=np.uint8)
            self.windowName = "Match error"

        # * Load scene fragments
        self.context.scene.readXML(
            self.context.getResourcePath('data', 'DragonScene.xml'))  # cursor
        self.context.scene.readXML(
            self.context.getResourcePath('data',
                                         'StaticDragonScene.xml'))  # target
Ejemplo n.º 21
0
 def activate(self):
     self.cursor = self.context.scene.findActorById(
         'cube')  # NOTE cube itself is the cursor object
     if self.cursor is None:
         self.logger.warn(
             "[RecordPoseTask] Cursor object not found; task could not be initialized"
         )
         return
     self.cursor.visible = True
     self.outFile = open(self.context.getResourcePath("../out", "pose.dat"),
                         "w")  # TODO change to getOutputPath()?
     self.outFile.write(self.poseRecordHeader)
     self.lastFrameCount = -1
     if self.showPoseWindow:
         self.imageOut.fill(255)
         cv2.imshow(self.windowName, self.imageOut)
         cv2.waitKey(1)
     Task.activate(self)
Ejemplo n.º 22
0
 def testErrorFieldUnset(self):
     global runFunction, result
     t = Task(runFunction)
     t.run(None)
     self.assertTrue(result, "Function not called")
     self.assertEqual(None, t.error, "Error field set")
Ejemplo n.º 23
0
 def deactivate(self):
   Task.deactivate(self)
   self.pointerActor.visible = False
   self.pointer.close()
Ejemplo n.º 24
0
    def __init__(self):
        #sys.argv = ['Main.py', '../res/videos/test-14.mpeg', '--hide_input']  # [debug: run with set command-line args]

        # * Initialize global context, passing in custom command line args (parsed by Context)
        argParser = argparse.ArgumentParser(add_help=False)
        showInputGroup = argParser.add_mutually_exclusive_group()
        showInputGroup.add_argument(
            '--show_input',
            dest='show_input',
            action="store_true",
            default=True,
            help="show input video (emulate see-through display)?")
        showInputGroup.add_argument(
            '--hide_input',
            dest='show_input',
            action="store_false",
            default=False,
            help="hide input video (show only virtual objects)?")
        argParser.add_argument('--task',
                               default="Task",
                               help="task to run (maps to class name)")
        argParser.add_argument(
            '--scene',
            dest="scene_files",
            metavar='SCENE_FILE',
            nargs='+',
            help="scene fragment(s) to load (filenames in <res>/data/)")

        self.context = Context.createInstance(
            description="Tangible Data Exploration",
            parent_argparsers=[argParser])
        self.context.main = self  # hijack global context to share a reference to self
        # NOTE Most objects require an initialized context, so do this as soon as possible

        # * Obtain a logger (NOTE Context must be initialized first since it configures logging)
        self.logger = logging.getLogger(__name__)
        self.logger.info("Resource path: {}".format(self.context.resPath))
        if not haveCV:
            self.logger.warn("OpenCV library not available")

        # * Initialize GL rendering context and associated objects (NOTE order of initialization may be important)
        self.context.renderer = Renderer()
        self.context.controller = Controller()

        # * Initialize scene and load base scene fragments, including tools
        self.context.scene = Scene()
        self.context.scene.readXML(
            self.context.getResourcePath('data',
                                         'CubeScene.xml'))  # just the cube
        #self.context.scene.readXML(self.context.getResourcePath('data', 'DragonScene.xml'))  # Stanford Dragon
        #self.context.scene.readXML(self.context.getResourcePath('data', 'BP3D-FMA7088-heart.xml'))  # BodyParts3D heart model hierarchy
        #self.context.scene.readXML(self.context.getResourcePath('data', 'RadialTreeScene.xml'))
        #self.context.scene.readXML(self.context.getResourcePath('data', 'PerspectiveScene.xml'))

        # ** Load scene fragments specified on commandline (only need to specify filename in data/ directory)
        self.logger.info("Scene fragment(s): %s",
                         self.context.options.scene_files)
        if self.context.options.scene_files is not None:
            for scene_file in self.context.options.scene_files:
                self.context.scene.readXML(
                    self.context.getResourcePath('data', scene_file))

        # * Initialize task (may load further scene fragments, including task-specific tools)
        try:
            taskModule = import_module(
                'task.' + self.context.options.task
            )  # fetch module by name from task package
            taskType = getattr(
                taskModule, self.context.options.task
            )  # fetch class by name from corresponding module (same name, by convention)
            self.context.task = taskType(
            )  # create an instance of specified task class
        except Exception as e:
            self.logger.error("Task initialization error: {}".format(e))
            self.context.task = Task()  # fallback to dummy task

        # * Finalize scene (resolves scene fragments into one hierarchy, builds ID-actor mapping)
        self.context.scene.finalize(
        )  # NOTE should be called after all read*() methods have been called on scene

        # * Find cube in scene
        self.cubeActor = self.context.scene.findActorById('cube')
        self.cubeComponent = self.cubeActor.components[
            'Cube'] if self.cubeActor is not None else None

        # * Open camera/input file
        self.logger.info("Input device/file: {}".format(
            self.context.options.input_source))
        self.camera = cv2.VideoCapture(
            self.context.options.input_source
        ) if not self.context.isImage else cv2.imread(
            self.context.options.input_source)
        # TODO move some more options (e.g. *video*) to context; introduce config.yaml-like solution with command-line overrides
        self.options = {
            'gui': self.context.options.gui,
            'debug': self.context.options.debug,
            'isVideo': self.context.isVideo,
            'loopVideo': self.context.options.loop_video,
            'syncVideo': self.context.options.sync_video,
            'videoFPS': self.context.options.video_fps,
            'isImage': self.context.isImage,
            'cameraWidth': cameraWidth,
            'cameraHeight': cameraHeight,
            'windowWidth': windowWidth,
            'windowHeight': windowHeight
        }
        self.context.videoInput = VideoInput(self.camera, self.options)
        # TODO If live camera, let input image stabilize by eating up some frames, then configure camera
        #   e.g. on Mac OS, use uvc-ctrl to turn off auto-exposure:
        #   $ ./uvc-ctrl -s 1 3 10

        # * Create image blitter, if input is to be shown
        if self.context.options.show_input:
            self.context.imageBlitter = FrameProcessorGL(
                self.options
            )  # CV-GL renderer that blits (copies) CV image to OpenGL window
            # TODO Evaluate 2 options: Have separate tracker and blitter/renderer or one combined tracker that IS-A FrameProcessorGL? (or make a pipeline?)
            self.logger.info(
                "Video see-through mode enabled; input video underlay will be shown"
            )
        else:
            self.logger.info(
                "Video see-through mode disabled; only virtual objects will be shown"
            )

        # * Setup tracking
        self.context.cubeTracker = CubeTracker(
            self.options
        )  # specialized cube tracker, available in context to allow access to cubeTracker's input and output images etc.
        if self.cubeComponent is not None:
            self.logger.info("Tracking setup: Cube has {} markers".format(
                len(self.cubeComponent.markers)))
            self.context.cubeTracker.addMarkersFromTrackable(
                self.cubeComponent)
Ejemplo n.º 25
0
 def testNoArgsJob(self):
     # Tests that a job which isn't expecting args still works
     j = NoArgsJob()
     t = Task(j.do)
     t.run(None)
     self.assertTrue(j.run)
Ejemplo n.º 26
0
 def deactivate(self):
   Task.deactivate(self)
   self.cursor.visible = False
   self.target.visible = False
   if self.showMatchWindow:
     cv2.destroyWindow(self.windowName)
Ejemplo n.º 27
0
 def deactivate(self):
     Task.deactivate(self)
     self.cursor.visible = False
     self.target.visible = False
     if self.showMatchWindow:
         cv2.destroyWindow(self.windowName)
Ejemplo n.º 28
0
 def testRunFlag(self):
     global runFunction, result
     t = Task(runFunction)
     t.run(None)
     self.assertTrue(result, "Function not called")
     self.assertTrue(t.hasRun, "Run flag not set")
Ejemplo n.º 29
0
 def testResultFieldSet(self):
     global runFunction, result
     t = Task(runFunction)
     t.run(None)
     self.assertTrue(result, "Function not called")
     self.assertEqual(True, t.result, "Result field not set")
Ejemplo n.º 30
0
 def testSuccessfulFlagTrue(self):
     global runFunction, result
     t = Task(runFunction)
     t.run(None)
     self.assertTrue(result, "Function not called")
     self.assertTrue(t.successful, "Run flag not set")