facethumbnail

package module
v0.0.0-...-02df5a9 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2020 License: MIT Imports: 9 Imported by: 2

README

facethumbnail

Thumbnail generator that preserves the face in the final image

Consider the image below

Sample Image

Now if we simply generate a square thumbnail from this, it will generate something like below

Sample Thumbnail

The head is cut off. facethumbnail tries to detect faces in pictures and attempts to choose a region of image so that the thumbnail contains the face properly, as follows

Sample Thumbnail

Facethumbnail also supports images with multiple faces and uses the largest face as the center of the thumbnail.

Build/Test

Clone this repo and in the folder run the following commands

go get
go build .
go test . -v

Sample

A sample use is

pwd, _ := os.Getwd()                                   // get the current directory
cascadeFile := path.Join(pwd, "test", "facefinder")    // get path to the facefinder binary
fd := GetFaceDetector(cascadeFile, -1, -1)             // create facedetector instance
result, err := ResizeImage(fd, srcPath, dstPath, 200)  // resize image!!

For more sample usage see the test file

How Does this work

This project uses github.com/esimov/pigo/core to detect the face, then generate thumbnail with the detected face centered.

Known issues

  1. Works for only jpeg

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FDetect

type FDetect struct {
	// contains filtered or unexported fields
}

FDetect struct contains Pigo face detector general settings.

func GetFaceDetector

func GetFaceDetector(cascadeFile string) *FDetect

GetFaceDetector returns an instance of facedetector with path to binary cascade file

func (*FDetect) DetectFacesInImageFile

func (fd *FDetect) DetectFacesInImageFile(sourceFilePath string) ([]image.Rectangle, error)

DetectFacesInImageFile detect faces in a image file

func (*FDetect) Init

func (fd *FDetect) Init(minSize, maxSize int) error

Init initializes facedetector with a cascadefile with expected minimum and maximum size of the face. min and max can be -1 to use default values

type FaceDetector

type FaceDetector interface {
	// Init initializes the face detector with maximum and minimum size of expected face
	Init(minSize, maxSize int) error

	// DetectFacesInImageFile detects a list of faces in an image file
	DetectFacesInImageFile(sourceFilePath string) ([]image.Rectangle, error)
}

faceDetector interface defines face detection interface ResizeImage expects

type ResizeResult

type ResizeResult struct {
	Center     image.Point
	FacesCount int
}

ResizeResult contains details of the resizing done

func ResizeImage

func ResizeImage(fd FaceDetector, srcPath, dstPath string, size uint) (ResizeResult, error)

ResizeImage uses an instance of FaceDetector to detect face in srcPath and generates a thumbnail of sizexsize in dstPath If no facedetector is given or no faces are detected, then the center of image is used for the thumbnail

func ResizeToAspectRatio

func ResizeToAspectRatio(fd FaceDetector, srcPath, dstPath string, w, h uint) (ResizeResult, error)

ResizeToAspectRatio crops image to meet the aspect ratio of w:h

Jump to

Keyboard shortcuts

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