Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created April 2, 2014 21:10
Show Gist options
  • Save peterhellberg/9943252 to your computer and use it in GitHub Desktop.
Save peterhellberg/9943252 to your computer and use it in GitHub Desktop.
Gobot: Controlling LEDs connected to an Arduino over Firmata using your face (OpenCV)
package main
import (
"fmt"
"log"
"path"
"path/filepath"
"runtime"
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-firmata"
"github.com/hybridgroup/gobot-gpio"
"github.com/hybridgroup/gobot-opencv"
)
func firstUSBModem() string {
devices, err := filepath.Glob("/dev/tty.usbmodem*")
if err != nil || len(devices) < 1 {
log.Fatal("No USB modem found.")
}
return devices[0]
}
func main() {
_, currentfile, _, _ := runtime.Caller(0)
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")
opencv := new(gobotOpencv.Opencv)
opencv.Name = "opencv"
camera := gobotOpencv.NewCamera(opencv)
camera.Name = "camera"
firmata := new(gobotFirmata.FirmataAdaptor)
firmata.Name = "firmata"
firmata.Port = firstUSBModem()
green := gobotGPIO.NewLed(firmata)
green.Pin = "11"
red1 := gobotGPIO.NewLed(firmata)
red1.Pin = "10"
red2 := gobotGPIO.NewLed(firmata)
red2.Pin = "9"
red3 := gobotGPIO.NewLed(firmata)
red3.Pin = "6"
red4 := gobotGPIO.NewLed(firmata)
red4.Pin = "5"
red5 := gobotGPIO.NewLed(firmata)
red5.Pin = "3"
work := func() {
gobot.On(camera.Events["Frame"], func(data interface{}) {
i := data.(*cv.IplImage)
faces := gobotOpencv.DetectFaces(cascade, i)
if len(faces) > 0 {
green.On()
f := faces[0]
w := f.Width()
switch {
case w < 250:
fmt.Println("*")
red1.On()
red2.Off()
red3.Off()
red4.Off()
red5.Off()
case w > 250 && w < 350:
fmt.Println("**")
red1.On()
red2.On()
red3.Off()
red4.Off()
red5.Off()
case w > 350 && w < 400:
fmt.Println("***")
red1.On()
red2.On()
red3.On()
red4.Off()
red5.Off()
case w > 400 && w < 450:
fmt.Println("****")
red1.On()
red2.On()
red3.On()
red4.On()
red5.Off()
case w > 450:
fmt.Println("*****")
red1.On()
red2.On()
red3.On()
red4.On()
red5.On()
}
} else {
green.Off()
red1.Off()
red2.Off()
red3.Off()
red4.Off()
red5.Off()
}
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{opencv, firmata},
Devices: []gobot.Device{camera},
Work: work,
}
robot.Start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment