2020-02-16 18:14:38 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/cyrilix/robocar-tools/record"
|
2021-11-24 18:31:16 +00:00
|
|
|
"github.com/disintegration/imaging"
|
2021-11-24 18:16:34 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-24 18:31:16 +00:00
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
2020-02-16 18:14:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
2021-11-24 18:31:16 +00:00
|
|
|
"strings"
|
2020-02-16 18:14:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var camSubDir = "cam"
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
func WriteArchive(basedir string, archiveName string, sliceSize int, imgWidth, imgHeight int, horizon int, flipImages bool) error {
|
|
|
|
content, err := BuildArchive(basedir, sliceSize, imgWidth, imgHeight, horizon, flipImages)
|
2021-10-17 17:15:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to build archive: %w", err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to build archive: %w", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(archiveName, content, os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to write archive content to disk: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
func BuildArchive(basedir string, sliceSize int, imgWidth, imgHeight int, horizon int, flipImages bool) ([]byte, error) {
|
2021-11-24 18:16:34 +00:00
|
|
|
l := zap.S()
|
|
|
|
l.Infof("build zip archive from %s\n", basedir)
|
2020-02-16 18:14:38 +00:00
|
|
|
dirItems, err := ioutil.ReadDir(basedir)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return nil, fmt.Errorf("unable to list directory in %v dir: %w", basedir, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imgCams := make([]string, 0)
|
|
|
|
records := make([]string, 0)
|
|
|
|
|
|
|
|
for _, dirItem := range dirItems {
|
2021-11-24 18:16:34 +00:00
|
|
|
l.Infof("process %v directory", dirItem.Name())
|
2020-02-16 18:14:38 +00:00
|
|
|
imgDir := path.Join(basedir, dirItem.Name(), camSubDir)
|
|
|
|
imgs, err := ioutil.ReadDir(imgDir)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return nil, fmt.Errorf("unable to list cam images in directory %v: %w", imgDir, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range imgs {
|
|
|
|
idx, err := indexFromFile(img.Name())
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return nil, fmt.Errorf("unable to find index in cam image name %v: %w", img.Name(), err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
2021-11-24 18:16:34 +00:00
|
|
|
l.Debugf("found image with index %v", idx)
|
2022-06-09 10:19:54 +00:00
|
|
|
records = append(records, path.Join(basedir, dirItem.Name(), fmt.Sprintf(record.FileNameFormat, idx)))
|
2020-02-16 18:14:38 +00:00
|
|
|
imgCams = append(imgCams, path.Join(basedir, dirItem.Name(), camSubDir, img.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:15:44 +00:00
|
|
|
if sliceSize > 0 {
|
2020-02-16 18:14:38 +00:00
|
|
|
imgCams, records, err = applySlice(imgCams, records, sliceSize)
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:31:16 +00:00
|
|
|
// Create a buffer to write our archive to.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
// Create a new zip archive.
|
|
|
|
w := zip.NewWriter(buf)
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
err = buildArchiveContent(w, imgCams, records, imgWidth, imgHeight, horizon, false)
|
2021-11-24 18:31:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to build archive: %w", err)
|
|
|
|
}
|
|
|
|
if flipImages {
|
2022-06-09 10:19:54 +00:00
|
|
|
err = buildArchiveContent(w, imgCams, records, imgWidth, imgHeight, horizon, true)
|
2021-11-24 18:31:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to build archive: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Close()
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-11-24 18:31:16 +00:00
|
|
|
return nil, fmt.Errorf("unable to close zip archive: %w", err)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadAll(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to generate archive content: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
2021-11-24 18:16:34 +00:00
|
|
|
l.Info("archive built\n")
|
2021-10-17 17:15:44 +00:00
|
|
|
return content, nil
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func applySlice(imgCams []string, records []string, sliceSize int) ([]string, []string, error) {
|
|
|
|
// Add sliceSize images shift
|
|
|
|
i := imgCams[:len(imgCams)-sliceSize]
|
|
|
|
r := records[sliceSize:]
|
|
|
|
|
|
|
|
return i, r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var indexRegexp *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
re, err := regexp.Compile("image_array_(?P<idx>[0-9]+)\\.jpg$")
|
|
|
|
if err != nil {
|
2021-11-24 18:16:34 +00:00
|
|
|
zap.S().Fatalf("unable to compile regex: %v", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
indexRegexp = re
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromFile(fileName string) (string, error) {
|
|
|
|
matches := findNamedMatches(indexRegexp, fileName)
|
|
|
|
if matches["idx"] == "" {
|
|
|
|
return "", fmt.Errorf("no index in filename")
|
|
|
|
}
|
|
|
|
return matches["idx"], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findNamedMatches(regex *regexp.Regexp, str string) map[string]string {
|
|
|
|
match := regex.FindStringSubmatch(str)
|
|
|
|
|
|
|
|
results := map[string]string{}
|
|
|
|
for i, name := range match {
|
|
|
|
results[regex.SubexpNames()[i]] = name
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
func buildArchiveContent(w *zip.Writer, imgFiles []string, recordFiles []string, imgWidth, imgHeight int, horizon int, withFlipImages bool) error {
|
2021-11-24 18:31:16 +00:00
|
|
|
err := addJsonFiles(recordFiles, imgFiles, withFlipImages, w)
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-11-24 18:31:16 +00:00
|
|
|
return fmt.Errorf("unable to write json files in zip archive: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
err = addCamImages(imgFiles, withFlipImages, w, imgWidth, imgHeight, horizon)
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-11-24 18:31:16 +00:00
|
|
|
return fmt.Errorf("unable to cam files in zip archive: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 18:31:16 +00:00
|
|
|
return err
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
func addCamImages(imgFiles []string, flipImage bool, w *zip.Writer, imgWidth, imgHeight int, horizon int) error {
|
2021-11-24 18:31:16 +00:00
|
|
|
for _, im := range imgFiles {
|
|
|
|
imgContent, err := ioutil.ReadFile(im)
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to read img: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 18:31:16 +00:00
|
|
|
_, imgName := path.Split(im)
|
2022-06-09 10:19:54 +00:00
|
|
|
if flipImage || imgWidth > 0 && imgHeight > 0 || horizon > 0 {
|
2021-11-24 18:31:16 +00:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(imgContent))
|
|
|
|
if err != nil {
|
2022-06-09 10:19:54 +00:00
|
|
|
zap.S().Fatalf("unable to decode jpeg image: %v", err)
|
2021-11-24 18:31:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
if imgWidth > 0 && imgHeight > 0 {
|
|
|
|
bounds := img.Bounds()
|
|
|
|
if bounds.Dx() != imgWidth || bounds.Dy() != imgWidth {
|
|
|
|
zap.S().Debugf("resize image %v from %dx%d to %dx%d", im, bounds.Dx(), bounds.Dy(), imgWidth, imgHeight)
|
|
|
|
img = imaging.Resize(img, imgWidth, imgHeight, imaging.NearestNeighbor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flipImage {
|
|
|
|
img = imaging.FlipH(img)
|
|
|
|
imgName = fmt.Sprintf("flip_%s", imgName)
|
|
|
|
}
|
|
|
|
if horizon > 0 {
|
|
|
|
img = imaging.Crop(img, image.Rect(0, horizon, img.Bounds().Dx(), img.Bounds().Dy()))
|
|
|
|
}
|
|
|
|
var bytesBuff bytes.Buffer
|
|
|
|
err = jpeg.Encode(&bytesBuff, img, nil)
|
2021-11-24 18:31:16 +00:00
|
|
|
imgContent = bytesBuff.Bytes()
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:15:44 +00:00
|
|
|
err = addToArchive(w, imgName, imgContent)
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to create new img entry in archive: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:31:16 +00:00
|
|
|
func addJsonFiles(recordFiles []string, imgCam []string, flipImage bool, w *zip.Writer) error {
|
2021-10-17 17:15:44 +00:00
|
|
|
for idx, r := range recordFiles {
|
2020-02-16 18:14:38 +00:00
|
|
|
content, err := ioutil.ReadFile(r)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to read json content: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
var rcd record.Record
|
|
|
|
err = json.Unmarshal(content, &rcd)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to unmarshal record: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
2021-10-17 17:15:44 +00:00
|
|
|
_, camName := path.Split((imgCam)[idx])
|
2021-11-24 18:31:16 +00:00
|
|
|
|
|
|
|
if flipImage {
|
|
|
|
rcd.UserAngle = rcd.UserAngle * -1
|
|
|
|
rcd.CamImageArray = fmt.Sprintf("flip_%s", camName)
|
|
|
|
}else {
|
|
|
|
rcd.CamImageArray = camName
|
|
|
|
}
|
2020-02-16 18:14:38 +00:00
|
|
|
|
|
|
|
recordBytes, err := json.Marshal(&rcd)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to marshal %v record: %w", rcd, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, recordName := path.Split(r)
|
2021-11-24 18:31:16 +00:00
|
|
|
if flipImage {
|
|
|
|
recordName = strings.ReplaceAll(recordName, "record", "record_flip")
|
|
|
|
}
|
2021-10-17 17:15:44 +00:00
|
|
|
err = addToArchive(w, recordName, recordBytes)
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to create new record in archive: %w", err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:31:16 +00:00
|
|
|
func addToArchive(w *zip.Writer, name string, content []byte) error {
|
2020-02-16 18:14:38 +00:00
|
|
|
recordWriter, err := w.Create(name)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to create new entry %v in archive: %w", name, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 17:15:44 +00:00
|
|
|
_, err = recordWriter.Write(content)
|
2020-02-16 18:14:38 +00:00
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
return fmt.Errorf("unable to add content in %v zip archive: %w", name, err)
|
2020-02-16 18:14:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|