Exemplo n.º 1
0
    def label_image_volc(self):
        # download images
        try:
            urllib.request.urlretrieve(self.image_url, IMAGE_LOCAL)
        except (URLError, HTTPError):
            raise RuntimeError("Failed to download '{}'".format(self.image_url))

        # Load TFLite model and allocate tensors.
        interpreter = tf.lite.Interpreter(MODEL_PATH)
        interpreter.allocate_tensors()

        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()

        # NxHxWxC, H:1, W:2
        height = input_details[0]['shape'][1]
        width = input_details[0]['shape'][2]
        img, input_data = self.load_images(IMAGE_LOCAL, height, width)
        # crop image for best display
        sc = smartcrop.SmartCrop()
        crop_r = sc.crop(img, 160, 90)
        crop_r['top_crop'].pop('score')
        crop_s = sc.crop(img, 100, 100)
        crop_s['top_crop'].pop('score')

        input_data = (np.float32(input_data) - INPUT_MEAN) / INPUT_STD

        interpreter.set_tensor(input_details[0]['index'], input_data)
        interpreter.invoke()
        output_data = interpreter.get_tensor(output_details[0]['index'])
        results = np.squeeze(output_data)

        results = self.format_results_volc(results, LABEL_PATH, crop_r['top_crop'], crop_s['top_crop'])
        return results
Exemplo n.º 2
0
async def get_smartcrop(request, width, height, url):
    async with httpx.AsyncClient(timeout=30) as client:
        response = await client.get(url)
        try:
            response.raise_for_status()
        except (httpx.RequestError, httpx.HTTPStatusError):
            return HttpResponseBadRequest()
        image_io = BytesIO()
        async for chunk in response.aiter_bytes():
            image_io.write(chunk)
        image_io.seek(0)
    sm = smartcrop.SmartCrop()
    image = Image.open(image_io)
    _width, _height = int(width), int(height)
    result = sm.crop(
        image=image,
        width=100,
        height=int(_height / _width * 100),
        prescale=True,
    )['top_crop']

    x, y = result['x'], result['y']
    w, h = result['width'], result['height']
    crop = image.crop((x, y, x + w, y + h)).resize((_width, _height))
    crop_io = BytesIO()
    crop.save(crop_io, format='png')
    crop_io.seek(0)
    return HttpResponse(crop_io.read(), content_type='image/png')
Exemplo n.º 3
0
    def create_thumb_py(self, mode=None, pth=None):
        """ Create the thumbnail using SmartCrop.py """
        if pth is None:
            raise ValueError("path can't be None")

        # Load smartcrop and set options
        sc = smartcrop.SmartCrop()

        # Get desired dimensions
        nwidth, nheight = self.resize_dims(mode)
        factor = nwidth / 100.0

        crop_width = 100 if settings.fast else nwidth
        crop_height = int(nheight / factor) if settings.fast else nheight
        logging.info("[%s] SmartCrop.py new dimensions: %ix%i" %
                     (self.name, nwidth, nheight))

        # Fix image mode if necessary
        img = self.original_image.copy()
        if not img.mode in ["RGB", "RGBA"]:
            newimg = Image.new("RGB", img.size)
            newimg.paste(img)
            img = newimg

        # Calculate the optimal crop size
        logging.info("[%s] SmartCrop.py computing optimal crop size." %
                     self.name)
        ret = sc.crop(img, crop_width, crop_height)
        box = (
            ret["top_crop"]["x"],
            ret["top_crop"]["y"],
            ret["top_crop"]["width"] + ret["top_crop"]["x"],
            ret["top_crop"]["height"] + ret["top_crop"]["y"],
        )

        # Do the actual crop
        nimg = self.original_image.crop(box)
        nimg.load()
        nimg.thumbnail((nwidth, nheight), Image.ANTIALIAS)

        # Create the filename and save the thumbnail
        logging.info("[%s] Saving SmartCrop.py thumbnail." % self.name)
        if settings.output_format == "jpg":
            nimg.save(
                pth,
                optimize=settings.jpeg_optimize,
                progressive=settings.jpeg_progressive,
                quality=settings.jpeg_quality,
            )
        else:
            nimg.save(pth)
        return pth
