Exemplo n.º 1
0
// DrawRectangles uses Rect array values to return image with rectangles drawn.
func DrawRectangles(image *cv.IplImage, rect []*cv.Rect, r int, g int, b int, thickness int) *cv.IplImage {
	for _, value := range rect {
		cv.Rectangle(image,
			cv.Point{value.X() + value.Width(), value.Y()},
			cv.Point{value.X(), value.Y() + value.Height()},
			cv.NewScalar(float64(b), float64(g), float64(r), 0), thickness, 1, 0)
	}
	return image
}
func CaptureFrames() {
	var cam = opencv.NewCameraCapture(0)
	defer cam.Release()

	frames := []*opencv.IplImage{}
	//var writter = opencv.NewVideoWriter("test.avi", int(opencv.FOURCC('D','I','V','3')),20, 50, 50, 1)

	win := opencv.NewWindow("Go-OpenCV Webcam123")
	defer win.Destroy()
	for framesCounter := 0; framesCounter <= 25; framesCounter++ {
		log.Print(framesCounter)
		if cam.GrabFrame() || framesCounter > 25 {
			//            var frame = cam.RetrieveFrame(1)
			//            var img = ProcessImage(frame, 50)
			//            frames = append(frames, img)

			var frame = cam.RetrieveFrame(1)
			win.ShowImage(frame)
			//            var img3 = frame.ToImage()

			var imageHeader = opencv.CreateImage(frame.Width(), frame.Height(), frame.Depth(), frame.Channels())

			for i := 0; i < frame.Width(); i++ {
				for j := 0; j < frame.Height(); j++ {

					scalar := frame.Get2D(i, j)
					var bgra = scalar.Val()
					var newscalar = opencv.NewScalar(bgra[0], bgra[1], bgra[2], 1)
					imageHeader.Set2D(i, j, newscalar)

				}
			}
			//            var img3Opencv = opencv.FromImage(img3)

			var copy = opencv.CreateImage(frame.Width(), frame.Height(), frame.Depth(), frame.Channels())
			opencv.Copy(frame, copy, nil)
			//            frames = append(frames, img3Opencv)

			//win.ShowImage(imageHeader)

		}
	}

	var videoWritter = NewVideoWriter(opencv.FOURCC('D', 'I', 'V', '3'), 20, opencv.Size{frames[0].Width(), frames[0].Height()})
	videoWritter.SaveAsVideo(frames, "test.avi")

}
Exemplo n.º 3
0
func processResponse(resp *http.Response) error {
	var imgExtension string

	// skip not modified or other weird headers that dont include payload
	if resp.StatusCode != 200 {
		return nil
	}

	// skip gzipped assets that ignored us when told no gzip
	if "gzip" == resp.Header.Get("Content-Encoding") {
		return nil
	}

	switch resp.Header.Get("Content-Type") {
	case "image/jpeg":
		imgExtension = ".jpg"
	case "image/png":
		imgExtension = ".png"
	case "image/gif":
		imgExtension = ".gif"
	}

	if "" != imgExtension {
		//log.Println("Processing", resp.Request.Method, resp.Request.URL.String())
		// save locally
		fileName := path.Join(os.TempDir(), strconv.FormatInt(time.Now().UnixNano(), 10)+imgExtension)
		file, _ := os.Create(fileName)
		defer os.Remove(fileName)

		// dump Body to file
		io.Copy(file, resp.Body)
		file.Close()
		resp.Body.Close()

		// detect faces
		image := opencv.LoadImage(fileName, -1)
		defer image.Release()
		if image != nil {
			cascade := opencv.LoadHaarClassifierCascade("./deface_files/haarcascade_frontalface_alt.xml")
			faces := cascade.DetectObjects(image)

			// replace faces
			if len(faces) > 0 {
				rageGuy := opencv.LoadImage("./deface_files/rage-guy.png", -1)
				defer rageGuy.Release()
				for _, value := range faces {
					faceWidth := value.Width()
					faceHeight := value.Height()
					faceX := value.X()
					faceY := value.Y()
					if (faceWidth <= 0) || (faceHeight <= 0) || (faceX > image.Width()) || (faceY > image.Height()) {
						//log.Println("Invalid rectangle for", resp.Request.Method, resp.Request.URL.String(), faceWidth, faceHeight, faceX, faceY)
						continue
					}

					resizedRageGuy := opencv.Resize(rageGuy, faceWidth, faceHeight, 0)
					rect := &opencv.Rect{}
					rect.Init(faceX, faceY, faceWidth, faceHeight)
					image.SetROI(*rect)
					// blending them horribly inefficiently because cvSplit/cvThreshold are not exposed in go
					// and dont want to link to C
					for y := 0; y < resizedRageGuy.Height(); y++ {
						for x := 0; x < resizedRageGuy.Width(); x++ {
							src := image.Get2D(x, y).Val()
							overlay := resizedRageGuy.Get2D(x, y).Val()
							if overlay[3] > 0 {
								image.Set2D(x, y, opencv.NewScalar(overlay[0], overlay[1], overlay[2], src[3]))
							}
						}
					}
					image.ResetROI()
					resizedRageGuy.Release()
				}
			}

			// save modified image and pass it to Body
			opencv.SaveImage(fileName, image, 0)
		} else {
			//log.Println("Couldnt load image", resp.Request.Method, resp.Request.URL.String())
		}

		file, _ = os.Open(fileName)
		resp.Body = ioutil.NopCloser(file)

		// update content length
		fileInfo, _ := file.Stat()
		resp.ContentLength = fileInfo.Size()
		resp.Header.Set("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
	}

	return nil
}
Exemplo n.º 4
0
	var eyeMarker = NewEyeMarker(cascade)

	var copy = opencv.CreateImage(v.currentFrameIn.Width(), v.currentFrameIn.Height(), v.currentFrameIn.Depth(), v.currentFrameIn.Channels())
	var copy2 = opencv.CreateImage(v.currentFrameIn.Width(), v.currentFrameIn.Height(), v.currentFrameIn.Depth(), v.currentFrameIn.Channels())
	opencv.Copy(v.currentFrameIn, copy, nil)
	opencv.Copy(v.currentFrameIn, copy2, nil)

	var rois = eyeMarker.MarkEyesRects(copy)

	if len(rois) > 0 {
		copy.SetROI(*rois[0])
		copy = ProcessRoi(copy, 75)
		//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(copy2, 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()
	}
	v.framesOut = append(v.framesOut, copy2)
}

type Process func(*VideoProcessor)
Exemplo n.º 5
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)
}