func main() { win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() cap := opencv.NewCameraCapture(0) if cap == nil { panic("can not open camera") } defer cap.Release() win.CreateTrackbar("Thresh", 1, 100, func(pos int, param ...interface{}) { for { if cap.GrabFrame() { img := cap.RetrieveFrame(1) if img != nil { ProcessImage(img, win, pos) } else { fmt.Println("Image ins nil") } } if key := opencv.WaitKey(10); key == 27 { os.Exit(0) } } }) opencv.WaitKey(0) }
func loop(path string, paint, crosshair bool) { pictureFiles = GetFileNames(path) // images := make([][]byte, len(pictureFiles)) faceDetector := gofaces.NewFaceDetector() // for i := 0; i < len(pictureFiles); i++ { // } fmt.Println("Pictures: ", len(pictureFiles)) for i := 0; i < len(pictureFiles); i++ { fmt.Println("Processing picture: ", pictureFiles[i]) img := gofaces.GetNormalizedByteVectorFromFile(pictureFiles[i]) faces := faceDetector.Detect(img) if len(faces) > 0 { if paint { img = gofaces.PaintFace(img, faces[0]) } if faces[0].Eyes() > 1 { img = gofaces.AlignFaceInImage(img, faces[0]) } if crosshair { img = gofaces.Crosshair(img) } cropface := gofaces.CropOutFace(img, faces[0]) win := opencv.NewWindow("Face Detected") win.ShowImage(opencv.DecodeImageMem(cropface)) opencv.WaitKey(0) win.Destroy() } else { win := opencv.NewWindow("No face found") win.ShowImage(opencv.DecodeImageMem(img)) opencv.WaitKey(0) win.Destroy() } } }
func main() { _, currentfile, _, _ := runtime.Caller(0) filename := path.Join(path.Dir(currentfile), "./images/gopher.png") if len(os.Args) == 2 { filename = os.Args[1] } image := opencv.LoadImage(filename) if image == nil { panic("LoadImage fail") } defer image.Release() win := opencv.NewWindow("Go-OpenCV") defer win.Destroy() win.SetMouseCallback(func(event, x, y, flags int) { fmt.Printf("event = %d, x = %d, y = %d, flags = %d\n", event, x, y, flags, ) }) win.CreateTrackbar("Thresh", 1, 100, func(pos int) { fmt.Printf("pos = %d\n", pos) }) win.ShowImage(image) opencv.WaitKey(0) }
func main() { filename := "../images/lena.jpg" if len(os.Args) == 2 { filename = os.Args[1] } image := opencv.LoadImage(filename) if image == nil { panic("LoadImage fail") } defer image.Release() win := opencv.NewWindow("Go-OpenCV") defer win.Destroy() win.SetMouseCallback(func(event, x, y, flags int) { fmt.Printf("event = %d, x = %d, y = %d, flags = %d\n", event, x, y, flags, ) }) win.CreateTrackbar("Thresh", 1, 100, func(pos int) { fmt.Printf("pos = %d\n", pos) }) win.ShowImage(image) opencv.WaitKey(0) }
func TrackEyes(srcFile, destFile string) { var video = opencv.NewFileCapture(srcFile) var fourcc = opencv.FOURCC('m', 'p', '4', 'v') var fps = float32(video.GetProperty(opencv.CV_CAP_PROP_FPS)) var size = opencv.Size{int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH)), int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))} if video == nil { panic("no file found with name: " + srcFile) } defer video.Release() win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() _, currentfile, _, _ := runtime.Caller(0) var cascade = path.Join(path.Dir(currentfile), "/../cascades/haarcascade_eye_tree_eyeglasses.xml") var eyeMarker = NewEyeMarker(cascade) var imgs = []*opencv.IplImage{} var imgs2 = []*opencv.IplImage{} for { if video.GrabFrame() { img := video.RetrieveFrame(1) if img != nil { var copy = opencv.CreateImage(img.Width(), img.Height(), img.Depth(), img.Channels()) opencv.Copy(img.Clone(), copy, nil) var markedImg = eyeMarker.MarkEyes(copy) imgs = append(imgs, markedImg) //win.ShowImage(imgs[len(imgs)-1]) } else { fmt.Println("Image ins nil") } } else { break } key := opencv.WaitKey(10) if key == 27 { os.Exit(0) } } for i := 0; i < len(imgs2); i++ { win.ShowImage(imgs2[i]) } log.Print("before write") NewVideoWriter(uint32(fourcc), fps, size).SaveAsVideo(imgs, "test.avi") os.Exit(0) }
func debug(path string) { faceDetector := gofaces.NewFaceDetector() picture := gofaces.GetNormalizedByteVectorFromFile(path) image := opencv.DecodeImageMem(picture) faceAreas := faceDetector.DetectFaces(image) faces := faceDetector.DetectFacialFeatures(image, faceAreas) picture = gofaces.PaintFaces(picture, faces) win := opencv.NewWindow("Debug Face Detection") defer win.Destroy() win.ShowImage(opencv.DecodeImageMem(picture)) opencv.WaitKey(0) }
func one(path string, paint bool) { faceDetector := gofaces.NewFaceDetector() picture := gofaces.GetNormalizedByteVectorFromFile(path) faces := faceDetector.Detect(picture) if paint { picture = gofaces.PaintFace(picture, faces[0]) } picture = gofaces.AlignFaceInImage(picture, faces[0]) picture = gofaces.CropOutFace(picture, faces[0]) win := opencv.NewWindow("Face Detection") defer win.Destroy() win.ShowImage(opencv.DecodeImageMem(picture)) opencv.WaitKey(0) }
func main() { _, currentfile, _, _ := runtime.Caller(0) image := opencv.LoadImage(path.Join(path.Dir(currentfile), "../images/lena.jpg")) cascade := opencv.LoadHaarClassifierCascade(path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")) faces := cascade.DetectObjects(image) for _, value := range faces { opencv.Rectangle(image, opencv.Point{value.X() + value.Width(), value.Y()}, opencv.Point{value.X(), value.Y() + value.Height()}, opencv.ScalarAll(255.0), 1, 1, 0) } win := opencv.NewWindow("Face Detection") win.ShowImage(image) opencv.WaitKey(0) }
func main() { win := opencv.NewWindow("Go-OpenCV Webcam Face Detection") defer win.Destroy() cap := opencv.NewCameraCapture(0) if cap == nil { panic("cannot open camera") } defer cap.Release() cwd, err := os.Getwd() if err != nil { panic(err) } cascade := opencv.LoadHaarClassifierCascade(path.Join(cwd, "haarcascade_frontalface_alt.xml")) fmt.Println("Press ESC to quit") for { if cap.GrabFrame() { img := cap.RetrieveFrame(1) if img != nil { faces := cascade.DetectObjects(img) for _, value := range faces { opencv.Circle(img, opencv.Point{ value.X() + (value.Width() / 2), value.Y() + (value.Height() / 2), }, value.Width()/2, opencv.ScalarAll(255.0), 1, 1, 0) } win.ShowImage(img) } else { fmt.Println("nil image") } } key := opencv.WaitKey(1) if key == 27 { os.Exit(0) } } }
func main() { cap := opencv.NewCameraCapture(0) if cap == nil { panic("cannot open camera") } defer cap.Release() cwd, err := os.Getwd() if err != nil { panic(err) } cascade := opencv.LoadHaarClassifierCascade(path.Join(cwd, "haarcascade_frontalface_alt.xml")) fmt.Println("Press ESC to quit") for { if cap.GrabFrame() { img := cap.RetrieveFrame(1) if img != nil { faces := cascade.DetectObjects(img) for _, value := range faces { leftX := value.X() rightX := leftX + value.Width() lowY := value.Y() highY := lowY + value.Height() log.Println("Face Seen! LowLeft Corner: (", leftX, ", ", lowY, "), UpRight Corner: (", rightX, ", ", highY, ")") } } else { fmt.Println("nil image") } } key := opencv.WaitKey(1) if key == 27 { os.Exit(0) } } }
func main() { filename := "../data/???.avi" if len(os.Args) == 2 { filename = os.Args[1] } else { fmt.Printf("Usage: go run player.go videoname\n") os.Exit(0) } cap := opencv.NewFileCapture(filename) if cap == nil { panic("can not open video") } defer cap.Release() win := opencv.NewWindow("GoOpenCV: VideoPlayer") defer win.Destroy() fps := int(cap.GetProperty(opencv.CV_CAP_PROP_FPS)) frames := int(cap.GetProperty(opencv.CV_CAP_PROP_FRAME_COUNT)) stop := false win.SetMouseCallback(func(event, x, y, flags int) { if flags&opencv.CV_EVENT_LBUTTONDOWN != 0 { stop = !stop if stop { fmt.Printf("status: stop") } else { fmt.Printf("status: palying") } } }) win.CreateTrackbar("Seek", 1, frames, func(pos int) { cur_pos := int(cap.GetProperty(opencv.CV_CAP_PROP_POS_FRAMES)) if pos != cur_pos { cap.SetProperty(opencv.CV_CAP_PROP_POS_FRAMES, float64(pos)) fmt.Printf("Seek to %d(%d)\n", pos, frames) } }) for { if !stop { img := cap.QueryFrame() if img == nil { break } frame_pos := int(cap.GetProperty(opencv.CV_CAP_PROP_POS_FRAMES)) if frame_pos >= frames { break } win.SetTrackbarPos("Seek", frame_pos) win.ShowImage(img) key := opencv.WaitKey(1000 / fps) if key == 27 { os.Exit(0) } } else { key := opencv.WaitKey(20) if key == 27 { os.Exit(0) } } } opencv.WaitKey(0) }
func main() { filename := "../images/fruits.jpg" if len(os.Args) == 2 { filename = os.Args[1] } img0 := opencv.LoadImage(filename) if img0 == nil { panic("LoadImage fail") } defer img0.Release() fmt.Print("Hot keys: \n", "\tESC - quit the program\n", "\tr - restore the original image\n", "\ti or ENTER - run inpainting algorithm\n", "\t\t(before running it, paint something on the image)\n", ) img := img0.Clone() inpainted := img0.Clone() inpaint_mask := opencv.CreateImage(img0.Width(), img0.Height(), 8, 1) opencv.Zero(inpaint_mask) //opencv.Zero( inpainted ) win := opencv.NewWindow("image") defer win.Destroy() prev_pt := opencv.Point{-1, -1} win.SetMouseCallback(func(event, x, y, flags int, param ...interface{}) { if img == nil { os.Exit(0) } if event == opencv.CV_EVENT_LBUTTONUP || (flags&opencv.CV_EVENT_FLAG_LBUTTON) == 0 { prev_pt = opencv.Point{-1, -1} } else if event == opencv.CV_EVENT_LBUTTONDOWN { prev_pt = opencv.Point{x, y} } else if event == opencv.CV_EVENT_MOUSEMOVE && (flags&opencv.CV_EVENT_FLAG_LBUTTON) != 0 { pt := opencv.Point{x, y} if prev_pt.X < 0 { prev_pt = pt } rgb := opencv.ScalarAll(255.0) opencv.Line(inpaint_mask, prev_pt, pt, rgb, 5, 8, 0) opencv.Line(img, prev_pt, pt, rgb, 5, 8, 0) prev_pt = pt win.ShowImage(img) } }) win.ShowImage(img) opencv.WaitKey(0) win2 := opencv.NewWindow("inpainted image") defer win2.Destroy() win2.ShowImage(inpainted) for { key := opencv.WaitKey(20) if key == 27 { os.Exit(0) } else if key == 'r' { opencv.Zero(inpaint_mask) opencv.Copy(img0, img, nil) win.ShowImage(img) } else if key == 'i' || key == '\n' { opencv.Inpaint(img, inpaint_mask, inpainted, 3, opencv.CV_INPAINT_TELEA, ) win2.ShowImage(inpainted) } } os.Exit(0) }
func main() { filename := "../images/lena.jpg" if len(os.Args) == 2 { filename = os.Args[1] } image := opencv.LoadImage(filename) if image == nil { panic("LoadImage fail") } defer image.Release() w := image.Width() h := image.Height() // Create the output image cedge := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 3) defer cedge.Release() // Convert to grayscale gray := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) edge := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) defer gray.Release() defer edge.Release() opencv.CvtColor(image, gray, opencv.CV_BGR2GRAY) win := opencv.NewWindow("Edge") defer win.Destroy() win.SetMouseCallback(func(event, x, y, flags int, param ...interface{}) { fmt.Printf("event = %d, x = %d, y = %d, flags = %d\n", event, x, y, flags, ) }) win.CreateTrackbar("Thresh", 1, 100, func(pos int, param ...interface{}) { edge_thresh := pos opencv.Smooth(gray, edge, opencv.CV_BLUR, 3, 3, 0, 0) opencv.Not(gray, edge) // Run the edge detector on grayscale opencv.Canny(gray, edge, float64(edge_thresh), float64(edge_thresh*3), 3) opencv.Zero(cedge) // copy edge points opencv.Copy(image, cedge, edge) win.ShowImage(cedge) fmt.Printf("pos = %d\n", pos) }) win.ShowImage(image) for { key := opencv.WaitKey(20) if key == 27 { os.Exit(0) } } os.Exit(0) }
func MarkRoi() { // // var fourcc = opencv.FOURCC('m','p','4','v') // var fps = float32(video.GetProperty(opencv.CV_CAP_PROP_FPS)) // var size = opencv.Size{int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH)), // int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))}; // // log.Print("fourcc: ", fourcc) // log.Print("fps: ", fps) // log.Print("size: ", size) // // defer video.Release() win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() win2 := opencv.NewWindow("rect cam: ") win2.Move(400, 0) defer win2.Destroy() _, currentfile, _, _ := runtime.Caller(0) var cascade = path.Join(path.Dir(currentfile), "/../cascades/haarcascade_eye_tree_eyeglasses.xml") var eyeMarker = NewEyeMarker(cascade) var cap = NewVideoCapture(path.Join(path.Dir(currentfile), "/../eyevid1.mp4")) //var cap = NewVideoCapture("") var rois []*opencv.Rect for { var frame = cap.GetFrame() if frame == nil { break } img := frame var copy = opencv.CreateImage(img.Width(), img.Height(), opencv.IPL_DEPTH_8U, 3) opencv.Copy(img, copy, nil) rois = eyeMarker.MarkEyesRects(copy) if len(rois) > 0 { log.Print(rois[0]) copy.SetROI(*rois[0]) copy = ProcessRoi(copy, 25) //var gray = opencv.CreateImage(copy.Width(),copy.Height(),opencv.IPL_DEPTH_8U,1) //copy.SetROI(*rois[0]) var circels = HoughCircles1(copy) log.Print(circels) for i := 0; i < len(circels); i++ { var center = opencv.Point{rois[0].X() + int(circels[i].X), rois[0].Y() + int(circels[0].Y)} //var center = opencv.Point{int(circels[i].X),int(circels[0].Y)} opencv.Circle(img, center, int(circels[i].R), opencv.NewScalar(255, 0, 0, 1), 1, 1, 0) //src *IplImage, dp float64, min_dist float64, param1 float64, param2 float64, min_radius int, max_radius int } copy.ResetROI() } win.ShowImage(img) win2.ShowImage(copy) key := opencv.WaitKey(10) if key == 27 { os.Exit(0) } } os.Exit(0) }
func ProcessImageTest() { log.Print("Try to open webcam.") cap := opencv.NewCameraCapture(0) if cap == nil { panic("can not open camera") } log.Print("webcame opened.") var outputFileName = "testvideo.mpeg" var width = 640 var height = 480 var fps = 20.0 var fourcc = opencv.FOURCC('m', 'p', '4', 'v') var noOfCapturedFrames = 100 cap.SetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH, float64(width)) cap.SetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT, float64(height)) cap.SetProperty(opencv.CV_CAP_PROP_FPS, fps) log.Print("capturing ", noOfCapturedFrames, " frames and save them to the following file: ", outputFileName) log.Print("width: ", width) log.Print("height: ", height) log.Print("fps: ", fps) log.Print("fourcc: ", fourcc) var frames []*opencv.IplImage for len(frames) < noOfCapturedFrames { if cap.GrabFrame() { img := cap.RetrieveFrame(1) if img != nil { var frame = cap.RetrieveFrame(1) var copy = opencv.CreateImage(int(width), int(height), frame.Depth(), frame.Channels()) defer copy.Release() opencv.Copy(frame, copy, nil) frames = append(frames, copy) } else { fmt.Println("Image ins nil") } } } cap.Release() win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() for i := 0; i < len(frames); i++ { log.Print("displaying frame no: ", i) win.ShowImage(frames[i]) key := opencv.WaitKey(10) if key != 0 { log.Print(key) } } log.Print("start writing frames to file.") var videoWritter = NewVideoWriter(fourcc, float32(fps), opencv.Size{int(width), int(height)}) videoWritter.SaveAsVideo(frames, outputFileName) log.Print("created file with name: ", outputFileName) os.Exit(0) }