Exemplo n.º 4
0
    def create(self, validated_data):
        imgpath = MEDIA_ROOT + "/uploads/" + str(validated_data["image"])
        width = validated_data["width"]
        height = validated_data["height"]

        smartcropimage = SmartcropImage.objects.create(**validated_data)

        image = ImagePIL.open(imgpath)
        sc = smartcrop.SmartCrop()
        result = sc.crop(image, width, height)

        smartcropimage.result = result

        return smartcropimage
Exemplo n.º 5
0
def GenerateImage():
    sc = smartcrop.SmartCrop()

    masks = list(os.scandir("./masks"))
    images = list(os.scandir("./images"))
    images = random.sample(images, len(masks))

    result = Image.new("RGB", (1500, 500), (21, 32, 43))

    for [mask, image] in zip(masks, images):
        mask = Image.open(mask.path).convert('RGB')
        maskL = mask.convert("L")
        mask = numpy.array(mask)

        x, y, width, height = bbox(mask)

        src = Image.open(image.path).convert('RGB')

        bounds = sc.crop(src, width, height)["top_crop"]

        src = src.resize(
            (width, height),
            resample=Image.LANCZOS,
            box=(bounds["x"], bounds["y"], bounds["width"] + bounds["x"],
                 bounds["height"] + bounds["y"]))
        src = pad(src, x, y)

        src = numpy.array(src)
        dst = mask / 255 * src
        dst = Image.fromarray(dst.astype(numpy.uint8))

        result.paste(dst, (0, 0), maskL)

    timestr = time.strftime("%Y%m%d-%H%M%S")
    filename = f"./results/{timestr}_result.png"
    result.save(filename)

    api = twitter.Api(
        consumer_key=os.environ.get('TWITTER_API_KEY'),
        consumer_secret=os.environ.get('TWITTER_API_SECRET'),
        access_token_key=os.environ.get('TWITTER_ACCESS_KEY'),
        access_token_secret=os.environ.get('TWITTER_ACCESS_SECRET'))

    api.UpdateBanner(filename)
Exemplo n.º 6
0
    def create_thumb_py(self, mode=None, pth=None):
        """ Create the thumbnail using SmartCrop.py """
        if pth is None:
            raise ValueError("path can't be None")

        # Load smartcrop and set options
        sc = smartcrop.SmartCrop()
        crop_options = smartcrop.DEFAULTS

        # Get desired dimensions
        nwidth, nheight = self.resize_dims(mode)
        crop_options['width'] = nwidth
        crop_options['height'] = nheight

        # Fix image mode if necessary
        img = self.open_original()
        if not img.mode in ['RGB', 'RGBA']:
            newimg = Image.new('RGB', img.size)
            newimg.paste(img)
            img = newimg

        # Calculate the optimal crop size
        ret = sc.crop(img, crop_options)
        box = (ret['topCrop']['x'],
                ret['topCrop']['y'],
                ret['topCrop']['width'] + ret['topCrop']['x'],
                ret['topCrop']['height'] + ret['topCrop']['y'])

        # Do the actual crop
        img = self.open_original()
        nimg = img.crop(box)
        nimg.thumbnail((nwidth, nheight), Image.ANTIALIAS)

        # Create the filename and save the thumbnail
        if settings.output_format == 'jpg':
            nimg.save(pth, optimize=settings.jpeg_optimize, 
                    progressive=settings.jpeg_progressive, 
                    quality=settings.jpeg_quality)
        else:
            nimg.save(pth)
        return pth
Exemplo n.º 7
0
#!/usr/bin/env python

import sys

import smartcrop
from PIL import Image

image = Image.open(sys.argv[1])

sc = smartcrop.SmartCrop()
result = sc.crop(image, 100, 100)
print(result)