OpenCV 3.0 CascadeClassifier.load() Assertion Fail (!empty) - ios

I've just updated my iOS project to version OpenCV 3.0 and whenever I try and load the haarcascade file I get an Assertion Fail.
Previous version of OpenCV works fine and there is no change to how I get the path and load the file (see below), it's just seems not to work with version 3.0
NSString *faceCascadePath = [[NSBundle mainBundle] pathForResource:kFaceCascadeFilename ofType:#"xml"];
_faceCascade.load([faceCascadePath UTF8String])
I've also attempted to amend the way I read the file (another example I found below).
const CFIndex CASCADE_NAME_LEN = 2048;
char *CASCADE_NAME = (char *) malloc(CASCADE_NAME_LEN);
CFStringGetFileSystemRepresentation( (CFStringRef)faceCascadePath, CASCADE_NAME, CASCADE_NAME_LEN);
But again to no avail...
Any suggestions would be much appreciated.
C.

Okay figured it out, I was running the "detectMultiScale" in a seperate thread and trying to load the haarcascade file in the main ViewDidLoad.
Moved the load within the thread doing the actual detection and it seemed to fix it.
Still not sure why previous versions were not effected though.

Related

OpenCV 3.1 iOS can't load classifier from .xml

I'm trying to run this example on iOS:
https://github.com/egeorgiou/openCViOSFaceTrackingTutorial
Everything works except this function:
faceCascade.detectMultiScale(grayscaleFrame, faces, 1.1, 2, HaarOptions, cv::Size(60, 60));
I get an error like this:
error: (-215) !empty() in function detectMultiScale
I read a bit about it and everyone is saying that the problem is that the faceCascade is not being loaded.
At the moment I load it like this:
NSString* faceCascadePath = [[NSBundle mainBundle] pathForResource:#"haarcascade_frontalface_alt2" ofType:#"xml"];
self.faceCascade.load([faceCascadePath UTF8String]);
How should I load the .xml file so it works correctly? Or maybe should I put the haarcascade_frontalface_alt2.xml file somewhere else in XCode?
I'm using Xcode 8.

How to add/use kiwiviewer in ios app to view .ply file (3d model) or any other way

I am building an ios app on xcode that at some point i have to display a 3d model to the user , I have read and search until i found something called kiwiviewer , the 3d model is (.ply) files so i want a help how to add kiwiviewer to xcode or use it inside the app, or if there is other way to view the 3d models in iphone app .
I searched and watched a youtube video that have no sound and did not get the way , please help.
I am using ios 5.1 .
thank you.
kiwi viewer does not support the PLY files so the above answer is not right you can use a VTP files in order to view it in 3D via kiwi viewer so try to convert your file to VTP and use the same function mentioned above.
std::string dataset = [[[NSBundle mainBundle] pathForResource:#"skull" ofType:#"vtp"] UTF8String];
You can use this code in KiwiSimple (lite version of KiwiViewer) to load PLY files:
-(void) initializeKiwiApp
{
[EAGLContext setCurrentContext:self.context];
std::string dataset = [[[NSBundle mainBundle] pathForResource:#"skull" ofType:#"ply"] UTF8String];
self->mKiwiApp = vesKiwiViewerApp::Ptr(new vesKiwiViewerApp);
self->mKiwiApp->initGL();
[self resizeView];
self->mKiwiApp->loadDataset(dataset);
self->mKiwiApp->resetView();
}

iOS - UIImage imageNamed: returns null on device

[UIImage imageNamed:filename]
This method returns null only on the device.
I know it's known problem and it's usually due to the fact that simulator is case insensitive.
I have tried solutions proposed here: UIImage imageNamed returns nil
But nothing worked out for me.
The case is simple: I have 4 files named:Bar#2x~ipad.png, Bar#2x~iphone.png, Bar~ipad.png, Bar~iphone.png.
All of them are in project with target checkbox checked.
NSLog(#"%#",[UIImage imageNamed:#"Bar"]);
That line of code gives me null for device and I really have no idea what I'm doing wrong right now.
I did have such a problem recently too.
After playing with filenames and paths sometimes it helps when you clean and rebuild your project.
I found myself in the same situation recently.
The solution in my case was adding the extension to the file name.
[UIImage imageNamed:#"Bar.png"]
Completely clean your build and redo it:
delete the app from the device
option-clean the whole build directory in Xcode (⌘-Shift-K)
quit xcode
delete ~/Library/Developer/Xcode/DerivedData
restart xcode, build and run
This just happened to me, and to discover was very tough: I had one image which where nil just on device
logoWhite.png
my code:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"LogoWhite"] forBarMetrics:UIBarMetricsDefault];
After a while of debugging, I noticed that the name of the image is beginning with capital case letter. Obviously this doesn't matter on OSX with a ignore case file system. iOs file system isn't, so the image worked on the simulator but not on the device.
I bet that all the solutions about cleaning derived data and rebuild did randomly end with renaming the image, and this would do the trick as well. Just posting here for future reference :)
I encountered this issue and just fixed it. I listed the solution below as a reference.
I have several images, and use [UIImage imageNamed:filePath] to show them. All of images displayed well except 1 on simulator/device, but all of them can be seen on OS X. For that image, [UIImage imageNamed] always return null.
After few minutes' investigation, I found that the image cause problem is far big in file size: 4.1M. Others are all around 800kb. They have nearly same size in dimension.
Then I try to open it in image editor, then re-save it. After this, the size dropped to 800k. problem solved.
The reasons I guess,
[UIImage imageNamed:filePath] has a max file size limit? (low possibility, but need to check official document)
the image itself has some error. But OS X has better tolerance than iOS. so iOS cannot read and return null. This issue is like OS X can play more types of video files than iOS since it support more codecs.
so if you encounter this issue in the future, take a few seconds look at the file size. Hope help.
This is an odd problem, which I hadn't seen until this week.
Below are my findings, and a workaround to your problem.
In my iPhone app, I download an image and store it locally, and this had always worked fine.
But now when I run the same code, it was suddenly failing to create a UIImage out of it using the imageNamed function, and now it was returning nil.
Three notes though:
This exact code did work before, using the same source code and .png image files. I'm not sure if my copy of XCode 6.x or iOS 8.x quietly updated itself in the meantime.
The code continues to work okay (with the same image file) on the iPhone simulator. It just doesn't work on a real device.
Take a look at the code below. When UIImage:imageNamed failed, I ran some code to check if the file actually existed.. and it did. Then I loaded the binary data from the file using NSData:contentsAtPath (which also proves that the file exists and was in the right folder), then created a UIImage out of that, and it worked fine.
Huh ?!
UIImage* img = [UIImage imageNamed:backgroundImageFilename];
if (img != nil)
{
// The image loaded fine (this always worked before). Job done.
// We'll set our UIImageView "imgBackgroundView" to contain this image.
self.imgBackgroundView.image = img;
}
else
{
// We were unable to load the image file for some reason.
// Let's investigate why.
// First, I checked whether the image was actually on the device, and this returned TRUE...
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:backgroundImageFilename];
if (fileExists)
NSLog(#"Image file does exist.");
else
NSLog(#"Image file does not exist.");
// Next, I attempted to just load the bytes in the file, and amazingly, this also worked fine...
NSData *data = [[NSFileManager defaultManager] contentsAtPath:backgroundImageFilename];
if (data != nil)
{
// ..and then I COULD actually create a UIImage out of it.
img = [UIImage imageWithData:data];
if (img != nil)
{
// We have managed to load the .png file, and can now
// set our UIImageView "imgBackgroundView" to contain this image.
self.imgBackgroundView.image = img;
}
}
}
As I said, this code does provide a workaround for this problem, but it's very odd that it's suddenly started happening.
And, I should say, I did try the other suggestions in this thread, cleaning the project, removing the DerivedData, completely removing the app from the device, and so on, but they didn't make any difference.
I would be interested in knowing if anyone else hits this issue, and finds that my code sample works for them.
Update
I'm an idiot.
I'm not sure if the UIImage:imageNamed function has changed or something (and if so, why it continues to work okay on the iPhone 8.1 Simulator), but I found the following one line does work okay:
UIImage* img = [[UIImage alloc] initWithContentsOfFile:backgroundImageFilename];
So it seems as though you should use this function for loading images which aren't a part of your app's bundle.
I also have same issue then : XCode - Build Phases - Copy Bundle Resources -{see image is available or not}- add{image} - clean - delete app - Run .
I would like to add one more important point to all the above solutions
Make sure you add your image resource to right target
By mistake if Developer mess-up link with resource and target then conditions arise.
if you have multiple target then double check the resource are set to correct target.
Attached screenshot example in case of resource link between multiple targets.

OpenCV Line-Mod problems with Images

I'm trying to use line-mod (in special line-2d) in opencv 2.4 to compare images. At the moment I try to change the test-implementation linemod.cpp to use an input images instead of the camera, but without any success.
I tried to load an image via imread('...', CV_LOAD_IMAGE_COLOR); and pushed that in the sources vector but got a 'OpenCV Error: Assertion failed (response_map.rows % T == 0) in linearize' error.
If I load a CV_LOAD_IMAGE_GRAYSCALE image the run stops at detector->match with the error 'Thread 1: EXC_BAD_ACCESS (code=1, address=0x11310f000)'.
I don't understand what makes the difference between images coming from a VideoCapturer and from imread...
Is there anyone out there that may help me? I'm totally lost ... again ;-)
(For example sample code for matching two objects from images with linemod would be absolutely great!)
I use opencv 2.4 with xcode on a mac.
Maybe it is too late for an answer, but I am also interested in the algorithm
In the OpenCV Minutes 2012-06-26 ( http://code.opencv.org/projects/opencv/wiki/2012 ) you can read:
Will work with Stefan Hinterstoisser for final version of LINEMOD by September
So if you did not already solve it, you may want to wait.

NSString stringWithContentsOfFile failing with what seems to be the wrong error code

I'm trying to load a file into a string. Here is the code I'm using:
NSError *error = nil;
NSString *fullPath = [[NSBundle mainBundle] pathForResource:filename
ofType:#"html"];
NSString *text = [NSString stringWithContentsOfFile:fullPath
encoding:NSUTF8StringEncoding
error:&error];
When passed in #"about" as the filename, it works absolutely fine, showing the code works.
When passed in #"eula" as the filename, it fails with 'Cocoa error 258', which translates to NSFileReadInvalidFileNameError. However, if I swap the contents of the files over but keep the names the same, the other file fails proving there is nothing wrong with the filename, it's something to do with the content.
The about file is fairly simple HTML but the eula file is a massive mess exported from Word by the legal department.
Does anyone know of anything inside a HTML file that could cause this error to be raised?
Much thanks,
Sam
I've just spent 45 minutes with this problem, only in my case the solution was stupid and the problem slightly different.
I had a file called Playlist.txt in my resources directory. It was loading just fine.
The file was modified at one point, from within XCode.
The file stopped loading properly, with the same error as above. However, it had never been moved nor had its encoding type been changed.
I did a command-I (Get Info) on the file in the XCode directory, it told me it was UTF-8 (as expected).
I tried the "usedEncoding" way of reading files, no dice. Same error, encoding was return null.
Finally, I erased the file from XCode, dragged it in again, and did a Clean All. That fixed the problem.
This is not the first time that XCode magically caching things (incorrectly) has caused me hours and hours of wasted time. If you have an error like this which doesn't make sense, try removing and replacing files and cleaning all targets.
The error is almost certainly that your file is not in UTF-8, but you're right, that does sound like a bug in the error report.
Open the eula file up with BBEdit (or the free TextWrangler) and see what encoding it uses. Change the encoding to UTF-8 and save it. Diff the two files to see what differences have appeared. Replace the original file with the new one (fixing any glitches).
If that resolves the problem, then use the Apple Bug Reporter to report the bug in the error report.
I had the same error with you ,use file name with
[[NSBundle mainBundle] pathForResource:#"pageList" ofType:#"txt"]] good luck!
The most likely reason that +stringWithContentsOfFile:encoding:error: would fail in this case would be if you provided the wrong encoding. Are you sure that your #"eula" file is UTF8 encoded?
If you're unsure about the encoding of the file, you could always try +stringWithContentsOfFile:usedEncoding:error: instead and see if it works and what encoding it comes up with.
Don't know if this is your problem, but I just had a similar thing (stringWithContentsOfFile, no JSON), and the problem was that the file had CRLF (windows) line-endings and Western-whatever-it's-called encoding. I used SubEthaEdit to convert to LF and UTF-8, and everything works fine.
I ran into the same error.
When I played around a bit, it appeared that I was not including the file in copy bundle resources of the target.
I did that and it worked fine.
But, have to say -- error is quite misleading for such a simple cause. Just a bad guess from Xcode

Resources