View Single Post
  #6   Spotlight this post!  
Unread 08-02-2017, 22:02
MarlyM64 MarlyM64 is offline
Registered User
FRC #1660 (Harlem Knights)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2016
Location: New York City
Posts: 3
MarlyM64 is an unknown quantity at this point
Re: Need NetworkTables to update??(Grip)

Quote:
Originally Posted by wlogeais View Post
Seems to me your goal is really how to 'get' and display?

if (!pipeline.filterContoursOutput().isEmpty()) {
Rect r = Imgproc.boundingRect(pipeline.filterContoursOutput ().get(0));
SmartDashboard.putString("opencv", "at (" + r.x + ',' + r.y + ')' );
Thanks for answering my previous questions. We decided not to use NetworkTables because we were able to see some values directly based on the previous code.

We have been able to get the Rect values to display on the SmartDashboard, but they don't seem to be updating.

We noticed that the process() method in the pipeline is what actually updates the values, but the example code doesn't seem to use it. (We are able to see new values each time we restart the SmartDashboard!, but not updating).

We tried calling the process() method inside of the while loop of our init method. We attempted to grab a picture and pass in as the Mat parameter, but there were no rectangles being seen (even though we have been consistently seeing rectangles in the Grip software).

I'm stuck again on trying to figure out how to get the rectangles to update in real time.

Any help would be greatly appreciated!

This is our camInit() method:
Code:
public void camInit() {

		// NetworkTable.setIPAddress("10.16.60.63");
		// table = NetworkTable.getTable("marly");

		/* Creates UsbCamera and MjpegServer [1] and connects them */
		UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();

		/* Creates the CvSource and MjpegServer [2] and connects them */
		CvSource outputStream = CameraServer.getInstance().putVideo("steamVideo", 640, 480);

		/* Creates the CvSink and connects it to the UsbCamera */
		CvSink cvSink = CameraServer.getInstance().getVideo();
       Mat mat = new Mat();
       Mat outputMat = new Mat();
	//   camera.

		VisionThread visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
			while (camRunning) {
				ArrayList<MatOfPoint> amat = pipeline.filterContoursOutput();
				int tempNumRectangles = amat.size();
				
				Rect tempR0 = r0; //keep old value if you don't see it?
				System.out.println(" tempNumRectangles "+ tempNumRectangles);
				
				if (tempNumRectangles > 0){		//!pipeline.filterContoursOutput().isEmpty()) {

				    tempR0 = Imgproc.boundingRect(amat.get(0));
					Rect tempR1 = r1;		//keep old value if you don't see it?

					if (tempNumRectangles > 1) {
						tempR1 = Imgproc.boundingRect(amat.get(1));
					}
					synchronized(camLock){
						System.out.println(" here");
						r0 = tempR0;
						r1 = tempR1;
						numRectangles = tempNumRectangles;
					}
				}
				
//				System.out.print ("r0.x: " +r0.x + " r0.y: " +r0.y+ " r0.height: " + r0.height + " r0.width: "+r0.width);
				SmartDashboard.putNumber("rect0.x", r0.x);
				SmartDashboard.putNumber("rect0.y", r0.y);
				SmartDashboard.putNumber("rect0.height", r0.height);
				SmartDashboard.putNumber("rect0.width", r0.width);
				
	//			System.out.print ("r1.x: " +r1.x + " r1.y: " +r1.y+ " r1.height: " + r1.height + " r1.width: "+r1.width);
				SmartDashboard.putNumber("rect1.x", r1.x);
				SmartDashboard.putNumber("rect1.y", r1.y);
				SmartDashboard.putNumber("rect1.height", r1.height);
				SmartDashboard.putNumber("rect1.width", r1.width);

		//		System.out.println("\t rect count = " + numRectangles);
				SmartDashboard.putNumber("rect count", tempNumRectangles);
			
				try {
				    Thread.sleep(100);
				} catch(InterruptedException e){
					camRunning = false;
					System.out.println("Thread sleep exception");
				}
				
				if (cvSink.grabFrame(mat) == 0)
				{
					System.out.println("not working****");
					outputStream.notifyError(cvSink.getError());
					continue;
				}
				
				//System.out.println("working!!");
				
				pipeline.process(mat);
				//cvSink.grabFrame(mat);
                //Imgproc.cvtColor(mat, outputMat, Imgproc.COLOR_BGR2GRAY);
                //outputStream.putFrame(outputMat);
				//System.out.println(mat.toString());
				
			}
		});

		visionThread.start();

	}
Reply With Quote