Skip to content

Commit

Permalink
Merge branch 'main' of github.com:adutchak/recognizer
Browse files Browse the repository at this point in the history
  • Loading branch information
adutchak committed Feb 7, 2024
2 parents ac4c758 + 022a691 commit fc79662
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20
go-version: '1.20'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: '1.53.3'
version: 'v1.53.3'
args: --timeout 3m
- name: Run build
run: |
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ The application retrieves image labels (i.e. glasses, hat, floor etc). Each retr
Also, you can set `CONFIDENCES_NOT_LESS_THAN` to make sure that certain labels exist on the picture.

# DISCOVERY_MODE
In order to retrieve the above described labels, you can enable `DISCOVERY_MODE`, this will not push any MQTT messages, but just output the recognized labels.
In order to retrieve the above described labels, you can enable `DISCOVERY_MODE`, this will not push any MQTT messages, but just output the recognized labels.
Optionally, you can add `DISCOVERY_LABELS_FILE_OUTPUT` env var and system will save the labels to a separate file.

# Docker-compose example:
```
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ func main() {
}
if configuration.DiscoveryMode {
l.Infof("DetectLabels output:\n%s", awsutil.Prettify(labelsOutput))
if configuration.DiscoveryLabelsFileOutput != "" {
l.Infof("writing labels to a file: %s", configuration.DiscoveryLabelsFileOutput)
err = writeToFile(configuration.DiscoveryLabelsFileOutput, awsutil.Prettify(labelsOutput))
if err != nil {
l.Error(err)
continue
}
}
}

var labelsPassed bool
Expand Down Expand Up @@ -254,3 +262,17 @@ func verifyLabelConfidenceNotMoreThan(label types.Label, labelName string, confi
}
return true, nil
}

func writeToFile(filename string, text string) error {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}

defer f.Close()

if _, err = f.WriteString(text); err != nil {
return err
}
return nil
}
32 changes: 17 additions & 15 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import (
)

type Config struct {
DiscoveryMode bool `json:"discoveryMode"`
MqttTopic string `json:"mqttTopic" validate:"required"`
MqttBroker string `json:"mqttBroker" validate:"required"`
MqttPort int `json:"mqttPort"`
MqttClientId string `json:"mqttClientId" validate:"required"`
MqttUsername string `json:"mqttUsername" validate:"required"`
MqttPassword string `json:"mqttPassword" validate:"required"`
MqttRecognizedMessage string `json:"mqttRecognizedMessage"`
MqttNotRecognizedMessage string `json:"mqttNotRecognizedMessage"`
DiscoveryMode bool `json:"discoveryMode"`
DiscoveryLabelsFileOutput string `json:"discoveryLabelsFileOutput"`
MqttTopic string `json:"mqttTopic" validate:"required"`
MqttBroker string `json:"mqttBroker" validate:"required"`
MqttPort int `json:"mqttPort"`
MqttClientId string `json:"mqttClientId" validate:"required"`
MqttUsername string `json:"mqttUsername" validate:"required"`
MqttPassword string `json:"mqttPassword" validate:"required"`
MqttRecognizedMessage string `json:"mqttRecognizedMessage"`
MqttNotRecognizedMessage string `json:"mqttNotRecognizedMessage"`

TargetImagePath string `json:"targetImagePath" validate:"required"`
SampleImagePaths []string `json:"sampleImagePaths" validate:"required"`
Expand Down Expand Up @@ -78,12 +79,13 @@ func Parse(args []string) (*Config, error) {
MqttRecognizedMessage: v.GetString(MqttRecognizedMessageKey),
MqttNotRecognizedMessage: v.GetString(MqttNotRecognizedMessageKey),

TargetImagePath: v.GetString(TargetImagePathKey),
SampleImagePaths: v.GetStringSlice(SampleImagePathsKey),
SimilarityThreshold: float32(v.GetFloat64(SimilarityThresholdKey)),
ConfidencesNotLessThan: v.GetString(ConfidencesNotLessThanKey),
ConfidencesNotMoreThan: v.GetString(ConfidencesNotMoreThanKey),
DiscoveryMode: v.GetBool(DiscoveryModeKey),
TargetImagePath: v.GetString(TargetImagePathKey),
SampleImagePaths: v.GetStringSlice(SampleImagePathsKey),
SimilarityThreshold: float32(v.GetFloat64(SimilarityThresholdKey)),
ConfidencesNotLessThan: v.GetString(ConfidencesNotLessThanKey),
ConfidencesNotMoreThan: v.GetString(ConfidencesNotMoreThanKey),
DiscoveryMode: v.GetBool(DiscoveryModeKey),
DiscoveryLabelsFileOutput: v.GetString(DiscoveryLabelsFileOutputKey),
}

validate := validator.New()
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func BuildFlagSet() *pflag.FlagSet {
fs.Float32(SimilarityThresholdKey, DefaultConfig.SimilarityThreshold, "specifies the minimal similarity threshold")
fs.String(ConfidencesNotLessThanKey, "", "specifies labels whose recognized confidence should not be less than threshold, example: \"Photography:98.0,Fisheye:60.0,Computer Hardware:40.0\"")
fs.String(ConfidencesNotMoreThanKey, "", "specifies labels whose recognized confidence should be more than threshold, example: \"Electronics:90.0,Phone:40.0,Computer Hardware:40.0\"")

fs.String(DiscoveryLabelsFileOutputKey, "", "specifies a path to a file where discovered labels will be written")
fs.Bool(DiscoveryModeKey, DefaultConfig.DiscoveryMode, "mode which simply prints recognized information")
return fs
}
3 changes: 2 additions & 1 deletion pkg/config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ const (
ConfidencesNotLessThanKey = "confidences-not-less-than"
ConfidencesNotMoreThanKey = "confidences-not-more-than"

DiscoveryModeKey = "discovery-mode"
DiscoveryModeKey = "discovery-mode"
DiscoveryLabelsFileOutputKey = "discovery-labels-file-output"
)

0 comments on commit fc79662

Please sign in to comment.