-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (162 loc) · 4.77 KB
/
main.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
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/apprentice3d/forge-api-go-client/recap"
)
func main() {
dir := "."
if len(os.Args) > 1 {
dir = os.Args[1]
}
images, err := getListOfJPGFilesFromPath(dir)
if err != nil {
log.Fatalln(err.Error())
}
log.Printf("Found %d jpg images.\n", len(images))
clientID, clientSecret, err := getCredentials()
if err != nil {
log.Fatalln(err.Error())
}
recapAPI := recap.NewAPIWithCredentials(clientID, clientSecret)
log.Println("Creating a scene ...")
scene, err := recapAPI.CreatePhotoScene("example", []string{"obj"}, "object")
//scene, err := recapAPI.CreatePhotoScene("example", []string{"obj"}, "aerial")
if err != nil {
log.Fatal(err.Error())
}
log.Printf("Scene created: id = %s\n", scene.ID)
log.Println("Uploading sample images ...standby...")
var wg sync.WaitGroup
wg.Add(len(images))
for idx, filename := range images {
// parallel execution is possible by writing `go` in front of below function
func(idx int, filename string) {
defer wg.Done()
status := fmt.Sprintf("[%2d/%d] File %s ", idx+1, len(images), filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
status += "failed to upload: " + err.Error()
log.Println(status)
return
}
_, err = recapAPI.AddFileToSceneUsingData(scene.ID, data)
if err != nil {
status += "failed to upload: " + err.Error()
log.Println(status)
return
}
status += "uploaded successfully"
log.Println(status)
}(idx, filename)
}
wg.Wait()
log.Println("Starting scene processing ...")
if _, err = recapAPI.StartSceneProcessing(scene.ID); err != nil {
log.Println(err.Error())
os.Exit(1)
}
log.Println("Checking scene status ...")
var progressResult recap.SceneProgressReply
var ratio float64
for {
if progressResult, err = recapAPI.GetSceneProgress(scene.ID); err != nil {
log.Printf("Failed to get the PhotoScene progress: %s\n", err.Error())
return
}
ratio, _ = strconv.ParseFloat(progressResult.PhotoScene.Progress, 64)
if err != nil {
log.Printf("Failed to parse progress results: %s\n", err.Error())
return
}
if ratio == float64(100.0) {
break
}
fmt.Printf("\rScene progress = %.2f%%", ratio)
time.Sleep(5 * time.Second)
}
log.Println("Finished processing the scene, now getting the results in obj format...")
result, err := recapAPI.GetSceneResults(scene.ID, "obj")
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
log.Printf("Results are available at following link => %s\n", result.PhotoScene.SceneLink)
if err := downloadLink(result.PhotoScene.SceneLink, "result_obj.zip"); err != nil {
log.Println("WARNING: Could not download the provided link")
} else {
workDir, _ := os.Getwd()
log.Printf("File downloaded to %s as 'result_obj.zip'\n", workDir)
}
info, _ := os.Stat("result_obj.zip")
log.Printf("The download file has size %d", info.Size())
//fmt.Println("\nNow downloading the results in rcm format...")
//result, err = recapAPI.GetSceneResults(scene.ID, "rcm")
//if err != nil {
// log.Println(err.Error())
//}
//
//fmt.Printf("Results are available at following link => %s\n", result.PhotoScene.SceneLink)
//if err := downloadLink(result.PhotoScene.SceneLink, "result_rcm.zip"); err != nil {
// log.Println("WARNING: Could not download the provided link")
//} else {
// workDir, _ := os.Getwd()
// fmt.Printf("File downloaded to %s as 'result_rcm.zip'\n", workDir)
//}
log.Println("Deleting the scene ...")
_, err = recapAPI.DeleteScene(scene.ID)
if err != nil {
log.Fatal(err.Error())
}
log.Println("Scene deleted successfully!")
}
func downloadLink(link, filename string) (err error) {
resp, err := http.Get(link)
if err != nil {
return
}
defer resp.Body.Close()
result, err := os.Create(filename)
if err != nil {
return
}
defer result.Close()
_, err = io.Copy(result, resp.Body)
return
}
func getListOfJPGFilesFromPath(dir string) (images []string, err error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, file := range files {
if !file.IsDir() {
if strings.Compare(strings.ToLower(filepath.Ext(file.Name())), ".jpg") == 0 {
images = append(images, filepath.Join(dir, file.Name()))
}
}
}
if len(images) == 0 {
err = errors.New("no valid images found for upload")
}
return
}
func getCredentials() (clientID string, clientSecret string, err error) {
clientID = os.Getenv("FORGE_CLIENT_ID")
clientSecret = os.Getenv("FORGE_CLIENT_SECRET")
if len(clientID) == 0 || len(clientSecret) == 0 {
err = errors.New("\nFORGE_CLIENT_ID and FORGE_CLIENT_SECRET env vars could not be found.\n" +
"We encourage using Forge secrets by specifying them as env variables.\nExiting ...")
}
return
}