speech recognition with cmu sphinx - doesn't work properly - java

I'm trying to use CMU Sphinx for speech recognition in java but the result I'm getting is not correct and I don't know why.
I have a .wav file I recorded with my voice saying some sentence in English.
Here is my code in java:
Configuration configuration = new Configuration();
// Set path to acoustic model.
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
// Set path to dictionary.
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
// Set language model.
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.dmp");
StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
recognizer.startRecognition(new FileInputStream("assets/voice/some_wav_file.wav"));
SpeechResult result = null;
while ((result = recognizer.getResult()) != null) {
System.out.println("~~ RESULTS: " + result.getHypothesis());
}
recognizer.stopRecognition();
}
catch(Exception e){
System.out.println("ERROR: " + e.getMessage());
}
I also have another code in Android that doesn't work as well:
Assets assets = new Assets(context);
File assetDir = assets.syncAssets();
String prefix = assetDir.getPath();
Config c = Decoder.defaultConfig();
c.setString("-hmm", prefix + "/en-us-ptm");
c.setString("-lm", prefix + "/en-us.lm");
c.setString("-dict", prefix + "/cmudict-en-us.dict");
Decoder d = new Decoder(c);
InputStream stream = context.getResources().openRawResource(R.raw.some_wav_file);
d.startUtt();
byte[] b = new byte[4096];
try {
int nbytes;
while ((nbytes = stream.read(b)) >= 0) {
ByteBuffer bb = ByteBuffer.wrap(b, 0, nbytes);
short[] s = new short[nbytes/2];
bb.asShortBuffer().get(s);
d.processRaw(s, nbytes/2, false, false);
}
} catch (IOException e) {
Log.d("ERROR: ", "Error when reading file" + e.getMessage());
}
d.endUtt();
Log.d("TOTAL RESULT: ", d.hyp().getHypstr());
for (Segment seg : d.seg()) {
Log.d("RESULT: ", seg.getWord());
}
I used this website to convert the wav file to 16bit, 16khz, mono and little-endian (tried all the options of it).
Any ideas why is doesn't work. I use the built in dictionaries and accustic models and my accent in English is not perfect (don't know if it matters).
EDIT:
This is my file. I recorded myself saying: "My baby is cute" and that's what I expect to be the output.
In the pure java code I get: "i've amy's youth" and in the android code I getl: " it"
Here is file containing the logs.

Your audio is somewhat corrupted by conversion. You should record into wav originally or into some other lossless format. Your pronunciation is also far from US English. For conversion between formats you can use sox instead of external website. Your android sample seems correct but it feels like you decode different file with android. You might check that you have actual proper file in resources.

Related

Image Not converting properly from tiff to jpg [duplicate]

