2012-05-30 2 views
2
private static String[] testFiles = new String[]  {"img01.JPG","img02.JPG","img03.JPG","img04.JPG","img06.JPG","img07.JPG","img05.JPG"}; 
// private static String testFilespath = "/home/student/Desktop/images"; 
private static String testFilespath ="hdfs://localhost:54310/user/root/images"; 
//private static String indexpath = "/home/student/Desktop/indexDemo"; 
private static String testExtensive="/home/student/Desktop/images"; 

public static class MapClass extends MapReduceBase 
implements Mapper<Text, Text, Text, Text> { 
private Text input_image = new Text(); 
private Text input_vector = new Text(); 
    @Override 
public void map(Text key, Text value,OutputCollector<Text, Text> output,Reporter  reporter) throws IOException { 

System.out.println("CorrelogramIndex Method:"); 
     String featureString; 
int MAXIMUM_DISTANCE = 16; 
AutoColorCorrelogram.Mode mode = AutoColorCorrelogram.Mode.FullNeighbourhood; 
for (String identifier : testFiles) { 
      try (FileInputStream fis = new FileInputStream(testFilespath + "/" + identifier)) { 
    //Document doc = builder.createDocument(fis, identifier); 
//FileInputStream imageStream = new FileInputStream(testFilespath + "/" + identifier); 
BufferedImage bimg = ImageIO.read(fis); 
AutoColorCorrelogram vd = new AutoColorCorrelogram(MAXIMUM_DISTANCE, mode); 
       vd.extract(bimg); 
       featureString = vd.getStringRepresentation(); 
       double[] bytearray=vd.getDoubleHistogram(); 
       System.out.println("image: "+ identifier + " " + featureString); 

     } 
      System.out.println(" ------------- "); 
input_image.set(identifier); 
input_vector.set(featureString); 
    output.collect(input_image, input_vector); 
       } 

    } 
    } 

    public static class Reduce extends MapReduceBase 
    implements Reducer<Text, Text, Text, Text> { 

    @Override 
public void reduce(Text key, Iterator<Text> values, 
        OutputCollector<Text, Text> output, 
        Reporter reporter) throws IOException { 
    String out_vector=""; 

    while (values.hasNext()) { 
    out_vector.concat(values.next().toString()); 
} 
    output.collect(key, new Text(out_vector)); 
    } 
} 

static int printUsage() { 
System.out.println("image_mapreduce [-m <maps>] [-r <reduces>] <input> <output>"); 
ToolRunner.printGenericCommandUsage(System.out); 
return -1; 
} 


@Override 
    public int run(String[] args) throws Exception { 
JobConf conf = new JobConf(getConf(), image_mapreduce.class); 
conf.setJobName("image_mapreduce"); 

// the keys are words (strings) 
conf.setOutputKeyClass(Text.class); 
// the values are counts (ints) 
conf.setOutputValueClass(Text.class); 

conf.setMapperClass(MapClass.class);   
// conf.setCombinerClass(Reduce.class); 
conf.setReducerClass(Reduce.class); 

List<String> other_args = new ArrayList<String>(); 
for(int i=0; i < args.length; ++i) { 
    try { 
    if ("-m".equals(args[i])) { 
     conf.setNumMapTasks(Integer.parseInt(args[++i])); 
    } else if ("-r".equals(args[i])) { 
     conf.setNumReduceTasks(Integer.parseInt(args[++i])); 
    } else { 
     other_args.add(args[i]); 
    } 
    } catch (NumberFormatException except) { 
    System.out.println("ERROR: Integer expected instead of " + args[i]); 
    return printUsage(); 
    } catch (ArrayIndexOutOfBoundsException except) { 
    System.out.println("ERROR: Required parameter missing from " + 
         args[i-1]); 
    return printUsage(); 
    } 
} 



    FileInputFormat.setInputPaths(conf, other_args.get(0)); 
    //FileInputFormat.setInputPaths(conf,new Path("hdfs://localhost:54310/user/root/images")); 
FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1))); 

JobClient.runJob(conf); 
return 0; 
} 


public static void main(String[] args) throws Exception { 
int res = ToolRunner.run(new Configuration(), new image_mapreduce(), args); 
System.exit(res); 
} 

} 

`입력으로 여러 이미지 파일을 가져 와서 hdfs에 저장된 프로그램을 작성 중 &지도 기능에서 기능을 추출합니다. 어떻게 FileInputStream (일부 매개 변수)에서 이미지를 읽을 경로를 지정할 수 있습니까? 또는 여러 이미지 파일을 읽을 방법이 있습니까?map-reduce에서 hdfs의 입력으로 여러 개의 이미지 파일을 읽는 방법?

내가 원하는 작업은 다음과 같습니다. - 입력으로 hdfs에서 여러 이미지 파일을 사용합니다. -지도 기능에서 기능을 추출합니다. - itearatively. 코드 또는 더 나은 방법으로 도와주세요.

답변

1

HIPI library을 사용하십시오. 이미지 모음을 ImageBundle에 저장합니다 (개별 이미지 파일을 HDFS에 저장하는 것이 더 효율적입니다). 그들도 몇 가지 예를 가지고 있습니다.

코드의 경우 사용할 입력 및 출력 형식을 지정해야합니다. 현재 파일을 넘기는 현재 입력 형식은 없지만 FileInputFormat을 확장하고 <Text, BytesWritable> 쌍을내는 RecordReader를 만들 수 있습니다. 여기서 키는 파일 이름이고 값은 이미지 파일의 바이트입니다. 사실 Hadoop - The Definitive Guide에서

이 정확한 입력 포맷의 예를 가지고 :

+0

@Chris ... 답장을 보내 주셔서 감사합니다.하지만 현재 LIRe, lucene API 만 사용하고 있습니다 ... 제가 작성한 코드가 맞습니까? – Amnesiac

+0

코드에 대한 의견이 업데이트되었습니다. –

+0

@Chris ... 다시 한 번 감사드립니다.하지만 경로를 지정하는 방법을 모르십니까? 위의 코드에 지정된 경로가 정확합니까? 위의 코드를 편집하고 필요한 코드로 변환하면 도움이 될 것입니다. 감사합니다 .. – Amnesiac

0

방금 ​​입력의 디렉토리에 conf.setFileInputPath()를 설정 MR 작업에 대한 입력으로 모든 이미지를 보내려면 특정 폴더에 선택적 이미지를 보내려는 경우 conf.setFileInputPath();를 설정할 때 여러 경로를 추가 할 수 있습니다.

한 가지 방법은 각 이미지에 대해 경로 [] 하나를 만드는 것입니다. 또는 모든 경로와 함께 쉼표로 구분 된 문자열로 설정하십시오. 다음 문서

http://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapred/FileInputFormat.html

그리고 당신은 텍스트와지도 입력 형식을 설정해야 할 한 가지 더를 통해 이동이 ByteArray 가 된 ByteArray 입력하는 대신 새로운 FileInputStream에를 만드는의 이미지 기능을 얻을.

관련 문제