func dendriteCam(deltaE chan float32, config Configuration) {
	camera := C.cvCaptureFromCAM(-1)

	// Shutdown dendrite if no camera detected.
	if camera == nil {
		fmt.Printf("WARNING: No camera detected. Shutting down DendriteCam\n")
		return
	}

	C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_FRAME_WIDTH, 160)
	C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_FRAME_HEIGHT, 120)

	// Capture original frame.
	prev := C.cvCloneImage(C.cvQueryFrame(camera))

	// Save out the first frame for debuging purposes.
	file := C.CString("frame.png")
	C.cvSaveImage(file, unsafe.Pointer(prev), nil)
	C.free(unsafe.Pointer(file))

	flow := C.cvCreateImage(C.cvSize(prev.width, prev.height), C.IPL_DEPTH_32F, 2)
	prevG := C.cvCreateImage(C.cvSize(prev.width, prev.height), C.IPL_DEPTH_8U, 1)
	nextG := C.cvCreateImage(C.cvSize(prev.width, prev.height), C.IPL_DEPTH_8U, 1)
	C.cvConvertImage(unsafe.Pointer(prev), unsafe.Pointer(prevG), 0)

	for {
		C.cvGrabFrame(camera)

		// Capture the new frame and convert it to grayscale.
		next := C.cvCloneImage(C.cvQueryFrame(camera))
		C.cvConvertImage(unsafe.Pointer(prev), unsafe.Pointer(prevG), 0)
		C.cvConvertImage(unsafe.Pointer(next), unsafe.Pointer(nextG), 0)

		C.cvCalcOpticalFlowFarneback(unsafe.Pointer(prevG), unsafe.Pointer(nextG), unsafe.Pointer(flow), 0.5, 2, 5, 2, 5, 1.1, 0)
		deltaE <- float32(calcDeltaEnergy(flow, &config))

		C.cvReleaseImage(&prev)
		prev = next
	}

	// Never reached - but here in comments for completeness.
	//C.cvReleaseImage(&prev)
	//C.cvReleaseImage(&nextG)
	//C.cvReleaseImage(&prevG)
	//C.cvReleaseImage(&flow)
	//C.cvReleaseCapture(&camera)
}
Exemple #2
0
func getVideoSource(videoFile string) (camera *C.CvCapture, err error) {
	if videoFile != "" {
		file := C.CString(videoFile)
		camera = C.cvCaptureFromFile(file)
		C.free(unsafe.Pointer(file))
		if camera == nil {
			return camera, errors.New("Unable to open a video file. Shutting down scout.")
		}

		return camera, nil
	} else {
		camera = C.cvCreateCameraCapture(0)
		if camera == nil {
			return camera, errors.New("Unable to open webcam. Shutting down scout.")
		}

		// Make sure the webcam is set to 1080p.
		C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_FRAME_WIDTH, 1920)
		C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_FRAME_HEIGHT, 1080)
		C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_BUFFERSIZE, 1)

		return camera, nil
	}
}
Exemple #3
0
func (capture *Capture) SetProperty(property_id int, value float64) int {
	rv := C.cvSetCaptureProperty((*C.CvCapture)(capture),
		C.int(property_id), C.double(value),
	)
	return int(rv)
}