chore: closes #36

This commit is contained in:
Andre Henriques 2023-10-12 09:38:00 +01:00
parent 998a583bdb
commit c7c6cfcd00
4 changed files with 25 additions and 5 deletions

View File

@ -37,7 +37,6 @@ func loadBaseImage(c *Context, id string) {
switch format { switch format {
case "png": case "png":
case "jpeg": case "jpeg":
break
default: default:
// TODO better logging // TODO better logging
fmt.Printf("Found unkown format '%s'\n", format) fmt.Printf("Found unkown format '%s'\n", format)

View File

@ -126,7 +126,7 @@ func processZipFile(c *Context, model *BaseModel) {
return return
} }
file_path := path.Join(base_path, data_point_id+".png") file_path := path.Join(base_path, data_point_id + "." + model.Format)
f, err := os.Create(file_path) f, err := os.Create(file_path)
if err != nil { if err != nil {
fmt.Printf("Could not create file %s\n", file_path) fmt.Printf("Could not create file %s\n", file_path)

View File

@ -27,6 +27,16 @@ func ReadPNG(scope *op.Scope, imagePath string, channels int64) *image.Image {
return image.Scale(0, 255) return image.Scale(0, 255)
} }
func ReadJPG(scope *op.Scope, imagePath string, channels int64) *image.Image {
scope = tg.NewScope(scope)
contents := op.ReadFile(scope.SubScope("ReadFile"), op.Const(scope.SubScope("filename"), imagePath))
output := op.DecodePng(scope.SubScope("DecodeJpeg"), contents, op.DecodePngChannels(channels))
output = op.ExpandDims(scope.SubScope("ExpandDims"), output, op.Const(scope.SubScope("axis"), []int32{0}))
image := &image.Image{
Tensor: tg.NewTensor(scope, output)}
return image.Scale(0, 255)
}
func handleRun(handle *Handle) { func handleRun(handle *Handle) {
handle.Post("/models/run", func(w http.ResponseWriter, r *http.Request, c *Context) *Error { handle.Post("/models/run", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
if !CheckAuthLevel(1, w, r, c) { if !CheckAuthLevel(1, w, r, c) {
@ -100,15 +110,16 @@ func handleRun(handle *Handle) {
// TODO create a database table with tasks // TODO create a database table with tasks
run_path := path.Join("/tmp", model.Id, "runs") run_path := path.Join("/tmp", model.Id, "runs")
os.MkdirAll(run_path, os.ModePerm) os.MkdirAll(run_path, os.ModePerm)
img_path := path.Join(run_path, "img." + model.Format)
img_file, err := os.Create(path.Join(run_path, "img.png")) img_file, err := os.Create(img_path)
if err != nil { if err != nil {
return Error500(err) return Error500(err)
} }
defer img_file.Close() defer img_file.Close()
img_file.Write(file) img_file.Write(file)
if !testImgForModel(c, model, path.Join(run_path, "img.png")) { if !testImgForModel(c, model, img_path) {
LoadDefineTemplate(w, "/models/edit.html", "run-model-card", c.AddMap(AnyMap{ LoadDefineTemplate(w, "/models/edit.html", "run-model-card", c.AddMap(AnyMap{
"Model": model, "Model": model,
"NotFound": false, "NotFound": false,
@ -120,7 +131,16 @@ func handleRun(handle *Handle) {
root := tg.NewRoot() root := tg.NewRoot()
tf_img := ReadPNG(root, path.Join(run_path, "img.png"), int64(model.ImageMode)) var tf_img *image.Image = nil
switch model.Format {
case "png":
tf_img = ReadPNG(root, img_path, int64(model.ImageMode))
case "jpeg":
tf_img = ReadJPG(root, img_path, int64(model.ImageMode))
default:
panic("Not sure what to do with '" + model.Format + "'")
}
exec_results := tg.Exec(root, []tf.Output{tf_img.Value()}, nil, &tf.SessionOptions{}) exec_results := tg.Exec(root, []tf.Output{tf_img.Value()}, nil, &tf.SessionOptions{})
inputImage, err:= tf.NewTensor(exec_results[0].Value()) inputImage, err:= tf.NewTensor(exec_results[0].Value())

View File

@ -27,6 +27,7 @@ DATA_DIR_PREPARE = DATA_DIR + "/"
def pathToLabel(path): def pathToLabel(path):
path = tf.strings.regex_replace(path, DATA_DIR_PREPARE, "") path = tf.strings.regex_replace(path, DATA_DIR_PREPARE, "")
path = tf.strings.regex_replace(path, ".jpg", "") path = tf.strings.regex_replace(path, ".jpg", "")
path = tf.strings.regex_replace(path, ".jpeg", "")
path = tf.strings.regex_replace(path, ".png", "") path = tf.strings.regex_replace(path, ".png", "")
return table.lookup(tf.strings.as_string([path])) return table.lookup(tf.strings.as_string([path]))
#return tf.strings.as_string([path]) #return tf.strings.as_string([path])