All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ociweb.pronghorn.image.ImageGraphBuilder Maven / Gradle / Ivy

package com.ociweb.pronghorn.image;

import com.ociweb.pronghorn.image.schema.CalibrationStatusSchema;
import com.ociweb.pronghorn.image.schema.LocationModeSchema;
import com.ociweb.pronghorn.image.schema.ImageSchema;
import com.ociweb.pronghorn.pipe.ChannelReader;
import com.ociweb.pronghorn.pipe.Pipe;
import com.ociweb.pronghorn.pipe.PipeConfig;
import com.ociweb.pronghorn.pipe.RawDataSchema;
import com.ociweb.pronghorn.stage.file.FileBlobReadStage;
import com.ociweb.pronghorn.stage.file.FileBlobWriteStage;
import com.ociweb.pronghorn.stage.math.HistogramSchema;
import com.ociweb.pronghorn.stage.math.HistogramSelectPeakStage;
import com.ociweb.pronghorn.stage.math.HistogramSumStage;
import com.ociweb.pronghorn.stage.math.ProbabilitySchema;
import com.ociweb.pronghorn.stage.route.RawDataJoinerStage;
import com.ociweb.pronghorn.stage.route.RawDataSplitterStage;
import com.ociweb.pronghorn.stage.route.ReplicatorStage;
import com.ociweb.pronghorn.stage.scheduling.GraphManager;
import com.ociweb.pronghorn.stage.test.PipeCleanerStage;
import com.ociweb.pronghorn.stage.test.PipeNoOp;

public class ImageGraphBuilder {

	public static final int DOWNSCALE_WIDTH = 320;
	public static final int DOWNSCALE_HEIGHT = 240;