recently i'm facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safari support this format). Due to certain constraint, i have to convert this format to others format that supported by major browser. However, it bring a lots of problem for me when i try to converting the format.
I had search through the web and although there been posted similar issue in this link How do I convert a TIF to PNG in Java?" but i can't have the result as it proposed..
Therefore i raise this Question again to wish that can have better explanation and guideline from you all..
There were few issue i'm faced during go through with the solution that proposed:
1) According to the answer that proposed by Jonathan Feinberg, it need to install JAI and JAI/ImageIO.
However, after i installed both of them i still couldn't import the file in Netbean 7.2. NetBean 7.2 remain propose import default imageIO library.
2) when i'm using default ImageIO library Read method, it will return NULL value and i cannot continue to proceed.
3) I also tried others method such as convert TIFF file to BIN File by using BufferedOutputStream method but the result file is greater than 11 MB which is too large to load and end up loading failed.
if (this.selectedDO != null) {
String tempDO = this.selectedDO.DONo;
String inPath = "J:\\" + tempDO + ".TIF";
String otPath = "J:\\" + tempDO + ".bin";
File opFile = new File(otPath);
File inFile = new File(inPath);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hence, hope that can get help and advise from you all so that i can convert TIFF format to other format such as JPEG/PNG.
Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.
SeekableStream s = new FileSeekableStream(inFile);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = dec.decodeAsRenderedImage(0);
FileOutputStream fos = new FileOutputStream(otPath);
JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
fos.flush();
fos.close();
otPath is the path that you would like to store your JPEG image.
For example: "C:/image/abc.JPG";
inFile is the input file which is the TIFF file
At least this method is workable to me. If there is any other better method, kindly share along with us.
EDIT: Just want to point out an editing in order to handle multipage tiff. Obviously you also have to handle the different names for the resulting images:
for (int page = 0; page < dec.getNumPages(); page++) {
RenderedImage op = dec.decodeAsRenderedImage(page );
...
}
Add dependency
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version> </dependency>
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1
Coding
final BufferedImage tif = ImageIO.read(new File("test.tif"));
ImageIO.write(tif, "png", new File("test.png"));
In case of many pages, working following:
add dependency:
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
use following Java8 code
public void convertTiffToPng(File file) {
try {
try (InputStream is = new FileInputStream(file)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
if (iterator == null || !iterator.hasNext()) {
throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
}
// We are just looking for the first reader compatible:
ImageReader reader = iterator.next();
reader.setInput(imageInputStream);
int numPage = reader.getNumImages(true);
// it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
String name = FilenameUtils.getBaseName(file.getAbsolutePath());
String parentFolder = file.getParentFile().getAbsolutePath();
IntStream.range(0, numPage).forEach(v -> {
try {
final BufferedImage tiff = reader.read(v);
ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
If your target is Android, you could try this great Java library on Github that provides many utilities for handling, opening, and writing .tiff files.
A simple example from that Git showng how to convert TIFF to JPEG:
TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
//Set to true if you want use java exception mechanism
options.throwExceptions = false;
//Available 128Mb for work
options.availableMemory = 128 * 1024 * 1024;
//Number of tiff directory to convert;
options.readTiffDirectory = 1;
//Convert to JPEG
TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener);
First, take a look to What is the best java image processing library/approach?. For your code you can use
javax.imageio.ImageIO.write(im, type, represFile);
like you can see in write an image to file example.

Read barcode from PDF file

Is there any Java API around that can find a bar code inside an opened file (scanned PDF) and then retrieve the data from the bar code?
Or how should I solve this problem?
If you're running on Windows, the ClearImage Barcode SDK has a Java API. It can read and decode barcodes from PDFs or images and provide the decoded data to you.
Some sample code:
public static void testDataMatrix () {
try {
String filename = "1.png ";
CiServer objCi = new CiServer();
Ci = objCi.getICiServer();
ICiDataMatrix reader = Ci.CreateDataMatrix(); // read DataMatrix Barcode
reader.getImage().Open(filename, 1);
int n = reader.Find(0); // find all the barcodes in the doc
for (i = 1; i <= n; i++) {
ICiBarcode Bc = reader.getBarcodes().getItem(i); // getItem is 1-based
System.out.println("Barcode " + i + " has Text: " + Bc.getText());
}
} catch (Exception ex) {System.out.println(ex.getMessage());}
}
Another one to consider is Accusoft's Barcode Xpress which also has a Java API.
Disclaimer: I've done some work for Inlite in the past.

Issue with reading Tiff image metadata with imageIO

I'm writing a program that is supposed to taking in a bunch of tiff's and put them together. I got it to work for most of the image files I read in but a large batch of them throw out an error when I try to read them in.
Here is a snippet of code I have:
int numPages = 0;
inStream = ImageIO.createImageInputStream(imageFile);
reader.setInput(inStream);
while(true){
bufferedImages.add(reader.readAll(numPages, reader.getDefaultReadParam()));
numPages++;
}
Yes I catch the out of bounds exception so we don't have to worry about that. My problem is that I get the following error:
javax.imageio.IIOException: I/O error reading image metadata!
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.readMetadata(TIFFImageReader.java:340)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.seekToImage(TIFFImageReader.java:310)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.prepareRead(TIFFImageReader.java:971)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1153)
at javax.imageio.ImageReader.readAll(ImageReader.java:1067)
at sel.image.appender.ImageAppender.mergeImages(ImageAppender.java:59)
at sel.imagenow.processor.AetnaLTCProcessor.processBatch(AetnaLTCProcessor.java:287)
at sel.imagenow.processor.AetnaLTCProcessor.processImpl(AetnaLTCProcessor.java:81)
at sel.processor.AbstractImageNowProcessor.process(AbstractImageNowProcessor.java:49)
at sel.RunConverter.main(RunConverter.java:37)
Caused by: java.io.EOFException
at javax.imageio.stream.ImageInputStreamImpl.readShort(ImageInputStreamImpl.java:229)
at javax.imageio.stream.ImageInputStreamImpl.readUnsignedShort(ImageInputStreamImpl.java:242)
at com.sun.media.imageioimpl.plugins.tiff.TIFFIFD.initialize(TIFFIFD.java:194)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageMetadata.initializeFromStream(TIFFImageMetadata.java:110)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader.readMetadata(TIFFImageReader.java:336)
... 9 more
I did make sure to add in the right JAI lib and my reader is using the "TIFF" type so the reader (and writer) is correct but for some reason the metadata is wrong. Now I can open and view all these images normally in windows so they really aren't corrupted or anything. Java just doesn't want to read them in right. Since I'm just using the stream meatadata to write them out later I don't care that much about the metadata I just need it to read in the file to the list so I can append it. I did find a writer.replaceImageMetaData method on the writer but the TIFFwriter version of IOWriter doens't have code for it. I'm stuck, anyone anything? Is there maybe a way to read in parts of the metadata to see what is wrong and fix it?
For anyone that would like to know I ended up fixing my own issue. It seems the the image metadata was a bit screwed up. Since I was just doing a plain merge and since I knew each image was one page I was able to use a buffered image to read in the picture then make it a IIOImage with null metadata. I used the stream metadata (which worked) to merge the images. Here is my complete method I use to merge a list of images:
public static File mergeImages(List<File> files, String argID, String fileType, String compressionType) throws Exception{
//find the temp location of the image
String location = ConfigManager.getInstance().getTempFileDirectory();
logger_.debug("image file type [" + fileType + "]");
ImageReader reader = ImageIO.getImageReadersByFormatName(fileType).next();
ImageWriter writer = ImageIO.getImageWritersByFormatName(fileType).next();
//set up the new image name
String filePath = location + "\\" + argID +"." + fileType;
//keeps track of the images we copied from
StringBuilder builder = new StringBuilder();
List<IIOImage> bufferedImages = new ArrayList<IIOImage>();
IIOMetadata metaData = null;
for (File imageFile:files) {
//get the name for logging later
builder.append(imageFile.getCanonicalPath()).append("\n");
if (metaData == null){
reader.setInput(ImageIO.createImageInputStream(imageFile));
metaData = reader.getStreamMetadata();
}
BufferedImage image = ImageIO.read(imageFile);
bufferedImages.add(new IIOImage(image, null, null));
}
ImageWriteParam params = writer.getDefaultWriteParam();
if (compressionType != null){
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionType(compressionType);
}
ImageOutputStream outStream = null;
try{
outStream = ImageIO.createImageOutputStream(new File(filePath));
int numPages = 0;
writer.setOutput(outStream);
for(IIOImage image:bufferedImages){
if (numPages == 0){
writer.write(metaData, image, params);
}
else{
writer.writeInsert(numPages, image, params);
}
numPages++;
}
}
finally{
if (outStream != null){
outStream.close();
}
}
//set up the file for us to use later
File mergedFile = new File(filePath);
logger_.info("Merged image into [" + filePath + "]");
logger_.debug("Merged images [\n" + builder.toString() + "] into --> " + filePath);
return mergedFile;
}
I hope this help someone else because I know there isn't much on this issue that I could find.

How to convert TIFF to JPEG/PNG in java

recently i'm facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safari support this format). Due to certain constraint, i have to convert this format to others format that supported by major browser. However, it bring a lots of problem for me when i try to converting the format.
I had search through the web and although there been posted similar issue in this link How do I convert a TIF to PNG in Java?" but i can't have the result as it proposed..
Therefore i raise this Question again to wish that can have better explanation and guideline from you all..
There were few issue i'm faced during go through with the solution that proposed:
1) According to the answer that proposed by Jonathan Feinberg, it need to install JAI and JAI/ImageIO.
However, after i installed both of them i still couldn't import the file in Netbean 7.2. NetBean 7.2 remain propose import default imageIO library.
2) when i'm using default ImageIO library Read method, it will return NULL value and i cannot continue to proceed.
3) I also tried others method such as convert TIFF file to BIN File by using BufferedOutputStream method but the result file is greater than 11 MB which is too large to load and end up loading failed.
if (this.selectedDO != null) {
String tempDO = this.selectedDO.DONo;
String inPath = "J:\\" + tempDO + ".TIF";
String otPath = "J:\\" + tempDO + ".bin";
File opFile = new File(otPath);
File inFile = new File(inPath);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hence, hope that can get help and advise from you all so that i can convert TIFF format to other format such as JPEG/PNG.
Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.
SeekableStream s = new FileSeekableStream(inFile);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = dec.decodeAsRenderedImage(0);
FileOutputStream fos = new FileOutputStream(otPath);
JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
fos.flush();
fos.close();
otPath is the path that you would like to store your JPEG image.
For example: "C:/image/abc.JPG";
inFile is the input file which is the TIFF file
At least this method is workable to me. If there is any other better method, kindly share along with us.
EDIT: Just want to point out an editing in order to handle multipage tiff. Obviously you also have to handle the different names for the resulting images:
for (int page = 0; page < dec.getNumPages(); page++) {
RenderedImage op = dec.decodeAsRenderedImage(page );
...
}
Add dependency
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version> </dependency>
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1
Coding
final BufferedImage tif = ImageIO.read(new File("test.tif"));
ImageIO.write(tif, "png", new File("test.png"));
In case of many pages, working following:
add dependency:
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
use following Java8 code
public void convertTiffToPng(File file) {
try {
try (InputStream is = new FileInputStream(file)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
if (iterator == null || !iterator.hasNext()) {
throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
}
// We are just looking for the first reader compatible:
ImageReader reader = iterator.next();
reader.setInput(imageInputStream);
int numPage = reader.getNumImages(true);
// it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
String name = FilenameUtils.getBaseName(file.getAbsolutePath());
String parentFolder = file.getParentFile().getAbsolutePath();
IntStream.range(0, numPage).forEach(v -> {
try {
final BufferedImage tiff = reader.read(v);
ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
If your target is Android, you could try this great Java library on Github that provides many utilities for handling, opening, and writing .tiff files.
A simple example from that Git showng how to convert TIFF to JPEG:
TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
//Set to true if you want use java exception mechanism
options.throwExceptions = false;
//Available 128Mb for work
options.availableMemory = 128 * 1024 * 1024;
//Number of tiff directory to convert;
options.readTiffDirectory = 1;
//Convert to JPEG
TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener);
First, take a look to What is the best java image processing library/approach?. For your code you can use
javax.imageio.ImageIO.write(im, type, represFile);
like you can see in write an image to file example.

Program returning "could not get audio input stream from input file" for specific .WAV file

I have a program that involves the analysis and output of .wav files. It works perfectly fine with tracks from cds or the internet but I have generated a sequence of pure sine wave tones using matlab to analyse and it is giving me the error shown in the title. The matlab files run fine in iTunes so I'm not sure why my program is having trouble with it.
public static void signalToFile(File f) throws IOException, UnsupportedAudioFileException
{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(f);
int numBytes = inputStream.available();
byte[] buffer = new byte[numBytes];
inputStream.read(buffer, 0, numBytes);
String newFile = f.getName().replace(".wav", ".txt");
System.out.println("Beginning file write: " + newFile + " (soundUtilities)");
BufferedWriter fileOut = new BufferedWriter(new FileWriter(new File("src/examples/Media/" + newFile)));
System.out.println("Ending file write: " + newFile + " (soundUtilities)");
System.out.println(buffer.length);
ByteBuffer myBB = ByteBuffer.wrap(buffer);
myBB.order(ByteOrder.LITTLE_ENDIAN);
while(myBB.remaining() > 1)
{
short current = myBB.getShort();
fileOut.write(String.valueOf(current));
fileOut.newLine();
}
fileOut.flush();
fileOut.close();
inputStream.close();
}
The first line of this method is what is causing the error. This method involves the signal information for the file to a txt file. Any help would be greatly appreciated.
Below is the small section of matlab code I used to create the wav file:
x13 = sin(2*pi*220*t1); % A3 long sample
x13envelope = [1:-1/length(x13):1/length(x13)];
x13full = x13.*x13envelope;
totalSound = [x1full x2full x3full x4full x5full x6full x7full x8full x9full x10full x11full x12full x13full]; % combines the notes
wavwrite(totalSound, fs, 32, 'TestTune');
Each of the entries in the "totalSound" array represent a note
WAV files have a header. You don't seem to create that header. What you seem to create is a raw PCM file.
I exepriences this problem "The output is different in JBoss 4.2 and JBoss 7
JBoss 4.2:
Support WAVE = true
JBoss 7:
Support WAVE = false"
check also this https://community.jboss.org/message/729654

Categories