2013-02-13 10 views
7

मैं उपायों की व्यक्तिगत रेखा निकालने का एक तरीका जानना चाहता हूं। मुझे यकीन नहीं है कि इसके लिए एक एल्गोरिदम पहले से मौजूद है, इसलिए मैंने एक शीट संगीत को बाएं से दाएं स्कैन करने का विचार किया है, सभी सफेद रिक्त स्थान ऊपर और नीचे उपायों की रेखा से निकालें।शीट संगीत से प्रत्येक पंक्ति पर उपायों को निकालें

मैं शीट संगीत को MusicXML में परिवर्तित करने या अन्य उपयोगी जानकारी निकालने का कोई तरीका नहीं ढूंढ रहा हूं। नहीं, अनिवार्य रूप से मैं जो कर रहा हूं वह नियमित दस्तावेज है। मुझे पैराग्राफ को अलग करने की जरूरत है। मुझे अनुच्छेद द्वारा दी गई जानकारी में रूचि नहीं है बल्कि दस्तावेज़ के क्षेत्रों से अलग-अलग उन्हें अलग कर रहा है। इस मामले में एक पैराग्राफ उपायों की एक पंक्ति होगी। मुझे व्यक्तिगत उपायों की आवश्यकता नहीं है लेकिन शीट संगीत की प्रत्येक पंक्ति पर सभी उपाय हैं।

enter image description here

यह मान लें कि आप PDF फ़ाइल में शीट संगीत है उत्पादन में से एक मैं पूरी शीट संगीत से लेकिन शीर्षक, संगीतकार के बिना चाहते हैं और आदि enter image description here

+1

क्या आपको कर्मचारियों के ऊपर/नीचे नोट्स की भी आवश्यकता है? कभी-कभी वे थोड़ा उलझन में आ सकते हैं। – Hannele

+2

'उपायों की व्यक्तिगत रेखा निकालने' का क्या अर्थ है? एक संगीत शब्द नहीं है। – EJP

+1

कोई सीधा जवाब नहीं है, लेकिन 'पायथन शीट संगीत ओसीआर' के लिए Googleिंग, मुझे एक और स्टैक प्रश्न :: http://stackoverflow.com/questions/675077/ocr-for-sheet-music :: जो आपकी मदद कर सकता है – pyInTheSky

उत्तर

4

है, मैं होगा एक इनपुट पीडीएफ फ़ाइल से शीट संगीत युक्त छवियों को प्राप्त करने के लिए Apache PDFBox का उपयोग करें, फिर आपको आवश्यक पूरे बार के निर्देशांक का पता लगाएं, एक चयनित छवि के साथ छवि को फसल करने के लिए निर्देशांक परिभाषित करें और वांछित परिणाम प्राप्त होने तक इसे कुशलतापूर्वक परिभाषित करें।

PDDocument document = null; 
document = PDDocument.load(inFile); 
List pages = document.getDocumentCatalog().getAllPages(); 
Iterator iter = pages.iterator(); 
while (iter.hasNext()) { 
      PDPage page = (PDPage) iter.next(); 
      PDResources resources = page.getResources(); 
      Map pageImages = resources.getImages(); 
      if (pageImages != null) { 
       Iterator imageIter = pageImages.keySet().iterator(); 
       while (imageIter.hasNext()) { 
        String key = (String) imageIter.next(); 
        PDXObjectImage image = (PDXObjectImage) pageImages.get(key); 
        image.write2OutputStream(/* some output stream */); 
       } 
      } 
} 

अपाचे पीडीएफबॉक्स में एक नमूना कोड उपलब्ध है।

import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.PDResources; 
import org.apache.pdfbox.pdmodel.encryption.AccessPermission; 
import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial; 
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject; 
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm; 
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; 

/** 
* This will read a read pdf and extract images. <br/><br/> 
* 
* usage: java org.apache.pdfbox.ExtractImages &lt;pdffile&gt; &lt;password&gt; [imageprefix] 
* 
* @author <a href="mailto:[email protected]">Ben Litchfield</a> 
* @version $Revision: 1.7 $ 
*/ 
public class ExtractImages 
{ 
    private int imageCounter = 1; 

    private static final String PASSWORD = "-password"; 
    private static final String PREFIX = "-prefix"; 
    private static final String ADDKEY = "-addkey"; 
    private static final String NONSEQ = "-nonSeq"; 

    private ExtractImages() 
    { 
    } 

    /** 
    * This is the entry point for the application. 
    * 
    * @param args The command-line arguments. 
    * 
    * @throws Exception If there is an error decrypting the document. 
    */ 
    public static void main(String[] args) throws Exception 
    { 
     ExtractImages extractor = new ExtractImages(); 
     extractor.extractImages(args); 
    } 

