Working with OpenIMAJ I'd like to save feature lists for later use but I'm getting a java.util.NoSuchElementException: No line found exception (see below) while re-reading the feature file I just saved. I've checked that the text file exists though I'm not really sure whether the full contents is what is ought to be (it's very long).
Any ideas what's wrong?
Thanks in advance!
(My trial code is pasted below).
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at org.openimaj.image.feature.local.keypoints.Keypoint.readASCII(Keypoint.java:296)
at org.openimaj.feature.local.list.LocalFeatureListUtils.readASCII(LocalFeatureListUtils.java:170)
at org.openimaj.feature.local.list.LocalFeatureListUtils.readASCII(LocalFeatureListUtils.java:136)
at org.openimaj.feature.local.list.MemoryLocalFeatureList.read(MemoryLocalFeatureList.java:134)
...
My trial code looks like this:
Video<MBFImage> originalVideo = getVideo();
MBFImage frame = originalVideo.getCurrentFrame().clone();
DoGSIFTEngine engine = new DoGSIFTEngine();
LocalFeatureList<Keypoint> originalFeatureList = engine.findFeatures(frame.flatten());
try {
originalFeatureList.writeASCII(new PrintWriter(new File("featureList.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Saved feature list with "+originalFeatureList.size()+" keypoints.");
MemoryLocalFeatureList<Keypoint> loadedFeatureList = null;
try {
loadedFeatureList = MemoryLocalFeatureList.read(new File("featureList.txt"), Keypoint.class);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Loaded feature list with "+loadedFeatureList.size()+" keypoints.");
I think the problem is that you're not closing the PrintWriter used to save the features, and that it hasn't had a time to actually write the contents. However you shouldn't really use the LocalFeatureList.writeASCII method directly as it will not write the header information; rather use IOUtils.writeASCII. Replace:
originalFeatureList.writeASCII(new PrintWriter(new File("featureList.txt")));
with
IOUtils.writeASCII(new File("featureList.txt"), originalFeatureList);
and then it should work. This also deals with closing the file once it's written.
Related
I am using Android camera2 to create a custom camera. The cameraDevice.close() method is slow and it makes UI freeze for 1 sec. I put it in another thread and it seems to work just fine. I want to know if this will cause some serious problem and whether there is another way to achieve this. Here is my closeCamera method:
private void closeCamera() {
boolean release = false;
try {
mCameraOpenCloseLock.acquire();
release = true;
} catch (InterruptedException e) {
release = false;
}
try {
preparing = true;
if (mCaptureSession != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mCaptureSession.isReprocessable()
|| validCameraSession) {
mCaptureSession.close();
}
mCaptureSession = null;
validCameraSession = false;
}
} catch (IllegalStateException e) {
mCaptureSession = null;
} catch (Exception e) {
mCaptureSession = null;
}
try {
new Thread(new Runnable() {
#Override
public void run() {
if (mCameraDevice != null) {
if (openCamera) {
mCameraDevice.close();
mCameraDevice = null;
}
}
}
}).start();
} catch (IllegalStateException e) {
Log.e(TAG, "closeCamera: mCaptureSession - ", e);
} catch (Exception e) {
Log.e(TAG, "closeCamera: mCaptureSession - ", e);
}
if (release) {
if (mCameraOpenCloseLock != null) {
int lock = mCameraOpenCloseLock.availablePermits();
if (lock > 1) mCameraOpenCloseLock.release(lock - 1);
else if (lock == 0) mCameraOpenCloseLock.release();
}
}
}
I think it may cause crash when mCameraDevice has not been closed but user open camera again. But it is rare case, and I am thinking of putting another check before open camera again. I don't want my UI to freeze 1 sec for it to close, is there any other way I can achieve that except putting it in seperate thread?
As Alex Cohn mentions, the recommended practice is to do all camera-related work on a separate thread from the UI.
It also takes a long time to open the camera, or create a capture session, relatively speaking, so doing those operations not on the UI thread is also a good idea.
That said, as long as you're not losing track of your own app state (so that you don't try to use a camera device you've already closed by accident, for example), there's no reason you can't mix calls to the camera device or capture session from multiple threads. The classes themselves are thread-safe.
As far as I know, such freeze with cameraDevice.close() happens on some unfortunate devices, and sometimes is cured by performing a normal system upgrade.
But this is a little consolation if this happens to you, on your device. Actually, you are kind of lucky that you can prepare a fix for that. The end-users of your app will benefit from your misfortune.
Your code looks OK, if it delivers desired improvements for you. As I explained, it may be hard to reproduce this problem on another device.
I would rather put all closeCamera() logic on the same background thread. If you provided a Handler to openCamera(), as in the official example,
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
then I would suggest posting all closeCamera() sequence to this mBackgroundHandler.
Hello im trying to output an excel file generated from a template xls using jxls library but I dont find the way to do so.
My code look like this:
ServletContext contexto = request.getServletContext();
String path = contexto.getRealPath("/lib/xlsx/plantilla.xlsx");
Conexion conexion = new Conexion();
List<Conexion> conexiones = new ArrayList<Conexion>();
ResultSet rs = CargarConsultas.ejecutarConsulta("CONS_GET_TABLA_CONEXION");
try {
while(rs.next()) {
conexion.setIp(rs.getString("ori_ip"));
conexion.setMac(rs.getString("ori_mac"));
conexion.setUrl_destino(rs.getString("url_dest"));
conexion.setSolicitud(rs.getString("time_stamp_solicitud"));
conexion.setUser_id(rs.getString("user_id"));
conexion.setSubida(rs.getString("icoming"));
conexion.setBajada(rs.getString("outgoing"));
conexion.setAutorizacion(rs.getString("time_stamp_autorizacion"));
conexion.setRenovacion(rs.getString("time_stamp_renovacion"));
conexion.setFin_sesion(rs.getString("time_stamp_fin_sesion"));
conexion.setGw_adress(rs.getString("gw_address"));
conexion.setGw_port(rs.getString("gw_port"));
conexiones.add(conexion);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File salida = new File(path);
InputStream is = new FileInputStream(salida);
try (FileOutputStream os = new FileOutputStream("salida.xlsx")) {
Context context = new Context();
context.putVar("conexiones", conexiones);
JxlsHelper.getInstance().processTemplate(is, os, context);
} catch (Exception e) {
e.printStackTrace();
}
Please let me knoew if you know how to output it and no save it to server disk.The code works fine. Thanks alot
I have a problem handling the end of file state of the axWindows Media Player and then open another frame in Visual C++
Here is my function :
private: System::Void axWindowsMediaPlayer1_PlayStateChange(System::Object^ sender, AxWMPLib::_WMPOCXEvents_PlayStateChangeEvent^ e) {
if (e->newState == 8) {
this->Hide();
MainForm^ frame = gcnew MainForm();
frame->ShowDialog();
}
}
If I build the project, the media player render the movie, plays it and when it reaches the end of file it's called the Hide function but the frame didn't show up. I receive no error.
What should I do or what I'm doing wrong ?
In this kind of situation you should try to catch an exception, and read its message - it's very helpful, and helps spare some time in the future.
try
{
...
}
catch (System::Exception^ e)
{
System::Console::WriteLine(e->Message);
}
I made this code to empty some files that I regularly delete, such as temp files in Windows. Several friends may wish to use the same application and I am working on the best way to handle the file not found exception.
How can this best be handled for use by multiple users?
public void Deletefiles()
{
try
{
string[] DirectoryList = Directory.GetDirectories("C:\\Users\\user\\Desktop\\1");
string[] FileList = Directory.GetFiles("C:\\Users\\user\\Desktop\\1");
foreach (string x in DirectoryList)
{
Directory.Delete(x, true);
FoldersCounter++;
}
foreach (string y in FileList)
{
File.Delete(y);
FilesCounter++;
}
MessageBox.Show("Done...\nFiles deleted - " + FileList.Length + "\nDirectories deleted - " + DirectoryList.Length + "\n" + FilesCounter + "\n", "message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception z)
{
if (z.Message.Contains("NotFound"))
{
MessageBox.Show("File Not Found");
}
else
{
throw (z);
}
//throw new FileNotFoundException();
}
}
Modifying you code as little as possible, you could simply wrap your Delete calls in a try/catch:
foreach (string x in DirectoryList)
{
try {
Directory.Delete(x, true);
}
catch (DirectoryNotFoundException e)
{
// do something, or not...
}
FoldersCounter++;
}
foreach (string y in FileList)
{
try
{
File.Delete(y);
}
catch (FileNotFoundException e)
{
// do something, or not...
}
FilesCounter++;
}
Remove the top level try/catch and just let the foreach statements cycle through -- trying and catching any exceptions as they come.
You don't necessarily need to alert the user that the file wasn't found. It is there it is going to be deleted, so the fact that it isn't there doesn't really effect the outcome of the program.
This isn't the most resource friendly method, but it is a simple enough application to not cause an issue.
getting following exception while inserting 2nd time in database from my liferay portlet.
[JDBCExceptionReporter:76] Duplicate entry '0' for key 'PRIMARY'.(i think its because my primary key value not getting auto increment)
I think have done mistake while auto incrementing the primary key in my custom portlet .but i don't know where i have to make changes for that.
if anyone can guide me about to where make the changes to solve this auto increment issue?
this is the code from auto increment been set
try {
restVar = restaurantPersistence.create(counterLocalService
.increment(restaurant.class.toString()));
} catch (SystemException e) {
e.printStackTrace();
return restVar = null;
}
try {
resourceLocalService.addResources(0,restParam.getGroupId(), restParam.getUserId(),
restaurant.class.getName(),restParam.getPrimaryKey(), false,true,true);
} catch (PortalException e) {
e.printStackTrace();
return restVar = null;
} catch (SystemException e) {
e.printStackTrace();
return restVar = null;
}
Try this one..
long primaryKeyId = CounterLocalServiceUtil.increment(ClassName.class.getName());
XYZDetails XYZDetails = XYZDetailsLocalServiceUtil.createXYZDetails(primaryKeyId);
Add other details using XYZDetails Obj
e.g
XYZDetails.setName("Name");
Then Save the Details..
XYZDetailsLocalServiceUtil.addXYZDetails(XYZDetails);
Hope this may help you !!!