muesli / smartcrop Goto Github PK
View Code? Open in Web Editor NEWsmartcrop finds good image crops for arbitrary crop sizes
License: MIT License
smartcrop finds good image crops for arbitrary crop sizes
License: MIT License
Hello,
I have been trying to use smartcrop for my use case (usually, making landscape thumbnails of portrait images of people), and in my completely unscientific investigations, it seems that the current smartcrop implementation picks any body part with no particular preference for face. That interpretation is supported by the fact that face detection has been dropped last year.
Here are the smart crops of images on #33 - the issue is very similar to mine.
I understand that having native dependencies makes building more complicated and confusing, and therefore, it is good to have a pure Go package. OpenCV 3 seems to make things even more complicated.
So, I remember seeing some comments about restoring OpenCV-based face detection as a separate optional package. What is the status of that effort? Would it be possible to somehow help with it?
Also, I wonder if using https://github.com/hybridgroup/gocv would make integration with OpenCV 3 easier, since https://github.com/go-opencv/go-opencv seems to have some struggles with supporting OpenCV 3.
Hey there!
I have a question and not sure how to solve this. I'm trying to detect a face and only cut out a rectangle with 200x200 size.
Currently I am always getting crops like this:
`smartcrop.Crop{X:0, Y:20, Width:598, Height:450, Score:smartcrop.Score{Detail:10.490105771557365, Saturation:55.443967954895136, Skin:271.62173185696514, Total:0.0029439244310097267}}``
I searched the code a bit and found a minScale setting and a prescale setting that's always true.
My first idea was to make them configurable and change prescaleMin to about 100. But that does not improve results.
I am not to sure what's going on here. Can you give me a hint on how to solve this?
$ go test -v
package github.com/muesli/smartcrop (test)
imports github.com/muesli/smartcrop/nfnt
imports github.com/nfnt/resize: cannot find package "github.com/nfnt/resize" in any of:
/$GOPATH/src/github.com/vendor/github.com/nfnt/resize (vendor tree)
/usr/local/go/src/github.com/nfnt/resize (from $GOROOT)
/$GOPATH/src/github.com/nfnt/resize (from $GOPATH)
FAIL github.com/muesli/smartcrop [setup failed]
I'm getting the following error with OpenCV 3.0 installed. Just curious whether smartcrop is known to work with OpenCV 3.0. If not which version is safer to use?
0.625
original resolution: 960x640
scale: 1.000000, cropw: 400.000000, croph: 400.000000, minscale: 1.000000
Time elapsed edge: 87.613185ms
OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /home/admin/source/opencv/opencv-3.0.0/modules/core/src/persistence.cpp, line 4976
terminate called after throwing an instance of 'cv::Exception'
what(): /home/admin/source/opencv/opencv-3.0.0/modules/core/src/persistence.cpp:4976: error: (-2) The node does not represent a user object (unknown type?) in function cvRead
SIGABRT: abort
PC=0x7f7a6c6f7165
signal arrived during cgo execution
goroutine 15 [syscall, locked to thread]:
runtime.cgocall_errno(0x40c230, 0xc2081aae40, 0x0)
/usr/local/go/src/runtime/cgocall.go:130 +0xf5 fp=0xc2081aae20 sp=0xc2081aadf8
github.com/lazywei/go-opencv/opencv._Cfunc_cvLoadHaarClassifierCascade(0x223d540, 0x100000001, 0x0)
/home/admin/go/src/github.com/lazywei/go-opencv/opencv/:677 +0x44 fp=0xc2081aae40 sp=0xc2081aae20
github.com/lazywei/go-opencv/opencv.LoadHaarClassifierCascade(0xbcb430, 0x3e, 0x7f7a72ca8ac0)
/home/admin/go/src/github.com/lazywei/go-opencv/opencv/cvaux.go:61 +0x90 fp=0xc2081aae78 sp=0xc2081aae40
github.com/muesli/smartcrop.faceDetect(0xc20819c3e0, 0xc2089ad180)
/home/admin/go/src/github.com/muesli/smartcrop/crop.go:420 +0x173 fp=0xc2081ab088 sp=0xc2081aae78
github.com/muesli/smartcrop.analyse(0xc20819c3e0, 0x4079000000000000, 0x4079000000000000, 0x3ff0000000000000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/home/admin/go/src/github.com/muesli/smartcrop/crop.go:284 +0x31e fp=0xc2081ab2d8 sp=0xc2081ab088
github.com/muesli/smartcrop.SmartCrop(0xc20819d940, 0x280, 0x280, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
Because the repository name ends in ".go", the the "go get" tool fails to properly install the package. You can either use "-go", or remove it entirely and just have the package called "smartcrop".
When I compile binary natively on MacOS - it's all ok:
$ go build -o ./warehouse-macos github.com/me/warehouse/cmd/warehouse
$ ls -lh ./warehouse-macos
-rwxr-xr-x 1 user user 9.2M Apr 24 16:22 ./warehouse-macos
But when I try to compile on MacOS for Linux I get an error:
$ GOOS=linux GOARCH=amd64 go build -o ./warehouse-linux github.com/me/warehouse/cmd/warehouse
# github.com/muesli/smartcrop
../../../../muesli/smartcrop/crop.go:440: undefined: opencv.LoadHaarClassifierCascade
../../../../muesli/smartcrop/crop.go:443: undefined: opencv.FromImage
What I doing wrong?
go get
takes a long time I believe because of the size of all the images in the sample folder. Are all those images necessary for the unit tests?
The current tagged version is rather old.
I'd love to see a binary release for the cli tool - I'm after a single-binary smartcrop implementation to put on a docker image. There's the official smartcrop-cli, but that brings whole nodejs into the fold, bumping the size of the image substantially. Any chance of getting a binary build here, that I can then just wget in? :D
Thanks!
Common sense seems to suggest it might be faster.
I wanted to try your repository but I believe I am missing some system dependencies?
go get github.com/muesli/smartcrop
# pkg-config --cflags opencv opencv opencv opencv opencv opencv opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
pkg-config: exit status 1
Any hints?
There are many issues that are necessary for this repository to be useful again,
I would submit pull requests if you'd like, but if you've already abadoned this repository I'd rather fork it.
What do you think? :/ would be a waste, since I think many people need this nowadays.
Hey @muesli, for most images, this library is working perfectly. Every now and then, there are crops that need a little adjustment.
Do you have any recommendations for improving the detection/crop model for images that crop incorrectly? I'd love to come up with a workflow for throwing a few before and after images into a folder, running ./train
, etc...
It might also be interesting to collect a library of models like the aarcascade_frontalface_alt.xml
mentioned in the readme. E.g. one for animals, sides of faces, etc...
Thanks for your time!
Having a native Go smartcrop implementation can be more desired in some cases; looking at the code it shoud be quite trivial to move opencv dependency to a separate (sub)package, this way main smartcrop package can be made pure-Go and if one needs improved face detection, this can be done importing separate Analyzer from an opencv-dependent package.
Like faceDetectionHaarCascade, useFaceDetect etc.
env: OS X 10.11.2
Go 1.5.2
OpenCV 2.4.12
Error when exec analyzer.FindBestCrop
:
stat /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml: no such file or directory
exit status 1
path created by homebrew is /usr/local/Cellar/opencv/2.4.12/share/OpenCV
, not /usr/share/opencv/
Is there any plan to add a CLI like smartcrop.js? It would be great to be able to use smartcrop from a shell.
Commit 1db4849 in master breaks backward compatibility
smartcrop.go
- func NewAnalyzer() Analyzer {
+ func NewAnalyzer(resizer options.Resizer) Analyzer {
- func NewAnalyzerWithCropSettings(cropSettings CropSettings) Analyzer {
+ func NewAnalyzerWithLogger(resizer options.Resizer, logger Logger) Analyzer {
- func SmartCrop(img image.Image, width, height int) (image.Rectangle, error) {
What the ...? It's not major version, and it's in master branch!
Thank you for maintaining this wonderful port from js lib
However I found the result sometimes is not the same as the original one. Not only the part of the image, but also the size of image is 512x512, even I use 300x300 in code.
package main
import (
"fmt"
"image"
"image/jpeg"
"math"
"os"
"github.com/muesli/smartcrop"
"github.com/muesli/smartcrop/nfnt"
)
func main() {
f, _ := os.Open("/tmp/origin.jpeg")
img, _, _ := image.Decode(f)
analyzer := smartcrop.NewAnalyzer(nfnt.NewDefaultResizer())
topCrop, _ := analyzer.FindBestCrop(img, 300, 300)
// The crop will have the requested aspect ratio, but you need to copy/scale it yourself
fmt.Printf("Top crop: %+v\n", topCrop)
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
croppedimg := img.(SubImager).SubImage(topCrop)
//save cropped image to file out.png
// Save cropped image to file
out, err := os.Create("/tmp/cropped.jpeg")
if err != nil {
fmt.Println("Failed to create the file:", err)
return
}
defer out.Close()
// Choose quality of the jpeg (1-100), higher is better quality
err = jpeg.Encode(out, croppedimg, &jpeg.Options{Quality: 90})
if err != nil {
fmt.Println("Failed to save the image:", err)
return
}
fmt.Println("Cropped image saved to /tmp/cropped.jpeg")
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.