smartcrop

package module
v0.0.0-...-54ace51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2017 License: MIT Imports: 13 Imported by: 0

README

smartcrop.go

smartcrop implementation in Go

smartcrop finds good crops for arbitrary images and crop sizes, based on Jonas Wagner's smartcrop.js

Example Image: https://www.flickr.com/photos/usfwspacific/8182486789 CC BY U.S. Fish & Wildlife

Installation

Make sure you have a working Go environment. See the install instructions.

Additionally you need to have opencv installed.

You can install it on Mac OS X using:

brew tap homebrew/science
brew install opencv

On Linux you need to have the following packages installed:

libcv-dev libopencv-dev libopencv-contrib-dev libhighgui-dev libopencv-photo-dev libopencv-imgproc-dev libopencv-stitching-dev libopencv-superres-dev libopencv-ts-dev libopencv-videostab-dev 

Now you can install smartcrop, simply run:

go get github.com/muesli/smartcrop

To compile it from source:

git clone git://github.com/muesli/smartcrop.git
cd smartcrop && go build && go test -v

Example

package main

import (
	"github.com/muesli/smartcrop"
	"fmt"
	"image"
	_ "image/png"
	"os"
)

func main() {
  fi,err := os.Open("test.png")
  if err != nil {
    log.Fatalf(err.Error())
  }

  defer fi.Close()

  img, _, err := image.Decode(fi)
  if err != nil {
    log.Fatalf(err.Error())
  }

  analyzer := smartcrop.NewAnalyzer()
	topCrop, _ := analyzer.FindBestCrop(img, 250, 250)
	fmt.Printf("Top crop: %+v\n", topCrop)
}

With face detection:

func main() {
  fi,err := os.Open("test.png")
  if err != nil {
    log.Fatalf(err.Error())
  }

  defer fi.Close()

  img, _, err := image.Decode(fi)
  if err != nil {
    log.Fatalf(err.Error())
  }

  settings := smartcrop.CropSettings{
    FaceDetection:                    true,
    FaceDetectionHaarCascadeFilepath: "./files/aarcascade_frontalface_alt.xml",
  }
  analyzer := smartcrop.NewAnalyzerWithCropSettings(settings)
  topCrop, _ := analyzer.FindBestCrop(img, 250, 250)
  fmt.Printf("Top crop: %+v\n", topCrop)
}

Also see the test-cases in crop_test.go for further working examples.

Development

API docs can be found here.

Join us on IRC: irc.freenode.net/#smartcrop

Continuous integration: Build Status

Documentation

Overview

Package smartcrop implements a content aware image cropping library based on Jonas Wagner's smartcrop.js https://github.com/jwagner/smartcrop.js

Package smartcrop implements a content aware image cropping library based on Jonas Wagner's smartcrop.js https://github.com/jwagner/smartcrop.js

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Analyzer

type Analyzer interface {
	FindBestCrop(img image.Image, width, height int) (Crop, error)
}

Analyzer interface analyzes its struct and returns the best possible crop with the given width and height returns an error if invalid

func NewAnalyzer

func NewAnalyzer() Analyzer

NewAnalyzer returns a new analyzer with default settings

func NewAnalyzerWithCropSettings

func NewAnalyzerWithCropSettings(cropSettings CropSettings, log Logger) Analyzer

NewAnalyzerWithCropSettings returns a new analyzer with the given settings

type Crop

type Crop struct {
	X      int
	Y      int
	Width  int
	Height int
	Score  Score
}

Crop contains results

func SmartCrop

func SmartCrop(img image.Image, width, height int) (Crop, error)

SmartCrop applies the smartcrop algorithms on the the given image and returns the top crop or an error if somthing went wrong.

type CropSettings

type CropSettings struct {
	FaceDetection                    bool
	FaceDetectionHaarCascadeFilepath string
	InterpolationType                resize.InterpolationFunction
	DebugMode                        bool
	Prescale                         bool
	PrescaleValue                    float64
}

CropSettings contains options to change cropping behaviour

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Printf(format string, args ...interface{})
}

The Logger interface

type Score

type Score struct {
	Detail     float64
	Saturation float64
	Skin       float64
	Total      float64
}

Score contains values that classify matches

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL