main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "time"
  8. "tryon/gen"
  9. "tryon/oss"
  10. "github.com/jessevdk/go-flags"
  11. )
  12. var GAppOption = &AppOption{}
  13. // short只能是一个字符
  14. type AppOption struct {
  15. Model string `short:"m" long:"model" description:"model image"`
  16. Scale float64 `short:"c" long:"scale" description:"scale [2.0-8.0] default 5.0"`
  17. Shoes []string `short:"s" long:"shoes" description:"shoes images"`
  18. }
  19. func (o *AppOption) Parse() error {
  20. _, err := flags.NewParser(o, flags.Default|flags.IgnoreUnknown).Parse()
  21. return err
  22. }
  23. const (
  24. LOCAL_MODEL_DIR = "models"
  25. LOCAL_SHOES_DIR = "shoes"
  26. OUTPUT_DIR = "output"
  27. )
  28. func main() {
  29. os.Mkdir(OUTPUT_DIR, os.ModePerm)
  30. // 读取命令行参数 获取图片
  31. // go run . -m xxx.png -s xx.png -s xx.png
  32. err := GAppOption.Parse()
  33. if err != nil {
  34. fmt.Println(err)
  35. return
  36. }
  37. // 适配/设置默认scale
  38. if GAppOption.Scale == 0 {
  39. GAppOption.Scale = 5.01
  40. }
  41. if GAppOption.Scale < 2 {
  42. GAppOption.Scale = 2.01
  43. }
  44. if GAppOption.Scale > 8 {
  45. GAppOption.Scale = 7.99
  46. }
  47. // 获取shoes目录中图片
  48. if len(GAppOption.Shoes) == 0 {
  49. files, err := getAllFilesInDir(LOCAL_SHOES_DIR)
  50. if err != nil {
  51. fmt.Println("获取shoes文件失败: ", err)
  52. return
  53. }
  54. GAppOption.Shoes = append(GAppOption.Shoes, files...)
  55. }
  56. if len(GAppOption.Model) == 0 || len(GAppOption.Shoes) == 0 {
  57. fmt.Println("缺失图片参数!")
  58. return
  59. }
  60. // 根据参数拼接图片所在当前完整路径
  61. modelLocalImage := fmt.Sprintf("%s/%s", LOCAL_MODEL_DIR, GAppOption.Model)
  62. shoesLocalImages := []string{}
  63. fmt.Println(len(shoesLocalImages))
  64. for _, shoe := range GAppOption.Shoes {
  65. shoesLocalImages = append(shoesLocalImages, fmt.Sprintf("%s/%s", LOCAL_SHOES_DIR, shoe))
  66. }
  67. ossReslut, err := oss.UpladImages(modelLocalImage, shoesLocalImages)
  68. if err != nil {
  69. fmt.Println(err)
  70. return
  71. }
  72. fmt.Println("上传结果: ", ossReslut)
  73. if len(ossReslut.Model) == 0 || len(ossReslut.Shoes) == 0 {
  74. fmt.Println("上传图片出错")
  75. return
  76. }
  77. taskId, err := gen.Generate(ossReslut.Model, ossReslut.Shoes, GAppOption.Scale)
  78. fmt.Println("taskId: ", taskId)
  79. if err != nil {
  80. fmt.Println(err)
  81. return
  82. }
  83. // 延迟
  84. fmt.Println("等待处理。。。")
  85. time.Sleep(3 * time.Second)
  86. // 这里成功返回url,失败返回""/status
  87. url, err := gen.GetReslut(taskId, OUTPUT_DIR)
  88. fmt.Println("url: ", url)
  89. if err != nil {
  90. fmt.Println(err)
  91. if len(url) == 0 {
  92. return
  93. }
  94. if url == "FAILED" {
  95. fmt.Println("后台处理失败status[FAILED]")
  96. return
  97. }
  98. // 重新查询
  99. replay := 10
  100. for {
  101. if replay == 0 {
  102. fmt.Printf("status: %s,任务处理中,请多等待...", url)
  103. return
  104. }
  105. time.Sleep(5 * time.Second)
  106. url, err = gen.GetReslut(taskId, OUTPUT_DIR)
  107. if err != nil {
  108. // 重试逻辑
  109. if len(url) > 0 {
  110. if url == "FAILED" {
  111. fmt.Println("后台处理失败status[FAILED]")
  112. return
  113. }
  114. replay--
  115. } else {
  116. // 异常逻辑
  117. fmt.Println(err)
  118. return
  119. }
  120. } else {
  121. // 成功跳出
  122. break
  123. }
  124. }
  125. }
  126. fmt.Println("url---> : ", url)
  127. dist, err := gen.Download(url, OUTPUT_DIR)
  128. if err != nil {
  129. fmt.Println(err)
  130. return
  131. }
  132. fmt.Println("获取结果: ", dist)
  133. }
  134. // 获取某个目录下的所有文件路径,递归遍历,并生成相对路径
  135. func getAllFilesInDir(baseDir string) ([]string, error) {
  136. var files []string
  137. // filepath.Walk 会递归遍历目录及子目录
  138. err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
  139. // 遇到错误直接返回
  140. if err != nil {
  141. return err
  142. }
  143. // 跳过目录,只处理文件
  144. if !info.IsDir() {
  145. // 计算相对路径
  146. relPath, err := filepath.Rel(baseDir, path)
  147. if err != nil {
  148. return err
  149. }
  150. // 将路径中的反斜杠替换为正斜杠
  151. relPath = strings.ReplaceAll(relPath, "\\", "/")
  152. // 将相对路径加入文件列表
  153. files = append(files, relPath)
  154. }
  155. return nil
  156. })
  157. if err != nil {
  158. return nil, err
  159. }
  160. return files, nil
  161. }