Example #1
0
            i += 1
        elif s[i] == ',' and level == 0:
            l = string2list(s[i_prev:i])
            l_ret.append(l)
            i += 1
            i_prev = i
        else:
            i += 1
    return l_ret


if __name__ == "__main__":
    bm_string = parse_arguments()
    bitmap = string2list(bm_string)
    bitmap = np.array(bitmap)
    img_syn = bm.bitmap2syn_img(bitmap)
    cv2.namedWindow('show')
    lc.display_image('show',
                     img_syn,
                     wait_time=500,
                     is_resize=True,
                     resize_max=-1,
                     resize_scale=50,
                     save_image=True)

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt as e:
        sys.stdout.write("user exits\n")
Example #2
0
    def _generate_guidance(self, header, state_info_str, engine_id):
        if config.RECOGNIZE_ONLY:
            return json.dumps(result)

        if self.is_first_frame:  # do something special when the task begins
            result, img_guidance = self.task.get_first_guidance()
            result['image'] = b64encode(zc.cv_image2raw(img_guidance))
            zc.check_and_display('guidance',
                                 img_guidance,
                                 display_list,
                                 wait_time=config.DISPLAY_WAIT_TIME,
                                 resize_max=config.DISPLAY_MAX_PIXEL)
            self.is_first_frame = False
            header['status'] = result.pop('status')
            result.pop('animation', None)
            return json.dumps(result)

        header['status'] = "success"
        result = {}  # default

        state_info = json.loads(state_info_str)
        if not state_info['trust']:
            header['status'] = "fail"
            return json.dumps(result)

        state = state_info['state']

        if state == "None":
            header['status'] = "nothing"
            return json.dumps(result)

        bitmap = np.array(json.loads(state))

        ## try to commit bitmap
        state_change = False
        if bm.bitmap_same(self.commited_bitmap, bitmap):
            pass
        else:
            current_time = time.time()
            if not bm.bitmap_same(self.temp_bitmap['bitmap'], bitmap):
                self.temp_bitmap['bitmap'] = bitmap
                self.temp_bitmap['first_time'] = current_time
                self.temp_bitmap['count'] = 0
            self.temp_bitmap['count'] += 1
            if current_time - self.temp_bitmap[
                    'first_time'] > config.BM_WINDOW_MIN_TIME or self.temp_bitmap[
                        'count'] >= config.BM_WINDOW_MIN_COUNT:
                self.commited_bitmap = self.temp_bitmap['bitmap']
                state_change = True

        bitmap = self.commited_bitmap
        if 'lego_syn' in display_list and bitmap is not None:
            img_syn = bm.bitmap2syn_img(bitmap)
            zc.display_image('lego_syn',
                             img_syn,
                             wait_time=config.DISPLAY_WAIT_TIME,
                             resize_scale=50)

        ## now user has done something, provide some feedback
        img_guidance = None
        if state_change:
            self.task.update_state(bitmap)
            result, img_guidance = self.task.get_guidance()
            result['image'] = b64encode(zc.cv_image2raw(img_guidance))
            header['status'] = result.pop('status')
            result.pop('animation', None)

        if img_guidance is not None:
            zc.check_and_display('guidance',
                                 img_guidance,
                                 display_list,
                                 wait_time=config.DISPLAY_WAIT_TIME,
                                 resize_max=config.DISPLAY_MAX_PIXEL)

        return json.dumps(result)
Example #3
0
    def _handle_img(self, img):
        if self.is_first_frame and not config.RECOGNIZE_ONLY: # do something special when the task begins
            result, img_guidance = self.task.get_first_guidance()
            zc.check_and_display('guidance', img_guidance, display_list, wait_time = config.DISPLAY_WAIT_TIME, resize_max = config.DISPLAY_MAX_PIXEL)
            self.is_first_frame = False
            result['state_index'] = 0  # first step
            return json.dumps(result)

        result = {'status' : "nothing"} # default

        stretch_ratio = float(16) / 9 * img.shape[0] / img.shape[1]
        if img.shape != (config.IMAGE_WIDTH, config.IMAGE_HEIGHT, 3):
            img = cv2.resize(img, (config.IMAGE_WIDTH, config.IMAGE_HEIGHT), interpolation = cv2.INTER_AREA)

        ## get bitmap for current image
        zc.check_and_display('input', img, display_list, wait_time = config.DISPLAY_WAIT_TIME, resize_max = config.DISPLAY_MAX_PIXEL)
        rtn_msg, bitmap = lc.process(img, stretch_ratio, display_list)
        if rtn_msg['status'] != 'success':
            print rtn_msg['message']
            if rtn_msg['message'] == "Not confident about reconstruction, maybe too much noise":
                self.counter['not_confident'] += 1
            return json.dumps(result)

        self.counter['confident'] += 1

        ## try to commit bitmap
        state_change = False
        if bm.bitmap_same(self.commited_bitmap, bitmap):
            pass
        else:
            current_time = time.time()
            if not bm.bitmap_same(self.temp_bitmap['bitmap'], bitmap):
                self.temp_bitmap['bitmap'] = bitmap
                self.temp_bitmap['first_time'] = current_time
                self.temp_bitmap['count'] = 0
                self.counter['diff_from_prev'] += 1
            else:
                self.counter['same_as_prev'] += 1
            self.temp_bitmap['count'] += 1
            if current_time - self.temp_bitmap['first_time'] > config.BM_WINDOW_MIN_TIME or self.temp_bitmap['count'] >= config.BM_WINDOW_MIN_COUNT:
                self.commited_bitmap = self.temp_bitmap['bitmap']
                state_change = True
        #print "\n\n\n\n\n%s\n\n\n\n\n" % self.counter

        bitmap = self.commited_bitmap
        if 'lego_syn' in display_list and bitmap is not None:
            img_syn = bm.bitmap2syn_img(bitmap)
            zc.display_image('lego_syn', img_syn, wait_time = config.DISPLAY_WAIT_TIME, resize_scale = 50)

        if config.RECOGNIZE_ONLY:
            return json.dumps(result)

        ## now user has done something, provide some feedback
        img_guidance = None
        if state_change:
            self.task.update_state(bitmap)
            result, img_guidance = self.task.get_guidance()

            if self.task.is_final_state():
                step_idx = len(self.task.states)
            else:
                # get current step
                step_idx = self.task.state2idx(self.task.current_state)
                # make sure step index is always -1 in case of error
                # also, differentiate from the default initial step (which we assign a step index 0)
                # from the internal step index obtained from the task (which also begins at 0) by 
                # shifting the index by 1:
                step_idx = -1 if step_idx < 0 else step_idx + 1
            result['state_index'] = step_idx

        if img_guidance is not None:
            zc.check_and_display('guidance', img_guidance, display_list, wait_time = config.DISPLAY_WAIT_TIME, resize_max = config.DISPLAY_MAX_PIXEL)

        return json.dumps(result)
