Bharadwaj
Bharadwaj

Reputation: 745

Passing cython argument as reference with Unknown type

I am creating c++ wrapper to python code using Cython. The cdef function is as follows

import cv2

cdef public void Load_Cascades(object& face_cascade, object& eye_cascade):
  face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
  print("Cascades loaded!!")

the type of the arguments are unknown and so I have used object and & is to pass them as reference variables. The calling function is as follows

cdef public void Run():
  face_cascade = None;
  eye_cascade = None;
  Load_Cascades(face_cascade, eye_cascade)

With such a code, there is error from the cython compiler that the reference variables cannot be changed.

I also called the function by passing address

Load_Cascades(&face_cascade, &eye_cascade)

This does not work as well, Can you please let me know how can I achieve the passing of unknown type variables via reference in cython

Upvotes: 0

Views: 535

Answers (1)

Bharadwaj
Bharadwaj

Reputation: 745

Since the object makes the argument a default Python argument, the mutability of Python is taken into consideration. Thus I merged all the arguments into a list which is mutable by default, it worked!

So the code is now

import cv2
cdef public void Load_Cascades(cascade):
  cascade.append(cv2.CascadeClassifier('haarcascade_frontalface_default.xml'))
  cascade.append(cv2.CascadeClassifier('haarcascade_eye.xml'))
  print("Cascades loaded!!")

While calling the function, I just pass an empty list

cdef public void Run():
  cascade = []
  Load_Cascades(cascade)

Thank you David for the input

Upvotes: 1

Related Questions