-
Notifications
You must be signed in to change notification settings - Fork 0
/
msiextract_image_metadata.go
276 lines (212 loc) · 6.35 KB
/
msiextract_image_metadata.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"cloud.google.com/go/translate"
vision "cloud.google.com/go/vision/apiv1"
"compress/gzip"
"github.com/jjacquay712/GoRODS/msi"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/mknote"
"github.com/rwcarlsen/goexif/tiff"
"golang.org/x/net/context"
"golang.org/x/text/language"
"google.golang.org/api/option"
"io"
"log"
"path/filepath"
"strings"
"unsafe"
)
// #cgo CFLAGS: -I/usr/include/irods
// #cgo LDFLAGS: -lirods_server -lirods_common -lpthread
/*
#include "msParam.h"
#include "re_structs.h"
*/
import "C"
const API_AUTH_FILE = "/etc/irods/iRODS-UGM-Demo.json"
//export ExtractImageMetadata
func ExtractImageMetadata(imagePath *C.msParam_t, enableGzipInt *C.msParam_t, rei *C.ruleExecInfo_t) int {
// Setup GoRODS/msi
msi.Configure(unsafe.Pointer(rei))
// Convert input to Golang types
imageFilePath := msi.ToParam(unsafe.Pointer(imagePath)).String()
enableGzip := msi.ToParam(unsafe.Pointer(enableGzipInt)).Int() > 0
// Filter out non-images
validExtensions := []string{".jpg", ".png", ".gif"}
ext := strings.ToLower(filepath.Ext(imageFilePath))
if !Contains(validExtensions, ext) {
return msi.SUCCESS
}
log.Printf("msiextract_image_metadata: Extracting metadata for %v", imageFilePath)
// Extract image metadata via machine learning API
labelsKVP := GetImageLabels(imageFilePath, API_AUTH_FILE).ToKVP()
// Extract exif metadata
exifKVP := ExtractExifData(imageFilePath)
if enableGzip {
gzipDataObjPath := imageFilePath + ".gz"
gzipDesc := msi.NewParam(msi.INT_MS_T)
if err := msi.Call("msiDataObjCreate", gzipDataObjPath, "", gzipDesc); err != nil {
log.Print(err)
return msi.SYS_INTERNAL_ERR
}
gzipDataObj := msi.NewObjReaderFromDesc(gzipDesc)
defer gzipDataObj.Close()
gzWriter := gzip.NewWriter(gzipDataObj)
defer gzWriter.Close()
origDataObj, err := msi.NewObjReader(imageFilePath)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
defer origDataObj.Close()
io.Copy(gzWriter, origDataObj)
imageFilePath = gzipDataObjPath
}
// Associate metadata to data object
if err := msi.Call("msiAssociateKeyValuePairsToObj", labelsKVP, imageFilePath, "-d"); err != nil {
log.Print(err)
return msi.SYS_INTERNAL_ERR
}
if err := msi.Call("msiAssociateKeyValuePairsToObj", exifKVP, imageFilePath, "-d"); err != nil {
log.Print(err)
return msi.SYS_INTERNAL_ERR
}
return msi.SUCCESS
}
type ExifWalker struct {
kvpMap map[string]string
}
func NewExifWalker() *ExifWalker {
w := new(ExifWalker)
w.kvpMap = make(map[string]string)
return w
}
func (w *ExifWalker) Walk(name exif.FieldName, tag *tiff.Tag) error {
w.kvpMap["exif_"+string(name)] = tag.String()
return nil
}
func ExtractExifData(rodsPath string) *msi.Param {
file, err := msi.NewObjReader(rodsPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
exif.RegisterParsers(mknote.All...)
walker := NewExifWalker()
exifData, err := exif.Decode(file)
if err != nil {
log.Print(err)
return msi.NewParam(msi.KeyValPair_MS_T).SetKVP(walker.kvpMap)
}
if wErr := exifData.Walk(walker); wErr != nil {
log.Print(wErr)
}
return msi.NewParam(msi.KeyValPair_MS_T).SetKVP(walker.kvpMap)
}
type ImgLabel struct {
english string
dutch string
}
type ImgLabels []ImgLabel
func (labels ImgLabels) FetchTranslations(apiAuthFile string) {
words := make([]string, len(labels))
for i, label := range labels {
words[i] = label.english
}
translations := TranslateString("nl", words, apiAuthFile)
for i, translation := range translations {
labels[i].dutch = translation
}
}
func (labels ImgLabels) ToKVP() *msi.Param {
metaMap := make(map[string]string)
metaMap["tags_english"] = ""
metaMap["tags_dutch"] = ""
for _, label := range labels {
metaMap["tags_english"] += label.english + ","
metaMap["tags_dutch"] += label.dutch + ","
}
metaMap["tags_english"] = strings.TrimRight(metaMap["tags_english"], ",")
metaMap["tags_dutch"] = strings.TrimRight(metaMap["tags_dutch"], ",")
return msi.NewParam(msi.KeyValPair_MS_T).SetKVP(metaMap)
}
func (labels ImgLabels) SetEnglish(enLabels []string) {
for i, label := range enLabels {
labels[i].english = label
}
}
var translateClient *translate.Client
var visionClient *vision.ImageAnnotatorClient
func GetImageLabels(filepath, apiAuthFile string) (imageLabels ImgLabels) {
authOption := option.WithServiceAccountFile(apiAuthFile)
ctx := context.Background()
// Lazy load single client
if visionClient == nil {
// Creates a client.
client, err := vision.NewImageAnnotatorClient(ctx, authOption)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
visionClient = client
}
file, err := msi.NewObjReader(filepath)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
defer file.Close()
image, err := vision.NewImageFromReader(file)
if err != nil {
log.Fatalf("Failed to create image: %v", err)
}
labels, err := visionClient.DetectLabels(ctx, image, nil, 10)
if err != nil {
log.Fatalf("Failed to detect labels: %v", err)
}
enLabels := make([]string, len(labels))
for i, label := range labels {
enLabels[i] = label.Description
}
imageLabels = make(ImgLabels, len(labels))
imageLabels.SetEnglish(enLabels)
// Translate the labels into dutch
imageLabels.FetchTranslations(apiAuthFile)
return
}
func TranslateString(targetLang string, words []string, apiAuthFile string) []string {
authOption := option.WithServiceAccountFile(apiAuthFile)
ctx := context.Background()
// Lazy load single client
if translateClient == nil {
// Creates a client.
client, err := translate.NewClient(ctx, authOption)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
translateClient = client
}
// Sets the target language.
target, err := language.Parse(targetLang)
if err != nil {
log.Fatalf("Failed to parse target language: %v", err)
}
translations, err := translateClient.Translate(ctx, words, target, nil)
if err != nil {
log.Fatalf("Failed to translate text: %v", err)
}
var responseTranslations []string
for _, translation := range translations {
responseTranslations = append(responseTranslations, translation.Text)
}
return responseTranslations
}
func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func main() {
// We need the main function to make possible
// CGO compiler to compile the package as C static archive
}