2018-09-14 12:44:15 +02:00
|
|
|
package photoprism
|
|
|
|
|
|
|
|
import (
|
2019-04-29 20:09:10 +02:00
|
|
|
"io/ioutil"
|
2018-09-14 12:44:15 +02:00
|
|
|
"testing"
|
2018-10-31 07:14:33 +01:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2018-10-31 07:14:33 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-14 12:44:15 +02:00
|
|
|
)
|
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
func TestTensorFlow_LoadLabelRules(t *testing.T) {
|
|
|
|
t.Run("labels.yml exists", func(t *testing.T) {
|
|
|
|
conf := config.NewTestConfig()
|
|
|
|
|
|
|
|
tensorFlow := NewTensorFlow(conf)
|
|
|
|
|
|
|
|
result := tensorFlow.loadLabelRules()
|
|
|
|
assert.Nil(t, result)
|
|
|
|
})
|
|
|
|
t.Run("labels.yml not existing in config path", func(t *testing.T) {
|
|
|
|
conf := config.NewTestErrorConfig()
|
|
|
|
|
|
|
|
tensorFlow := NewTensorFlow(conf)
|
|
|
|
|
|
|
|
result := tensorFlow.loadLabelRules()
|
|
|
|
assert.Contains(t, result.Error(), "label rules file not found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
func TestTensorFlow_LabelsFromFile(t *testing.T) {
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Run("/chameleon_lime.jpg", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
tensorFlow := NewTensorFlow(conf)
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
result, err := tensorFlow.LabelsFromFile(conf.ExamplesPath() + "/chameleon_lime.jpg")
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Nil(t, err)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotNil(t, result)
|
|
|
|
assert.IsType(t, Labels{}, result)
|
|
|
|
assert.Equal(t, 1, len(result))
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Log(result)
|
|
|
|
|
|
|
|
assert.Equal(t, "chameleon", result[0].Name)
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, 7, result[0].Uncertainty)
|
|
|
|
})
|
|
|
|
t.Run("not existing file", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
tensorFlow := NewTensorFlow(conf)
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
result, err := tensorFlow.LabelsFromFile(conf.ExamplesPath() + "/notexisting.jpg")
|
|
|
|
assert.Contains(t, err.Error(), "no such file or directory")
|
|
|
|
assert.Empty(t, result)
|
|
|
|
})
|
2018-09-14 12:44:15 +02:00
|
|
|
}
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
func TestTensorFlow_Labels(t *testing.T) {
|
2019-04-29 20:09:10 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Run("/chameleon_lime.jpg", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
tensorFlow := NewTensorFlow(conf)
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/chameleon_lime.jpg"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Log(result)
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.NotNil(t, result)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.IsType(t, Labels{}, result)
|
|
|
|
assert.Equal(t, 1, len(result))
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, "chameleon", result[0].Name)
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, 100-93, result[0].Uncertainty)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("/dog_orange.jpg", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
tensorFlow := NewTensorFlow(conf)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/dog_orange.jpg"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Log(result)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.NotNil(t, result)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.IsType(t, Labels{}, result)
|
|
|
|
assert.Equal(t, 2, len(result))
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, "chihuahua dog", result[0].Name)
|
|
|
|
assert.Equal(t, "pembroke dog", result[1].Name)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, 34, result[0].Uncertainty)
|
|
|
|
assert.Equal(t, 91, result[1].Uncertainty)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("/Random.docx", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
tensorFlow := NewTensorFlow(conf)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/Random.docx"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
assert.Contains(t, err.Error(), "invalid image")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("/6720px_white.jpg", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
|
|
|
|
|
|
|
tensorFlow := NewTensorFlow(conf)
|
|
|
|
|
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/6720px_white.jpg"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTensorFlow_LoadModel(t *testing.T) {
|
|
|
|
t.Run("model path exists", func(t *testing.T) {
|
|
|
|
conf := config.NewTestConfig()
|
|
|
|
|
|
|
|
tensorFlow := NewTensorFlow(conf)
|
|
|
|
|
|
|
|
result := tensorFlow.loadModel()
|
|
|
|
assert.Nil(t, result)
|
|
|
|
})
|
|
|
|
t.Run("model path does not exist", func(t *testing.T) {
|
|
|
|
conf := config.NewTestErrorConfig()
|
|
|
|
|
|
|
|
tensorFlow := NewTensorFlow(conf)
|
|
|
|
|
|
|
|
result := tensorFlow.loadModel()
|
|
|
|
assert.Contains(t, result.Error(), "Could not find SavedModel")
|
|
|
|
})
|
2019-04-29 20:09:10 +02:00
|
|
|
}
|