Skip to content

Instantly share code, notes, and snippets.

@grocky
Last active June 13, 2020 08:00
Show Gist options
  • Save grocky/92202fe9dbeeac2f7c6e501e33799c41 to your computer and use it in GitHub Desktop.
Save grocky/92202fe9dbeeac2f7c6e501e33799c41 to your computer and use it in GitHub Desktop.
mouse-detective
type result struct {
// the frame number
frame int
// The detected bounds
detectors []objectbox.CheckDetectorResponse
file io.Reader
err error
}
func checker(done <-chan struct{}, frames <-chan frame, results chan<- result) {
objectClient := objectbox.New("http://localhost:8083")
info, err := objectClient.Info()
if err != nil {
log.Fatalf("could not get box info: %v", err)
}
log.Printf("Connected to box: %s %s %s %d", info.Build, info.Name, info.Status, info.Version)
// process each frame from in channel
for f := range frames {
if f.number == 1 || f.number%10 == 0 {
log.Printf("Processing frame %d\n", f.number)
}
// Set up a ReadWriter to hold the image sent to the model to write the file later.
var bufferRead bytes.Buffer
buffer := bytes.NewReader(f.buffer)
// Send data read by the objectbox request to the buffer.
tee := io.TeeReader(buffer, &bufferRead)
resp, err := objectClient.Check(tee)
detectors := make([]objectbox.CheckDetectorResponse, 0, len(resp.Detectors))
// flatten detectors and identify found tags
for _, t := range resp.Detectors {
if len(t.Objects) > 0 {
detectors = append(detectors, t)
}
}
if len(detectors) == 0 {
continue
}
select {
case results <- result{f.number, detectors, &bufferRead, err}:
case <-done:
return
}
}
}
// channel of frames with mice
results := make(chan result)
var wg sync.WaitGroup
wg.Add(concurrencyF)
// Process the frames by fanning out to `concurrency` workers.
log.Println("Start processing frames")
for i := 0; i < concurrencyF; i++ {
go func() {
checker(done, frames, results)
wg.Done()
}()
}
// when each all workers are done, close the results channel
go func() {
wg.Wait()
close(results)
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment