Code Monkey home page Code Monkey logo

glimpse-android's Introduction

Glimpse

A content-aware cropping library for Android.

Give the right first impression with just a glimpse! Instead of center cropping images blindly, leverage Glimpse's eye to catch the right spot.

image

Setup

Add to top level gradle.build file

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

Add to app module gradle.build file

android {
    aaptOptions {
        noCompress "tflite"
        noCompress "lite"
    }
}

dependencies {
    implementation 'com.github.the-super-toys.glimpse-android:glimpse-core:0.0.3'
    
    //only required for glide extensions
    implementation 'com.github.the-super-toys.glimpse-android:glimpse-glide:0.0.3'
    
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
}

Usage

Init Glimpse in your Android app

class YourApp : Application() {
    override fun onCreate() {
        super.onCreate()
        Glimpse.init(this)
    }
}

Use Glimpse without extensions

First compute image's focal points by calling Bitmap.findCenter extension function.

val original = (imageView.drawable as BitmapDrawable).bitmap
val (x, y) = original.findCenter()

Then supply those focal points alongside with the width and height values of the target ImageView to Bitmap.crop extension function.

val cropped = original.crop(x, y, imageView.layoutParams.width, imageView.layoutParams.height)
imageView.setImageBitmap(cropped)

Use Glimpse with Glide extension

If you are using Glide for image loading you can just add GlimpseTransformation to GlideRequest builder:

GlideApp.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transform(GlimpseTransformation())
                .into(imageView)

It is recommended to set diskCacheStrategy(DiskCacheStrategy.RESOURCE) to cache the cropped bitmap by Glimpse, otherwise focal points will be calculated every time the image is displayed.

What about other image loaders such as Picasso and Fresco?

We tried to ship Glimpse with both Picasso and Fresco extensions but we were not able to find the right api.

We opened an issue on Picasso repository and a labeled Fresco question on Stack Overflow asking for guidance, but sadly we did not find much support. If you know how to assemble Glimpse cropping functionality to either lib without disrupting their original pipeline, please open an issue to review together the proposal. We really want to offer support for all the popular loading image libraries!

Java callers

If you're using Java you can take a look at this file to see how Glimpse api looks for Java callers.

Sample app

:sample-app module showcase Glimpse usage by making use of Glide extensions. The application is also published on Google Play.

Is Glimpse ready for production?

Actually, it depends. Ideally you should not use Glimpse to crop the same image over and over. Even if you use Glide extension which caches the output transformation, the underlying calculation will be performed in every android client. Thus, if possible, send to your cloud solution the x and y focal points of the calculated crop alongside the image when user uploads content.

We timed the crop calculation on an OnePlus 6 and in average it takes 30 milliseconds. Depending on the device and your specific use case Glimpse may or may not be ready for production. In any case we encourage you to provide feedback to help us make Glimpse a mature library.

How much increase APK's size?

If you're already using TensorFlow lite in your app, adding Glimpse costs only the size of the model, which is 148 KB. But chances are that you are not using it, so you have to take into account also the size of TensorFlow lite, which is about 300 KB.

Let's go DEEPER (The Deep Learning Model)

If you want to know how Glimpse works in a more deeper lever, take a look at this repository, where you can see the architecture and implementation of the Deep Learning model used by Glimpse. We explain how everything works in a much more technical level, and with all details, so you can reproduce it pn your own, and improve it if you want.

But if you do, don't hesitate to submit a Pull Request to that repository so we can keep improving Glimpse!!

glimpse-android's People

Contributors

pabloppp avatar victoralbertos avatar

Watchers

Rocky avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.