Repository: opencv
Issue: Unable to perform Inference with cv2.dnn_DetectionModel(yolo_model) when Cythonized
Labels: feature,category: python bindings,priority: low,category: 3rdparty

##### System information (version)

- OpenCV => `4.5.0-dev` (built from repo on Jetson AGX Xavier)
- Operating System / Platform => `linux aarch64`
- Compiler => `cmake`

##### Detailed description
If I use the `cv2.dnn.readNet()` followed by `model.detect()` in regular non-cythonized code, everything runs flawlessly.

```python
def infer():
    class_names = []
    with open("classes.txt", "r") as f:
        class_names = [cname.strip() for cname in f.readlines()]

    #vc = cv2.VideoCapture("demo.mp4")

    net = cv2.dnn.readNet("yolov4-tiny.weights", "yolov4-tiny.cfg")
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
    net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)

    model = cv2.dnn_DetectionModel(net)
    model.setInputParams(size=(416, 416), scale=1/255)


    base_image = cv2.imread("/home/nvidia/ai/cythonexperiment/dog.jpg")

    while cv2.waitKey(1) < 1:
        frame = base_image.copy()

        start = time.time()
        classes, scores, boxes = model.detect(frame, CONFIDENCE_THRESHOLD, NMS_THRESHOLD)
        end = time.time()

        start_drawing = time.time()
        for (classid, score, box) in zip(classes, scores, boxes):
            color = COLORS[int(classid) % len(COLORS)]
            label = "%s : %f" % (class_names[classid[0]], score)
            cv2.rectangle(frame, box, color, 2)
            cv2.putText(frame, label, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
        end_drawing = time.time()
        
        fps_label = "FPS: %.2f (excluding drawing time of %.2fms)" % (1 / (end - start), (end_drawing - start_drawing) * 1000)
        cv2.putText(frame, fps_label, (0, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow("detections", frame)

```
The model outputs for [classes, scores, boxes] are as follows:
```
output = 
[[16]
 [16]
 [16]
 [16]
 [16]
 [21]
 [21]] [[0.6479492 ]
 [0.6459961 ]
 [0.44702148]
 [0.41748047]
 [0.3046875 ]
 [0.60791016]
 [0.5644531 ]] [[ 613  485  365  500]
 [ 303  276  319  723]
 [ 966  540  269  453]
 [1374  569  371  439]
 [1201  553  248  441]
 [ 308  347  332  681]
 [  79  408  318  571]]
```
![dog_inference](https://user-images.githubusercontent.com/7716574/98994226-77ca3b00-24fd-11eb-8294-a3c0f108ee44.jpg)


**The same code when cythonized, **does not output any results**.** 
The model outputs for [classes, scores, boxes] are as follows:

```
output = 
((), (), ())
```

If cythonized, the code will load models without complaining, but the same image will show no inference results. 

I am not sure why `model.detect` does not work in a cythonized function.

##### Steps to reproduce
I have attached all files for you to use and trial with.

1. convert the inference portion of the script into a `def()` to then be cythonized. 
2. take the modified script, change extension to `.pyx` and then use `setup.py` to create `.c` and `.so` files. 
3. remove remaining yolo python files except the `.so` in the folder. 
4. create a `main.py` that imports the `yolo_cython.so` and then called  `yolo_cython.infer()`
5. When main.py called `yolo.infer()` called, you will see it loads models, but the output images have no output bboxes.

**Attached files as follows:**  [yolo_cython.zip](https://github.com/opencv/opencv/files/5532513/yolo_cython.zip)

1. `yolo_basic.py` - Regular script to perform inference
2. `yolo_cython.py` - modified script that puts all inference capabilities in a function to be called by external `main.py`
3. `yolo_cython.pyx` - yolo with a `.pyx` extension to be read by the cython compiler
3. `main.py` - imports yolo.py and called `infer()` to run inference with yolo model. 
4. `setup.py` - uses cython (`python3 setup.py build_ext --inplace`) to cythonize the `yolo.pyx` into `.c` and `.so` files
5. `dog.jpg`  - to use for inference
6. `yolo.cfg and yolo.weights`

```
//Convert yolo_cython.pyx
//Then cythonize
$ python3 setup.py build_ext --inplace
$ main.py 
```


##### Issue submission checklist

 - [ x ]  I report the issue, it's not a question
 - [ x ] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [ x ] I updated to latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [ x ] There is reproducer code and related data files: videos, images, onnx, etc
  
**Please let me know if this is something to do with my cython `setup.py` script that isn't compiling it correctly.**

Thank you in advance!