Exemple #1
0
    def play(self, file, mode='HSV'):
        npz = np.load(file)
        data = self.data = npz['data']
        labels = self.labels = npz['labels']
        n = self.num_annotated = data.shape[0]
        w = self.width = 214
        h = self.height = 120
        c = self.chans = 3
        size = w * h * c
        iw = ImageWindow(w, h)

        assert size == data[0].size, "Unexpected buffer size (%d vs %d)" % (
            size, data[0].size)

        assert data.shape[0] == labels.shape[
            0], "Mismatched image and label count"

        i = 0
        while i < n:
            image = Image.frombytes(mode, (w, h), data[i])
            iw.show_image(image)
            iw.force_focus()
            print('Image {} / {}: {}'.format(i, n, Action.name(labels[i])))
            iw.wait()

            key = iw.get_key()

            if key == 'Escape':
                break
            elif key == 'BackSpace':
                if i > 0:
                    i -= 1
                continue
            else:
                i += 1
    def annotate(self, file):
        self._load_bag_data(file)
        w = self.width
        h = self.height
        s = self.scale
        c = self.chans
        size = w*h/s/s*c
        iw = ImageWindow(w, h)
        self.labels = np.empty(self.num_images, dtype='byte')
        self.data = np.empty((self.num_images, size), dtype='byte')

        # Check that our incoming image size is as expected...
        image = Image.open(BytesIO(self.image_data[0]))
        resized = image.resize((w/s, h/s), resample=Image.LANCZOS)
        hsv = resized.convert('HSV')
        assert size == np.fromstring(hsv.tobytes(), dtype='byte').size, "Unexpected image size!"

        i = 0
        while i < self.num_images:
            image = Image.open(BytesIO(self.image_data[i]))
            resized = image.resize((w/s, h/s), resample=Image.LANCZOS)
            hsv = resized.convert('HSV')
            #hue,_,_ = hsv.split()

            draw = ImageDraw.Draw(image)
            draw.line([(w/s, 0), (w/s, h)])
            draw.line([((s-1)*w/s, 0), ((s-1)*w/s, h)])
            # draw.line([(0, h/s), (w, h/s)])
            # draw.line([(0, (s-1)*h/s), (w, (s-1)*h/s)])

            iw.show_image(image)
            iw.force_focus()

            print('Image {} / {}:'.format(i, self.num_images), end='')

            iw.wait()

            key = iw.get_key()

            if key=='Escape':
                print('(QUIT)')
                break
            elif key=='BackSpace':
                if i > 0:
                    i -= 1
                print('(BACK)')
                continue
            elif key=='space':
                label = Action.SCAN
            elif key=='Return':
                label = Action.TARGET
            elif key=='Left':
                label = Action.TARGET_LEFT
            elif key=='Right':
                label = Action.TARGET_RIGHT
            elif self.num_actions > 4 and key=='Up':
                label = Action.TARGET_UP
            elif self.num_actions > 4 and key=='Down':
                label = Action.TARGET_DOWN
            else:
                label = Action.SCAN

            self.labels[i] = label
            self.data[i] = np.fromstring(hsv.tobytes(), dtype='byte')
            print(Action.name(label))
            i += 1

        iw.close()
        self.num_annotated = i
    def reannotate(self, bagfile, npzfile_in):
        self._load_bag_data(bagfile) # self.num_images set here
        w = self.width
        h = self.height
        s = self.scale
        c = self.chans
        size = w*h/s/s*c
        iw = ImageWindow(w/2, h/2)
        edit = False


        npz = np.load(npzfile_in)
        labels = self.labels = npz['labels']
        n = self.num_annotated = labels.shape[0]

        if self.num_images != self.num_annotated:
            print('Warning: bag and npz file lengths differ ({} vs {})'.format(self.num_images, self.num_annotated))

        data = self.data = np.empty((self.num_annotated, size), dtype='byte')

        # Check that our incoming image size is as expected...
        image = Image.open(BytesIO(self.image_data[0]))
        resized = image.resize((w/s, h/s), resample=Image.LANCZOS)
        hsv = resized.convert('HSV')
        assert size == np.fromstring(hsv.tobytes(), dtype='byte').size, "Unexpected image size!"

        i = 0
        while i < self.num_annotated:
            image = Image.open(BytesIO(self.image_data[i]))\
                         .crop((w/s, h/s, (s-1)*w/s, (s-1)*h/s))
            resized = image.resize((w/s, h/s), resample=Image.LANCZOS)
            hsv = resized.convert('HSV')
            iw.show_image(image)
            iw.force_focus()

            if edit:
                print('Image {} / {} ({}): '.format(i, self.num_annotated, Action.name(labels[i])), end='')
                sys.stdout.flush()
            else:
                print('Image {} / {}: {}'.format(i, n, Action.name(labels[i])))
                if labels[i] >= self.num_actions:
                    print ('>>> CHANGING TO {}'.format(Action.name(Action.SCAN)))
            iw.wait()

            key = iw.get_key()

            if key=='Escape':
                print('(QUIT)')
                break
            elif key=='BackSpace':
                if i > 0:
                    i -= 1
                print('(BACK)')
                continue
            elif key=='e':
                if edit:
                    edit = False
                    print('(EDIT OFF)')
                else:
                    edit = True
                    print('(EDIT ON)')
                continue
            elif not edit:
                if labels[i] >= self.num_actions:
                    label = Action.SCAN
                else:
                    label = self.labels[i]
            elif key=='space':
                label = Action.SCAN
            elif key=='Return':
                label = Action.TARGET
            elif self.num_actions > 2 and key=='Left':
                label = Action.TARGET_LEFT
            elif self.num_actions > 2 and key=='Right':
                label = Action.TARGET_RIGHT
            elif self.num_actions > 4 and key=='Up':
                label = Action.TARGET_UP
            elif self.num_actions > 4 and key=='Down':
                label = Action.TARGET_DOWN
            else:
                label = Action.SCAN

            self.labels[i] = label
            self.data[i] = np.fromstring(hsv.tobytes(), dtype='byte')
            if edit:
                print(Action.name(label))
            i += 1

        iw.close()
        self.num_annotated = i
    def annotate(self, file):
        self._load_data(file)
        w = self.width
        h = self.height
        s = self.scale
        c = self.chans
        size = w * h / s / s * c
        iw = ImageWindow(w / 2, h / 2)
        self.labels = np.empty(self.num_images, dtype='byte')
        self.data = np.empty((self.num_images, size), dtype='byte')
        keep = np.ones(self.num_images)

        # Check that our incoming image size is as expected...
        image = Image.open(BytesIO(self.image_data[0]))
        resized = image.resize((w / s, h / s), resample=Image.LANCZOS)
        hsv = resized.convert('HSV')
        assert size == np.fromstring(
            hsv.tobytes(), dtype='byte').size, "Unexpected image size!"

        i = 0
        while i < self.num_images:
            image = Image.open(BytesIO(self.image_data[i]))\
                         .crop((w/s, h/s, (s-1)*w/s, (s-1)*h/s))
            resized = image.resize((w / s, h / s), resample=Image.LANCZOS)
            hsv = resized.convert('HSV')

            iw.show_image(image)
            iw.force_focus()

            if keep[i] == 1:
                print('Image {} / {}: '.format(i, self.num_images), end='')
            else:
                print('Image {} / {} (KILLED): '.format(i, self.num_images),
                      end='')
            sys.stdout.flush()

            iw.wait()

            key = iw.get_key()

            if key == 'Escape':
                print('(QUIT)')
                break
            elif key == 'BackSpace':
                if i > 0:
                    i -= 1
                print('(BACK)')
                continue
            elif key == 'k':
                print('(KILLED)')
                keep[i] = 0
                label = Action.SCAN
            elif key == 'r':
                print('(RESTORED)')
                keep[i] = 1
                if i > 0:
                    i -= 1
                continue
            elif key == 'space':
                label = Action.SCAN
            elif key == 'Return':
                label = Action.TARGET
            elif self.num_actions > 2 and key == 'Left':
                label = Action.TARGET_LEFT
            elif self.num_actions > 2 and key == 'Right':
                label = Action.TARGET_RIGHT
            elif self.num_actions > 4 and key == 'Up':
                label = Action.TARGET_UP
            elif self.num_actions > 4 and key == 'Down':
                label = Action.TARGET_DOWN
            else:
                label = Action.SCAN

            self.labels[i] = label
            self.data[i] = np.fromstring(hsv.tobytes(), dtype='byte')
            print(Action.name(label))
            i += 1

        iw.close()
        self.num_annotated = i
        self.labels = self.labels[:i]
        self.data = self.data[:i]
        self.labels = self.labels[keep[:i] == 1]
        self.data = self.data[keep[:i] == 1]
        self.num_annotated = len(self.labels)