-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblurry.go
62 lines (49 loc) · 1.25 KB
/
blurry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Author: Muhamad Surya Iksanudin<surya.kejawen@gmail.com>
package identify
import (
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
)
type BlurryDetection struct {
IsBlur bool
Score float64
}
func (b BlurryDetection) Detect(ImagePath string) (BlurryDetection, error) {
if !b.commandAvailable() {
return b, errors.New("ImageMagick is not available.")
}
if !b.validate(ImagePath) {
return b, errors.New("Please remove space in your image.")
}
output, err := b.run(ImagePath)
if err != nil {
return b, err
}
result := strings.Split(output, "\n")
b.Score, _ = strconv.ParseFloat(strings.Trim(strings.Split(result[len(result)-2], "(")[1], ")"), 64)
if 0.1 > b.Score {
b.IsBlur = true
} else {
b.IsBlur = false
}
return b, nil
}
func (b BlurryDetection) commandAvailable() bool {
cmd := exec.Command("identify", "-version")
_, err := cmd.Output()
return err == nil
}
func (b BlurryDetection) validate(ImagePath string) bool {
return len(strings.Split(ImagePath, " ")) == 1
}
func (b BlurryDetection) run(ImagePath string) (string, error) {
cmd := exec.Command("bash", "-c", fmt.Sprintf("identify -verbose %s | grep deviation", ImagePath))
stdout, err := cmd.Output()
if err != nil {
return "", err
}
return string(stdout), err
}