diff --git a/network.go b/network.go index e55638b..1883eb0 100644 --- a/network.go +++ b/network.go @@ -80,4 +80,4 @@ func (n *YOLONetwork) Detect(img *DarknetImage) (*DetectionResult, error) { OverallTimeTaken: endTimeOverall.Sub(startTime), } return &out, nil -} \ No newline at end of file +} diff --git a/section.go b/section.go new file mode 100644 index 0000000..db55fa8 --- /dev/null +++ b/section.go @@ -0,0 +1,58 @@ +package darknet + +import ( + "bufio" + "os" + "strings" +) + +// Section represents a section in the configuration file. +type Section struct { + Type string + Options []string +} + +// readCfg reads a configuration file and returns a list of sections. +func readCfg(filename string) ([]Section, error) { + // Open the configuration file. + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + + // Create a list of sections. + var sections []Section + var current *Section + + // Read each line in the file. + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line == "" { + continue + } + switch line[0] { + case '[': + if current != nil { + sections = append(sections, *current) + } + current = &Section{Type: line} + case '#', ';', '\x00': + // Ignore comments and empty lines. + default: + if current == nil { + current = &Section{} + } + current.Options = append(current.Options, line) + } + } + if current != nil { + sections = append(sections, *current) + } + if err := scanner.Err(); err != nil { + return nil, err + } + + return sections, nil +} diff --git a/section_test.go b/section_test.go new file mode 100644 index 0000000..58f018d --- /dev/null +++ b/section_test.go @@ -0,0 +1,20 @@ +package darknet + +import ( + "fmt" + "testing" +) + +func TestReadSectionsFromCfg(t *testing.T) { + sections, err := readCfg("./cmd/examples/yolov7-tiny.cfg") + if err != nil { + fmt.Println(err) + return + } + for _, s := range sections { + fmt.Println(s.Type) + for _, o := range s.Options { + fmt.Println("\t", o) + } + } +}