Example #4
0
    def _handle_img(self, img):
        if self.is_first_frame and not config.RECOGNIZE_ONLY:  # do something special when the task begins
            result, img_guidance = self.task.get_first_guidance()
            zc.check_and_display('guidance',
                                 img_guidance,
                                 display_list,
                                 wait_time=config.DISPLAY_WAIT_TIME,
                                 resize_max=config.DISPLAY_MAX_PIXEL)
            result['image'] = b64encode(zc.cv_image2raw(img_guidance))
            result.pop('animation', None)
            self.is_first_frame = False
            return json.dumps(result)

        result = {'status': "nothing"}  # default

        stretch_ratio = float(16) / 9 * img.shape[0] / img.shape[1]
        if img.shape != (config.IMAGE_WIDTH, config.IMAGE_HEIGHT, 3):
            img = cv2.resize(img, (config.IMAGE_WIDTH, config.IMAGE_HEIGHT),
                             interpolation=cv2.INTER_AREA)

        ## get bitmap for current image
        zc.check_and_display('input',
                             img,
                             display_list,
                             wait_time=config.DISPLAY_WAIT_TIME,
                             resize_max=config.DISPLAY_MAX_PIXEL)
        rtn_msg, bitmap = lc.process(img, stretch_ratio, display_list)

        if gabriel.Debug.TIME_MEASUREMENT:
            result[gabriel.Protocol_measurement.
                   JSON_KEY_APP_SYMBOLIC_TIME] = time.time()

        if rtn_msg['status'] != 'success':
            print rtn_msg['message']
            if rtn_msg[
                    'message'] == "Not confident about reconstruction, maybe too much noise":
                self.counter['not_confident'] += 1
            return json.dumps(result)

        self.counter['confident'] += 1

        ## try to commit bitmap
        state_change = False
        if bm.bitmap_same(self.commited_bitmap, bitmap):
            pass
        else:
            current_time = time.time()
            if not bm.bitmap_same(self.temp_bitmap['bitmap'], bitmap):
                self.temp_bitmap['bitmap'] = bitmap
                self.temp_bitmap['first_time'] = current_time
                self.temp_bitmap['count'] = 0
                self.counter['diff_from_prev'] += 1
            else:
                self.counter['same_as_prev'] += 1
            self.temp_bitmap['count'] += 1
            if current_time - self.temp_bitmap[
                    'first_time'] > config.BM_WINDOW_MIN_TIME or self.temp_bitmap[
                        'count'] >= config.BM_WINDOW_MIN_COUNT:
                self.commited_bitmap = self.temp_bitmap['bitmap']
                state_change = True
        #print "\n\n\n\n\n%s\n\n\n\n\n" % self.counter

        bitmap = self.commited_bitmap
        if 'lego_syn' in display_list and bitmap is not None:
            img_syn = bm.bitmap2syn_img(bitmap)
            zc.display_image('lego_syn',
                             img_syn,
                             wait_time=config.DISPLAY_WAIT_TIME,
                             resize_scale=50)

        if config.RECOGNIZE_ONLY:
            return json.dumps(result)

        ## now user has done something, provide some feedback
        img_guidance = None
        if state_change:
            self.task.update_state(bitmap)
            sym_time = result[
                gabriel.Protocol_measurement.JSON_KEY_APP_SYMBOLIC_TIME]
            result, img_guidance = self.task.get_guidance()
            result[gabriel.Protocol_measurement.
                   JSON_KEY_APP_SYMBOLIC_TIME] = sym_time
            result['image'] = b64encode(zc.cv_image2raw(img_guidance))
            result.pop('animation', None)

        if img_guidance is not None:
            zc.check_and_display('guidance',
                                 img_guidance,
                                 display_list,
                                 wait_time=config.DISPLAY_WAIT_TIME,
                                 resize_max=config.DISPLAY_MAX_PIXEL)

        return json.dumps(result)