Skip to content

Commit

Permalink
update go example
Browse files Browse the repository at this point in the history
  • Loading branch information
kharijarrett committed May 4, 2023
1 parent 11dcb51 commit 15cbd4d
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 580 deletions.
28 changes: 0 additions & 28 deletions go/camera_config.json

This file was deleted.

93 changes: 53 additions & 40 deletions go/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,46 @@ package main

import (
"context"
"image/color"
"image"

"github.com/edaniels/golog"
"github.com/webview/webview"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/rimage"
robotimpl "go.viam.com/rdk/robot/impl"
"go.viam.com/rdk/services/vision"
"gocv.io/x/gocv"
"go.viam.com/rdk/vision/objectdetection"

// registers all cameras.
_ "go.viam.com/rdk/components/camera/register"
// registers the vision service
_ "go.viam.com/rdk/services/vision/register"
// registers the mlmodel service
_ "go.viam.com/rdk/services/mlmodel/register"
)

const (
pathHere = "/full/path/to/vision-service-examples/go/"
saveTo = "output.jpg"
)

func main() {
// Setup robot client
logger := golog.NewDevelopmentLogger("opencv")
// read the robot config and create the robot
// if you want to create the robot directly from a JSON
r, err := robotimpl.RobotFromConfigPath(context.Background(), "camera_config.json", logger)
if err != nil {
logger.Fatalf("cannot create robot: %v", err)
}
/* if you want to read the robot from the app
r, err := client.New(
context.Background(),
"[THE ADDRESS OF YOUR ROBOT, GOTTEN FROM THE CONNECT TAB]",
logger,
client.WithDialOptions(rpc.WithCredentials(rpc.Credentials{
Type: utils.CredentialsTypeRobotLocationSecret,
Payload: "[THE SECRET OF YOUR ROBOT GOTTEN FROM THE CONNECT TAB]",
})),
)

// if you want to create the robot directly from a JSON config
r, err := robotimpl.RobotFromConfigPath(context.Background(), "robot_config.json", logger)
// if you want to read the robot from the app
// r, err := robotFromConnection(logger)
if err != nil {
logger.Fatalf("cannot create robot: %v", err)
}
*/
defer r.Close(context.Background())

// Print available resources
logger.Info("Resources:")
logger.Info(r.ResourceNames())

// grab the camera from the robot
cameraName := "cam1" // make sure to use the same name as in the json/APP
cam, err := camera.FromRobot(r, cameraName)
Expand All @@ -52,47 +53,59 @@ func main() {
logger.Fatalf("cannot get camera stream: %v", err)
}
// grab Viam's vision service that already has the TF-Lite model for object detection
visionSrv, err := vision.FirstFromRobot(r)
myDetector, err := vision.FromRobot(r, "myDetector")
if err != nil {
logger.Fatalf("cannot get vision service: %v", err)
}
// make the display window and get the camera stream
window := gocv.NewWindow("Object Detect")
defer window.Close()
// loop forever: find objects using Viam Vision, and draw boxes aroung them using openCV

// Do not display any detection with confidence < confThreshold
confThreshold := 0.6
for {
// get image
img, release, err := camStream.Next(context.Background())
if err != nil {
logger.Fatalf("cannot get image: %v", err)
}
defer release()

// get detection bounding boxes
detections, err := visionSrv.Detections(context.Background(), img, "find_objects")
detections, err := myDetector.Detections(context.Background(), img, nil)
if err != nil {
logger.Errorf("detection error: %v", err)
continue
}
mat, err := gocv.ImageToMatRGBA(img)
if err != nil {
logger.Errorf("error converting image to OpenCV Mat type: %v", err)
continue
}
// get the objects that appear above a certain confidence (0.6 here)
conf := 0.6
// get the detections that appear above a certain confidence (0.6 here)
labels := make([]string, 0, 25)
filtered := make([]objectdetection.Detection, 0, len(detections))
for _, d := range detections {
if d.Score() > conf {
boundingBox := d.BoundingBox()
// draw rectangle around the object
gocv.Rectangle(&mat, *boundingBox, color.RGBA{255, 0, 0, 255}, 3)
if d.Score() > confThreshold {
filtered = append(filtered, d)
labels = append(labels, d.Label())
}
}
logger.Infof("found these objects in image: %v", labels)
// display
window.IMShow(mat)
window.WaitKey(1)

// draw the filtered detections onto the image
saveMe, err := objectdetection.Overlay(img, filtered)
if err != nil {
logger.Errorf("could not overlay image")
}

// save image locally and display if interesting
rimage.SaveImage(saveMe, saveTo)
if len(labels) > 0 {
displayImage(saveMe) // once this is called, you're done.
}

}

}

// displayImage will display the image stored in pathHere+saveTo
func displayImage(img image.Image) {
webView := webview.New(true)
defer webView.Destroy()
webView.Navigate("file://" + pathHere + saveTo)
webView.SetSize(img.Bounds().Dx()+100, img.Bounds().Dy()+100, webview.HintFixed)
webView.Navigate("file://" + pathHere + saveTo)
webView.Run()
}
Loading

0 comments on commit 15cbd4d

Please sign in to comment.