Ejemplo n.º 1
0
def _get_img_with_all_operations():
    logger.debug(operations)

    b = operations.brightness
    c = operations.contrast
    s = operations.sharpness

    img = _img_preview
    if b != 0:
        img = img_helper.brightness(img, b)

    if c != 0:
        img = img_helper.contrast(img, c)

    if s != 0:
        img = img_helper.sharpness(img, s)

    if operations.rotation_angle:
        img = img_helper.rotate(img, operations.rotation_angle)

    if operations.flip_left:
        img = img_helper.flip_left(img)

    if operations.flip_top:
        img = img_helper.flip_top(img)

    if operations.size:
        img = img_helper.resize(img, *operations.size)

    return img
Ejemplo n.º 2
0
def init():
    """Get and parse parameters from console"""

    args = sys.argv[1:]

    if len(args) == 0:
        logger.error("-p can't be empty")
        raise ValueError("-p can't be empty")

    logger.debug(f"run with params: {args}")

    # transform arguments from console
    opts, rem = getopt.getopt(
        args, "p:",
        ["rotate=", "resize=", "color_filter=", "flip_top", "flip_left"])
    rotate_angle = resize = color_filter = flip_top = flip_left = None

    path = None
    for opt, arg in opts:
        if opt == "-p":
            path = arg
        elif opt == "--rotate":
            rotate_angle = int(arg)
        elif opt == "--resize":
            resize = arg
        elif opt == "--color_filter":
            color_filter = arg
        elif opt == "--flip_top":
            flip_top = True
        elif opt == "--flip_left":
            flip_left = arg

    if not path:
        raise ValueError("No path")

    img = img_helper.get_img(path)
    if rotate_angle:
        img = img_helper.rotate(img, rotate_angle)

    if resize:
        w, h = map(int, resize.split(','))
        img = img_helper.resize(img, w, h)

    if color_filter:
        img = img_helper.color_filter(img, color_filter)

    if flip_left:
        img = img_helper.flip_left(img)

    if flip_top:
        img = img_helper.flip_top(img)

    if __debug__:
        img.show()
Ejemplo n.º 3
0
def _get_img_with_all_operations():
    b = operations.brightness
    c = operations.contrast
    s = operations.sharpness

    img = _img_preview
    if b != 0:
        img = img_helper.brightness(img, b)

    if c != 0:
        img = img_helper.contrast(img, c)

    if s != 0:
        img = img_helper.sharpness(img, s)

    if operations.rotation_angle:
        img = img_helper.rotate(img, operations.rotation_angle)

    if operations.flip_left:
        img = img_helper.flip_left(img)

    if operations.flip_top:
        img = img_helper.flip_top(img)

    if operations.size:
        img = img_helper.resize(img, *operations.size)

    if operations.red != operations.red_prev:
        img = img_helper.hist_red(img, operations.red, operations.red_prev)
        operations.red_prev = operations.red

    if operations.green != operations.green_prev:
        img = img_helper.hist_green(img, operations.green,
                                    operations.green_prev)
        operations.green_prev = operations.green

    if operations.blue != operations.blue_prev:
        img = img_helper.hist_blue(img, operations.blue, operations.blue_prev)
        operations.blue_prev = operations.blue

    return img
Ejemplo n.º 4
0
    def interpreter(self, s, img, img_path=''):
        if s.data == 'tuning':
            tmp = 1 if s.children[0] == 'increase' else -1
            attr = s.children[1]
            val = int(s.children[2])

            print(tmp, attr, val)
        elif s.data == 'set_filter':
            filter_name = s.children[0]
            return img_helper.color_filter(img, filter_name)

        elif s.data == 'flip':
            flip_dir = s.children[0]
            if flip_dir == 'vertically':
                return img_helper.flip_top(img)
            elif flip_dir == 'horizontally':
                return img_helper.flip_left(img)

        elif s.data == 'detect':
            print(img_path)
            if self.yolo is None:
                self.yolo = Yolo()
                self.agender = Agender()
                self.emotion = Emotion()

            if img_path != self.img_path:
                self.img_path = img_path
                self.persons = []
                self.objects = []

            objects = self.yolo.detect(img_path)
            faces = self.agender.detect(img_path)
            emotions = self.emotion.detect(img_path)

            font_path = "/Users/woffee/www/language_design/prototype/font_consolas/CONSOLA.TTF"
            font = ImageFont.truetype(font_path, 20)
            fw = 11
            fh = 16

            msg = []
            draw = ImageDraw.Draw(img)

            for i, box in enumerate(objects):
                l = box['left']
                t = box['top']
                b = box['bottom']
                r = box['right']
                label = box['class']

                self.objects.append({
                    'left': l,
                    'top': t,
                    'bottom': b,
                    'right': r,
                    'class': label
                })

                if label == 'person':
                    self.persons.append({
                        'left': l,
                        'top': t,
                        'bottom': b,
                        'right': r,
                        'gender': '',
                        'age': 0,
                        'emotion': ''
                    })
                else:
                    draw.rectangle(((l, t), (r, b)), outline='blue')
                    txt_width = fw * len(label)
                    draw.rectangle(((l, t), (l + txt_width, t + fh)),
                                   fill="blue")
                    draw.text((l, t), label, font=font)

            for i, box in enumerate(faces):
                l = box['left']
                t = box['top']
                b = box['bottom']
                r = box['right']
                gender = 'male' if box['gender'] < 0.5 else 'female'
                age = box['age']
                label = gender + ", %.2f" % age
                print(" * Agender: " + label)
                # msg.append(" * Agender: " + label)

                score = 0
                for i, p in enumerate(self.persons):
                    area = self.computeArea(l, t, r, b, p['left'], p['top'],
                                            p['right'], p['bottom'])
                    s = area / ((r - l) * (b - t))
                    if s > 0.5:
                        self.persons[i]['age'] = age
                        self.persons[i]['gender'] = gender

            for i, box in enumerate(emotions):
                l = box['left']
                t = box['top']
                b = box['bottom']
                r = box['right']
                emo = box['emotion']

                print(" * Emotion: " + emo)
                # msg.append(" * Emotion: " + emo)

                for i, p in enumerate(self.persons):
                    area = self.computeArea(l, t, r, b, p['left'], p['top'],
                                            p['right'], p['bottom'])
                    if (r - l) * (b - t) > 0:
                        s = area / ((r - l) * (b - t))
                        if s > 0.5:
                            self.persons[i]['emotion'] = emo

                # draw.rectangle(((l, t), (r, b)), outline='yellow')
                # txt_width = fw * len(emo)
                # draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="black")
                # draw.text((l, t), emo, font=font)

            for i, box in enumerate(self.persons):
                l = box['left']
                t = box['top']
                b = box['bottom']
                r = box['right']
                draw.rectangle(((l, t), (r, b)), outline='blue')

                gender_age = box['gender']
                if box['age'] > 0:
                    gender_age = gender_age + ", age %.2f" % box['age']
                emotion = box['emotion']

                txt_width = fw * len(gender_age)
                draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue")
                draw.text((l, t), gender_age, font=font)

                txt_width = fw * len(emotion)
                draw.rectangle(((l, t + fh), (l + txt_width, t + fh * 2)),
                               fill="blue")
                draw.text((l, t + fh), emotion, font=font)

            self.msg = " * done"

        elif s.data == 'how_many':
            str = s.children[0]
            str = self.remove_s(str)
            num = 0
            for obj in self.objects:
                if obj['class'] == str:
                    num += 1
            self.msg = " * %d %s(s)" % (num, str)

        elif s.data == 'tag_objects':
            img = self.original_img.copy()

            str = s.children[0]
            str = self.remove_s(str)

            msg = []
            draw = ImageDraw.Draw(img)

            font_path = "/Users/woffee/www/language_design/prototype/font_consolas/CONSOLA.TTF"
            font = ImageFont.truetype(font_path, 20)
            fw = 11
            fh = 16

            for i, box in enumerate(self.objects):
                if str == box['class']:
                    l = box['left']
                    t = box['top']
                    b = box['bottom']
                    r = box['right']
                    label = box['class']
                    print(" * Yolo: " + label)
                    msg.append(" * Yolo: " + label)

                    draw.rectangle(((l, t), (r, b)), outline='blue')
                    txt_width = fw * len(label)
                    draw.rectangle(((l, t), (l + txt_width, t + fh)),
                                   fill="blue")
                    draw.text((l, t), label, font=font)

            self.msg = "\n".join(msg)

        elif s.data == 'show_statistics':
            statistics = {}
            for o in self.objects:
                if o['class'] not in statistics.keys():
                    statistics[o['class']] = 1
                else:
                    statistics[o['class']] += 1
            msg = []
            for k in statistics.keys():
                v = statistics[k]
                msg.append(" * %d %s(s)" % (v, k))
            self.msg = "\n".join(msg)

        elif s.data == 'detect_food':
            self.detect_food(img_path)

        elif s.data == 'image_segmentation':
            img = self.image_segmentation(img_path)

        return img