    private void extractImages(String[] args) throws Exception 
    { 
     if(args.length < 1 || args.length > 4) 
     { 
      usage(); 
     } 
     else 
     { 
      String pdfFile = null; 
      String password = ""; 
      String prefix = null; 
      boolean addKey = false; 
      boolean useNonSeqParser = false; 
      for(int i=0; i<args.length; i++) 
      { 
       if(args[i].equals(PASSWORD)) 
       { 
        i++; 
        if(i >= args.length) 
        { 
         usage(); 
        } 
        password = args[i]; 
       } 
       else if(args[i].equals(PREFIX)) 
       { 
        i++; 
        if(i >= args.length) 
        { 
         usage(); 
        } 
        prefix = args[i]; 
       } 
       else if(args[i].equals(ADDKEY)) 
       { 
        addKey = true; 
       } 
       else if(args[i].equals(NONSEQ)) 
       { 
        useNonSeqParser = true; 
       } 
       else 
       { 
        if(pdfFile == null) 
        { 
         pdfFile = args[i]; 
        } 
       } 
      } 
      if(pdfFile == null) 
      { 
       usage(); 
      } 
      else 
      { 
       if(prefix == null && pdfFile.length() >4) 
       { 
        prefix = pdfFile.substring(0, pdfFile.length() -4); 
       } 

       PDDocument document = null; 

       try 
       { 
        if (useNonSeqParser) 
        { 
         document = PDDocument.loadNonSeq(new File(pdfFile), null, password); 
        } 
        else 
        { 
         document = PDDocument.load(pdfFile); 

         if(document.isEncrypted()) 
         { 
          StandardDecryptionMaterial spm = new StandardDecryptionMaterial(password); 
          document.openProtection(spm); 
         } 
        } 
        AccessPermission ap = document.getCurrentAccessPermission(); 
        if(! ap.canExtractContent()) 
        { 
         throw new IOException(
          "Error: You do not have permission to extract images."); 
        } 

        List pages = document.getDocumentCatalog().getAllPages(); 
        Iterator iter = pages.iterator(); 
        while(iter.hasNext()) 
        { 
         PDPage page = (PDPage)iter.next(); 
         PDResources resources = page.getResources(); 
         // extract all XObjectImages which are part of the page resources 
         processResources(resources, prefix, addKey); 
        } 
       } 
       finally 
       { 
        if(document != null) 
        { 
         document.close(); 
        } 
       } 
      } 
     } 
    } 

    private void processResources(PDResources resources, String prefix, boolean addKey) throws IOException 
    { 
     if (resources == null) 
     { 
      return; 
     } 
     Map<String, PDXObject> xobjects = resources.getXObjects(); 
     if(xobjects != null) 
     { 
      Iterator<String> xobjectIter = xobjects.keySet().iterator(); 
      while(xobjectIter.hasNext()) 
      { 
       String key = xobjectIter.next(); 
       PDXObject xobject = xobjects.get(key); 
       // write the images 
       if (xobject instanceof PDXObjectImage) 
       { 
        PDXObjectImage image = (PDXObjectImage)xobject; 
        String name = null; 
        if (addKey) 
        { 
         name = getUniqueFileName(prefix + "_" + key, image.getSuffix()); 
        } 
        else 
        { 
         name = getUniqueFileName(prefix, image.getSuffix()); 
        } 
        System.out.println("Writing image:" + name); 
        image.write2file(name); 
       } 
       // maybe there are more images embedded in a form object 
       else if (xobject instanceof PDXObjectForm) 
       { 
        PDXObjectForm xObjectForm = (PDXObjectForm)xobject; 
        PDResources formResources = xObjectForm.getResources(); 
        processResources(formResources, prefix, addKey); 
       } 
      } 
     } 
    } 

    private String getUniqueFileName(String prefix, String suffix) 
    { 
     String uniqueName = null; 
     File f = null; 
     while(f == null || f.exists()) 
     { 
      uniqueName = prefix + "-" + imageCounter; 
      f = new File(uniqueName + "." + suffix); 
      imageCounter++; 
     } 
     return uniqueName; 
    } 

    /** 
    * This will print the usage requirements and exit. 
    */ 
    private static void usage() 
    { 
     System.err.println("Usage: java org.apache.pdfbox.ExtractImages [OPTIONS] <PDF file>\n" + 
      " -password <password>  Password to decrypt document\n" + 
      " -prefix <image-prefix>  Image prefix(default to pdf name)\n" + 
      " -addkey      add the internal image key to the file name\n" + 
      " -nonSeq      Enables the new non-sequential parser\n" + 
      " <PDF file>     The PDF document to use\n" 
      ); 
     System.exit(1); 
    } 

} 

अब चित्र काटने के लिए आप का उपयोग कर सकते हैं:

/** 
    * Crop the main image according to this rectangle, and scale it to the 
    * correct size for a thumbnail. 
    */ 
    public InputStream cropAndScale(InputStream mainImageStream, 
      CropRectangle crop) { 
     try { 
      RenderedOp mainImage = loadImage(mainImageStream); 
      RenderedOp opaqueImage = makeImageOpaque(mainImage); 
      RenderedOp croppedImage = cropImage(opaqueImage, crop); 
      RenderedOp scaledImage = scaleImage(croppedImage); 
      byte[] jpegBytes = encodeAsJpeg(scaledImage); 
      return new ByteArrayInputStream(jpegBytes); 
     } catch (Exception e) { 
      throw new IllegalStateException("Failed to scale the image", e); 
     } 
    } 

जो this page में उपलब्ध है और project

वहाँ एक पीडीएफ फाइल के अंदर छवियों पार्स करने के लिए अन्य विकल्प है, एक बार देख ले at this code विशेष रूप से this

+0

यह सुनिश्चित नहीं है कि यह उत्तर क्यों चुना गया क्योंकि यह पूरी तरह से उपयोगकर्ताओं के प्रश्न का उत्तर देने में विफल रहता है। –

संबंधित मुद्दे