Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get basic Vision function working #664

Open
jbham opened this issue Oct 22, 2024 · 2 comments
Open

Unable to get basic Vision function working #664

jbham opened this issue Oct 22, 2024 · 2 comments
Labels
A-framework Affects the framework crates and the translator for them question Further information is requested

Comments

@jbham
Copy link

jbham commented Oct 22, 2024

After banging my head for over a day and not getting anywhere, I am coming here to request for help.

I am trying to get a basic OCR running. However, it doesn't seems to work. Below is what I have tried already.

My code:

let image_data = std::fs::read(path).unwrap();

// Create Request Handler:
let arg1 = objc2_vision::VNImageRequestHandler::alloc();
let arg2 = NSData::with_bytes(&image_data);
let arg3 = NSDictionary::new();

let request_handler = unsafe { objc2_vision::VNImageRequestHandler::initWithData_options(arg1, &arg2, &arg3) };

// Create new request
let req_arg1 = objc2_vision::VNRecognizeTextRequest::alloc();
let a2 = unsafe { objc2_vision::VNRequest::new() };
let req_arg2 = unsafe { objc2_vision::VNRequest::completionHandler(&a2) };

let requests = unsafe { objc2_vision::VNRecognizeTextRequest::initWithCompletionHandler(req_arg1, req_arg2) };

let mut def = NSArray::new();
unsafe { request_handler.performRequests_error(&def) }.unwrap();
let observations = unsafe { requests.results() };

I know (I think) my issue is in the way I am preparing my req_arg2 argument since thats where most of the magic will be happening. But, I can't get it to work :(

I tried to create a block with closure in different ways but i didn't get anywhere.

If I can get some help then that would be really appreciated!

@madsmtm madsmtm added question Further information is requested A-framework Affects the framework crates and the translator for them labels Oct 25, 2024
@madsmtm
Copy link
Owner

madsmtm commented Oct 25, 2024

I haven't used the Vision framework myself, but my guess is that you need to create the actual handler block yourself using the block2 crate?

Something like (very untested):

let completion_handler = block2::RcBlock::new(|request: NonNull<VNRequest>, error: *mut NSError| unsafe {
    dbg!(request.as_ref());
    dbg!(error.as_ref());
});
let block_ptr = &completion_handler as &Block<_> as *const Block<_> as *mut Block<_>;

let request = unsafe {
    objc2_vision::VNRecognizeTextRequest::initWithCompletionHandler(
        objc2_vision::VNRecognizeTextRequest::alloc(),
        block_ptr,
    )
};

let mut def = <NSMutableArray<VNRequest>>::new();
def.push(&request);
unsafe { request_handler.performRequests_error(&def) }.unwrap();
let observations = unsafe { request.results() };

@madsmtm
Copy link
Owner

madsmtm commented Oct 25, 2024

(Part of this is made easier in the yet unreleased version of block2, which includes a .as_ptr() to make the conversion to *mut Block<_> easier).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants