-
Notifications
You must be signed in to change notification settings - Fork 9
/
imagehash.go
84 lines (64 loc) · 1.82 KB
/
imagehash.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package service
import (
"context"
"github.com/corona10/goimagehash"
iiifconfig "github.com/go-iiif/go-iiif/v4/config"
iiifimage "github.com/go-iiif/go-iiif/v4/image"
_ "log"
)
func init() {
ctx := context.Background()
err := RegisterService(ctx, "imagehash", initImageHashService)
if err != nil {
panic(err)
}
}
func initImageHashService(ctx context.Context, cfg *iiifconfig.Config, im iiifimage.Image) (Service, error) {
return NewImageHashService(cfg.ImageHash, im)
}
type ImageHashService struct {
Service `json:",omitempty"`
ImageHashContext string `json:"@context"`
ImageHashProfile string `json:"profile"`
ImageHashLabel string `json:"label"`
ImageHashAvg string `json:"average,omitempty"`
ImageHashDiff string `json:"difference,omitempty"`
}
func (s *ImageHashService) Context() string {
return s.ImageHashContext
}
func (s *ImageHashService) Profile() string {
return s.ImageHashProfile
}
func (s *ImageHashService) Label() string {
return s.ImageHashLabel
}
func (s *ImageHashService) Value() interface{} {
return map[string]string{
"average": s.ImageHashAvg,
"difference": s.ImageHashDiff,
}
}
func NewImageHashService(cfg iiifconfig.ImageHashConfig, image iiifimage.Image) (Service, error) {
im, err := iiifimage.IIIFImageToGolangImage(image)
if err != nil {
return nil, err
}
// please do these concurrently
avg_hash, err := goimagehash.AverageHash(im)
if err != nil {
return nil, err
}
diff_hash, err := goimagehash.DifferenceHash(im)
if err != nil {
return nil, err
}
s := ImageHashService{
ImageHashContext: "x-urn:service:go-iiif#imagehash",
ImageHashProfile: "x-urn:service:go-iiif#imagehash",
ImageHashLabel: "x-urn:service:go-iiif#imagehash",
ImageHashAvg: avg_hash.ToString(),
ImageHashDiff: diff_hash.ToString(),
}
return &s, nil
}