public class signal_identifier {
private static final Log LOG = LogFactory.getLog(signal_identifier.class);
public static void main(String[] args) throws Exception {
long t = cvGetTickCount();
Configuration conf = new Configuration();
long milliSeconds = 1800000;
conf.setLong("mapred.task.timeout", milliSeconds);
Job job = new Job(conf, "TrafficSignalProcessing");
job.setJarByClass(signal_identifier.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(signal_mapper.class);
job.setReducerClass(signal_reducer.class);
job.setInputFormatClass(VideoInputFormat.class);
job.setOutputValueClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/tmp/traffic_signal.mp4"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/tmp/ouputv"));
job.waitForCompletion(true);
}
}
Mapper类:
public class signal_mapper extends Mapper<Text, VideoObject, Text, IntWritable> {
private static final Log LOG = LogFactory.getLog(signal_mapper.class);
private static OpenCVFrameGrabber grabber;
private static IplImage currentFrame;
private static IplImage frame;
private static IplImage imgHSV;
private static IplImage imgThresholdr;
private static IplImage imgThresholdg;
private static IplImage imgC;
static int LowerRed = 160;
static int UpperRed = 180;
static int LowerGreen = 40;
static int UpperGreen = 80;
CvArr mask;
//private static final int FOURCC = CV_FOURCC('X', 'V', 'I', 'D');
public void map(Text key, VideoObject value, Context context, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException, InterruptedException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(value.getVideoByteArray());
LOG.info("Log__VideoConverter__byteArray: "+ byteArrayInputStream.available());
String fileName = key.toString();
int id = value.getId();
long differencer = 0;
long differenceg = 0;
long lStartTime = 0;
String flag = "start";
//LocalFileSystem fs = FileSystem.getLocal(context.getConfiguration());
Path filePath = new Path("/tmp", fileName);
//Path resFile = new Path("/tmp", "res_"+fileName);
System.out.println("File to Process :"+filePath.toString());
//FSDataOutputStream out = fs.create(filePath, true);
//out.write(value.getVideoByteArray());
//out.close();
try{
grabber = new OpenCVFrameGrabber(filePath.toString());
grabber.start();
CvMemStorage storage = CvMemStorage.create();
CvSize frameSize = new CvSize(grabber.getImageWidth(), grabber.getImageHeight());
currentFrame = cvCreateImage(frameSize, 8, 3);
IplImage cropped;// = cvCreateImage(frameSize, 8, 3);
CvRect r = new CvRect(250, 40, 350, 350);
System.out.println("Video processing .........started");
while(queryFrame()) {
cvClearMemStorage(storage);
if(flag.equals("start")){
lStartTime = new Date().getTime();
}
cvSetImageROI(currentFrame, r);
cropped = cvCreateImage(cvGetSize(currentFrame), currentFrame.depth(),currentFrame.nChannels());
// Copy original image (only ROI) to the cropped image
cvCopy(currentFrame, cropped);
imgHSV = cvCreateImage(cvGetSize(cropped), 8, 3);
cvCvtColor(cropped, imgHSV, CV_BGR2HSV);
imgThresholdr = cvCreateImage(cvGetSize(cropped), 8, 1);
imgThresholdg = cvCreateImage(cvGetSize(cropped), 8, 1);
imgC = cvCreateImage(cvGetSize(cropped),8,1);
cvInRangeS(imgHSV, cvScalar(LowerRed,150,75,0), cvScalar(UpperRed, 255, 255, 0), imgThresholdr);
cvInRangeS(imgHSV, cvScalar(LowerGreen,150,75,0), cvScalar(UpperGreen, 255, 255, 0), imgThresholdg);
Dimension positionr = getCoordinates(imgThresholdr);
int posr = positionr.width+positionr.height;
Dimension positiong = getCoordinates(imgThresholdg);
int posg = positiong.width+positiong.height;
//&& !flag.equalsIgnoreCase("red") && !flag.equalsIgnoreCase("green")
if(posr > 255 && posr < 265 ){
flag = "red";
}else {
long lEndTime = new Date().getTime();
differenceg = (lEndTime - lStartTime) - differencer;
output.collect(new Text("Green Color found at second- => "),new IntWritable((int)differenceg/1000));
//System.out.println("Green Color found at second: " + differenceg/1000);
}
if(posg > 430 && posg < 440){
flag = "green";
}else{
long lEndTime = new Date().getTime();
differencer = (lEndTime - lStartTime) - differenceg;
output.collect(new Text("Red Color found at second- => "),new IntWritable((int)differencer/1000));
//System.out.println("Red Color found at second: " + differencer/1000);
}
}
grabber.stop();
System.out.println("Video processing .........Completed");
}catch(Exception e) {
e.printStackTrace();
}
}
private static boolean queryFrame() throws Exception {
try {
IplImage frame = grabber.grab();
if (frame != null) {
cvConvertImage(frame, currentFrame, 0);
return true;
} else {
return false;
}
}catch(com.googlecode.javacv.FrameGrabber.Exception fge) {
return false;
}
catch(Exception e) {
return false;
}
}
static Dimension getCoordinates(IplImage thresholdImage) {
int posX = 0;
int posY = 0;
CvMoments moments = new CvMoments();
cvMoments(thresholdImage, moments, 1);
double momX10 = cvGetSpatialMoment(moments, 1, 0);
double momY01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
posX = (int) (momX10 / area);
posY = (int) (momY01 / area);
return new Dimension(posX, posY);
}
}
减速机等级:
public class signal_reducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
等等我将发布我的堆栈跟踪异常