	public static void buildLocationDetectionGraph(GraphManager gm, 
			String loadFilePath, String saveFilePath,
			Pipe imagePipe, //input pipe for the raw image data
			Pipe modeSelectionPipe, //input pipe to turn on learning mode or cancel learning mode.
			Pipe probLocation, //output pipe sending probable locations
			Pipe calibrationDone //output pipe sending training is complete
			) {

		if (null == calibrationDone) {
			calibrationDone = CalibrationStatusSchema.instance.newPipe(8, 0);
			PipeCleanerStage.newInstance(gm, calibrationDone);
		}
		
		if (null == probLocation) {					
			probLocation = ProbabilitySchema.instance.newPipe(8, 50);
			PipeCleanerStage.newInstance(gm, probLocation);
		}
		
		//////////////////////////////////
		//Pipe definitions
		/////////////////////////////////
		
		Pipe loadDataRaw   = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe loadDataRed   = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe loadDataGreen = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe loadDataBlue  = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe loadDataMono  = RawDataSchema.instance.newPipe(4, 1<<10);
		
	
		Pipe saveDataRaw   = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe saveDataRed   = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe saveDataGreen = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe saveDataBlue  = RawDataSchema.instance.newPipe(4, 1<<10);
		Pipe saveDataMono  = RawDataSchema.instance.newPipe(4, 1<<10);

		//these are longer to hold the data while the thread is away.
		Pipe imageR = ImageSchema.instance.newPipe(3*(DOWNSCALE_HEIGHT+1), DOWNSCALE_WIDTH); 
		Pipe imageG = ImageSchema.instance.newPipe(3*(DOWNSCALE_HEIGHT+1), DOWNSCALE_WIDTH);
		Pipe imageB = ImageSchema.instance.newPipe(3*(DOWNSCALE_HEIGHT+1), DOWNSCALE_WIDTH);
		Pipe imageM = ImageSchema.instance.newPipe(3*(DOWNSCALE_HEIGHT+1), DOWNSCALE_WIDTH);

		int maxUnits = 10_000;
		int maxHistogramLen = ChannelReader.PACKED_LONG_SIZE * maxUnits;
		
		Pipe histR = HistogramSchema.instance.newPipe(4, maxHistogramLen);
		Pipe histG = HistogramSchema.instance.newPipe(4, maxHistogramLen);
		Pipe histB = HistogramSchema.instance.newPipe(4, maxHistogramLen);
		Pipe histM = HistogramSchema.instance.newPipe(4, maxHistogramLen);
		
		Pipe histSum = HistogramSchema.instance.newPipe(4, maxHistogramLen);		

		
		Pipe calibrationDoneRoot = PipeConfig.pipe(calibrationDone.config().shrink2x());			
		
		Pipe calibrationDoneAckR = PipeConfig.pipe(calibrationDone.config());
		Pipe calibrationDoneAckG = PipeConfig.pipe(calibrationDone.config());
		Pipe calibrationDoneAckB = PipeConfig.pipe(calibrationDone.config());
		Pipe calibrationDoneAckM = PipeConfig.pipe(calibrationDone.config());
		
		
		//build an empty selector if one is not provided
		if (null == modeSelectionPipe) {
			modeSelectionPipe = LocationModeSchema.instance.newPipe(6,0);
			PipeNoOp.newInstance(gm, modeSelectionPipe);
		}
		
		PipeConfig msConfig = modeSelectionPipe.config().grow2x();		
		Pipe modeSelectionR = PipeConfig.pipe(msConfig);
		Pipe modeSelectionG = PipeConfig.pipe(msConfig);
		Pipe modeSelectionB = PipeConfig.pipe(msConfig);
		Pipe modeSelectionM = PipeConfig.pipe(msConfig);		
	    
	    Pipe calibrationDoneR = CalibrationStatusSchema.instance.newPipe(6, 0);
	    Pipe calibrationDoneG = CalibrationStatusSchema.instance.newPipe(6, 0); 
	    Pipe calibrationDoneB = CalibrationStatusSchema.instance.newPipe(6, 0);
	    Pipe calibrationDoneM = CalibrationStatusSchema.instance.newPipe(6, 0); 
		////////////////////////////////////////
		//Stage definitions
		////////////////////////////////////////
		
		
		ImageDownscaleStage.newInstance(gm, imagePipe, new Pipe[] {imageR, imageG, imageB, imageM}, DOWNSCALE_WIDTH, DOWNSCALE_HEIGHT) ;
	    
		//data is only read once on startup
		FileBlobReadStage.newInstance(gm, loadDataRaw, loadFilePath, false);		
		
		RawDataSplitterStage.newInstance(gm, loadDataRaw,
				                    loadDataRed, loadDataGreen, loadDataBlue, loadDataMono);

		
		ReplicatorStage.newInstance(gm, modeSelectionPipe, modeSelectionR, modeSelectionG, modeSelectionB, modeSelectionM);
				
		
		ReplicatorStage.newInstance(gm, calibrationDoneRoot, calibrationDone, calibrationDoneAckR, calibrationDoneAckG, calibrationDoneAckB, calibrationDoneAckM );
		
	    
	    CalibrationCyclicBarierStage.newInstance(gm, 
	    					calibrationDoneRoot,
	    		            calibrationDoneR, calibrationDoneG, calibrationDoneB, calibrationDoneM);
	    
	    //modeSelectionPipe
		MapImageStage.newInstance(gm, imageR, modeSelectionR, histR, calibrationDoneAckR, calibrationDoneR, loadDataRed,   saveDataRed, "Red");
		MapImageStage.newInstance(gm, imageG, modeSelectionG, histG, calibrationDoneAckG, calibrationDoneG, loadDataGreen, saveDataGreen, "Green");
		MapImageStage.newInstance(gm, imageB, modeSelectionB, histB, calibrationDoneAckB, calibrationDoneB, loadDataBlue,  saveDataBlue, "Blue");
		MapImageStage.newInstance(gm, imageM, modeSelectionM, histM, calibrationDoneAckM, calibrationDoneM, loadDataMono,  saveDataMono, "Mono");
		
		
		HistogramSumStage.newInstance(gm, histSum, histR, histG, histB, histM);
		HistogramSelectPeakStage.newInstance(gm, histSum, probLocation );

		RawDataJoinerStage.newInstance(gm, saveDataRaw, 
				          saveDataRed, saveDataGreen, saveDataBlue, saveDataMono);

		FileBlobWriteStage.newInstance(gm, saveDataRaw, saveFilePath);

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy