/
photo_workflow.go
294 lines (245 loc) · 7.56 KB
/
photo_workflow.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package workflow
import (
"encoding/json"
"fmt"
"image"
"image/color"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"github.com/anish-krishnan/Tidepod/tidepod-server/entity"
"github.com/anish-krishnan/Tidepod/tidepod-server/util"
objectDetectionScript "github.com/anish-krishnan/Tidepod/tidepod-server/workflow/object_detection/scripts"
"github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/mapquest/open"
"github.com/disintegration/imaging"
"gorm.io/gorm"
)
// Geocoder used to convert lat/long coords of images
var geocoder geo.Geocoder
var mapquestAPIKEY string
// mutex used to prevent race condition on
// parallel database writes
var mutex sync.Mutex
// Database access point
var database *gorm.DB
// Queues for various photo workflows
var faceDetectQueue chan *entity.Photo
var labelPhotoQueue chan *entity.Photo
var completedTaskQueue chan *entity.Photo
func init() {
mapquestApiKeyRaw, err := ioutil.ReadFile("credentials/MapQuestAPIKEY.txt")
if err != nil {
log.Fatal(err)
}
mapquestAPIKEY = string(mapquestApiKeyRaw[:len(mapquestApiKeyRaw)-1])
geocoder = open.Geocoder(string(mapquestAPIKEY))
// buffer up to 10000 images before blocking the upload process
faceDetectQueue = make(chan *entity.Photo, 10000)
labelPhotoQueue = make(chan *entity.Photo, 10000)
completedTaskQueue = make(chan *entity.Photo)
go RunFaceDetect()
go LabelPhoto()
go GarbageCollector()
}
// RunPhotoWorkflow runs the workflows sequentially
func RunPhotoWorkflow(db *gorm.DB, photo *entity.Photo) {
database = db
createFormattedTempImage(photo.FilePath, "./photo_storage/TEMP/"+photo.FilePath)
CreatePhotoThumbnail(db, photo)
UpdatePhotoWithReadableLocation(db, photo)
faceDetectQueue <- photo
labelPhotoQueue <- photo
}
// Creates a formatted temporary image used by all workflows
func createFormattedTempImage(filename string, tempFilePath string) {
source, err := os.Open("./photo_storage/saved/" + filename)
if err != nil {
panic(err)
}
defer source.Close()
destination, err := os.Create(tempFilePath)
if err != nil {
panic(err)
}
_, err = io.Copy(destination, source)
if err != nil {
panic(err)
}
destination.Close()
util.UpdatePhotoRotation(tempFilePath)
}
// LabelPhoto takes a photo and runs it through the tensorflow object
// detection module. Updates database entry appropriately with labels
func LabelPhoto() {
for {
// Wait until a photo is received
photo := <-labelPhotoQueue
// Get Labels
labels, err := objectDetectionScript.GetLabelsForImage(photo.FilePath)
if err != nil {
panic(err)
}
labelEntries := []entity.Label{}
for _, label := range labels {
var labelEntry entity.Label
database.Where("label_name = ?", label).First(&labelEntry)
labelEntries = append(labelEntries, labelEntry)
}
mutex.Lock()
var newPhoto entity.Photo
database.First(&newPhoto, photo.ID)
newPhoto.Labels = labelEntries
database.Save(&newPhoto)
mutex.Unlock()
completedTaskQueue <- photo
}
}
// CreatePhotoThumbnail takes a photo, creates a 200x200 thumbnail
// and saves it to the photo_storage/thumbnails/ directory.
// It also rotates the thumbnail as needed
func CreatePhotoThumbnail(db *gorm.DB, photo *entity.Photo) {
photo.ThumbnailFilePath = photo.FilePath
db.Save(photo)
rotation := util.GetPhotoRotation("photo_storage/saved/" + photo.FilePath)
img, err := imaging.Open("photo_storage/saved/" + photo.FilePath)
if err != nil {
panic(err)
}
thumb := imaging.Thumbnail(img, 200, 200, imaging.CatmullRom)
if rotation != 0 {
thumb = imaging.Rotate(thumb, rotation, color.Gray{})
}
err = imaging.Save(thumb, "photo_storage/thumbnails/"+photo.FilePath)
if err != nil {
fmt.Println("ERROR saving thumbnail", photo.FilePath)
panic(err)
}
}
// RunFaceDetect takes a photo, and finds all faces in the image. It creates a
// new "Box" for each found face
func RunFaceDetect() {
for {
photo := <-faceDetectQueue
fileParts := strings.Split(photo.FilePath, ".")
ext := fileParts[len(fileParts)-1]
img, err := imaging.Open("./photo_storage/TEMP/" + photo.FilePath)
if err != nil {
panic(err)
}
// rec, err := face.NewRecognizer("./workflow")
// if err != nil {
// panic(err)
// }
// defer rec.Close()
// faces, err := rec.RecognizeFile("./photo_storage/TEMP/" + photo.FilePath)
// if err != nil {
// panic(err)
// }
// Encode the data
form := url.Values{}
form.Add("filename", photo.FilePath)
resp, err := http.PostForm("http://localhost:2999/faceDetect", form)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
fmt.Println("got body")
fmt.Println(string(body))
if err != nil {
log.Fatalln(err)
}
type Face struct {
MinX int `json:"minX"`
MinY int `json:"minY"`
MaxX int `json:"maxX"`
MaxY int `json:"maxY"`
}
type FaceData struct {
Faces []Face `json:"faces"`
}
var faceData FaceData
err = json.Unmarshal(body, &faceData)
for _, face := range faceData.Faces {
box := entity.Box{
PhotoID: photo.ID,
MinX: face.MinX,
MinY: face.MinY,
MaxX: face.MaxX,
MaxY: face.MaxY,
}
database.Create(&box)
box.FilePath = fmt.Sprintf("%d.%s", box.ID, ext)
database.Save(&box)
photo.Boxes = append(photo.Boxes, box)
// crop out a rectangular region
croppedImg := imaging.Crop(img, image.Rect(box.MinX, box.MinY, box.MaxX, box.MaxY))
// lower resolution
compresedCroppedImg := imaging.Thumbnail(croppedImg, 100, 100, imaging.CatmullRom)
// save cropped image
err = imaging.Save(compresedCroppedImg, "./photo_storage/boxes/"+box.FilePath)
if err != nil {
panic(err)
}
}
mutex.Lock()
var newPhoto entity.Photo
database.First(&newPhoto, photo.ID)
newPhoto.Boxes = photo.Boxes
database.Save(&newPhoto)
mutex.Unlock()
completedTaskQueue <- photo
}
}
// UpdatePhotoWithReadableLocation takes a photo, and uses the geo-coords
// to find a human readable address and updates the db entry appropriately
func UpdatePhotoWithReadableLocation(db *gorm.DB, photo *entity.Photo) {
if photo.Latitude == 0.0 && photo.Longitude == 0.0 {
return
}
request := fmt.Sprintf("https://www.mapquestapi.com/geocoding/v1/reverse?key=%s&location=%f%%2C%f&outFormat=json&thumbMaps=false",
mapquestAPIKEY,
photo.Latitude,
photo.Longitude)
resp, err := http.Get(request)
if err != nil {
panic(err)
}
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
location := result["results"].([]interface{})[0].(map[string]interface{})["locations"].([]interface{})[0].(map[string]interface{})
street := location["street"]
neighborhood := location["adminArea6"]
city := location["adminArea5"]
postalCode := location["postalCode"]
county := location["adminArea4"]
state := location["adminArea3"]
country := location["adminArea1"]
formattedAddress := fmt.Sprintf("%s, %s %s, %s, %s, %s, %s", street, neighborhood, city, county, postalCode, state, country)
db.Model(&photo).Updates(entity.Photo{LocationString: formattedAddress})
}
// GarbageCollector waits for all tasks associated with a photo to be
// completed before removing the TEMP file
func GarbageCollector() {
var activeTempImages map[int]int = make(map[int]int)
for {
photo := <-completedTaskQueue
activeTempImages[photo.ID]++
// Once 2 tasks have been completed, we can safely remove the temp image
if activeTempImages[photo.ID] >= 2 {
err := os.Remove("./photo_storage/TEMP/" + photo.FilePath)
if err != nil {
panic(err)
}
delete(activeTempImages, photo.ID)
}
}
}