-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage.go
140 lines (113 loc) · 3.07 KB
/
image.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package clarifai
import (
"encoding/base64"
"io/ioutil"
"net/http"
"os"
)
type Image struct {
Concepts []map[string]interface{} `json:"concepts,omitempty"`
Metadata interface{} `json:"metadata,omitempty"`
Properties *ImageProperties `json:"image, omitempty"`
}
type ImageData struct {
Concepts []map[string]interface{} `json:"concepts,omitempty"`
Metadata interface{} `json:"metadata,omitempty"`
Properties *ImageProperties `json:"image, omitempty"`
}
type ImageProperties struct {
AllowDuplicateURL bool `json:"allow_duplicate_url,omitempty"`
Base64 string `json:"base64,omitempty"`
URL string `json:"url,omitempty"`
Crop []float32 `json:"crop,omitempty"`
}
// SupportedMimeTypes is a map of supported image types
// as per https://developer-preview.clarifai.com/guide/#supported-types
var SupportedMimeTypes map[string]struct{}
func init() {
SupportedMimeTypes = map[string]struct{}{
"image/bmp": struct{}{},
"image/jpeg": struct{}{},
"image/png": struct{}{},
"image/tiff": struct{}{},
}
}
// NewImageFromURL instantiates a new image based on URL.
func NewImageFromURL(url string) *Image {
return &Image{
Properties: &ImageProperties{
URL: url,
},
}
}
// NewImageFromURL instantiates a new image from a local file.
func NewImageFromFile(path string) (*Image, error) {
base64Str, err := addFromBase64(path)
if err != nil {
return &Image{}, err
}
return &Image{
Properties: &ImageProperties{
Base64: base64Str,
},
}, nil
}
// AllowDuplicates enables image duplicates.
func (i *Image) AllowDuplicates() {
if i.Properties == nil {
i.Properties = &ImageProperties{}
}
i.Properties.AllowDuplicateURL = true
}
// AddMetadata adds an image metadata.
func (i *Image) AddMetadata(m interface{}) {
i.Metadata = m
}
// AddCrop adds an image metadata.
func (i *Image) AddCrop(args ...float32) {
if i.Properties == nil {
i.Properties = &ImageProperties{}
}
for _, v := range args {
i.Properties.Crop = append(i.Properties.Crop, v)
}
}
// AddConcept adds an image concept.
func (i *Image) AddConcept(id string, value interface{}) {
i.Concepts = append(i.Concepts, map[string]interface{}{
"id": id,
"value": value,
})
}
// AddConcepts adds a list of concepts to an image.
func (i *Image) AddConcepts(c []string) {
for _, v := range c {
i.AddConcept(v, true)
}
}
// addFromBase64 reads a local image, validates it and returns it as a base64 string.
func addFromBase64(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
valErr := validateLocalFile(data)
if valErr != nil {
return "", valErr
}
return base64.StdEncoding.EncodeToString(data), nil
}
// validateLocalFile validates contents of the locally provided image file.
func validateLocalFile(data []byte) error {
mimeType := http.DetectContentType(data)
_, ok := SupportedMimeTypes[mimeType]
if !ok {
return ErrUnsupportedMimeType
}
return nil
}