1

Error when using Yolov5 & Opencv, onnx(-215:Assertion failed)

I'm going to perform sleep recognition detection using Yolov5 & Opencv, onnx. It receives the video in real time through the webcam and outputs whether you close your eyes.

I converted the .pt file to .onnx, and there seems to be no problem reading the model.

These errors continue to occur in output = net.forward()

[error: (-215:Assertion failed) total(srcShape, srcRange.start, srcRange.end) == maskTotal in function 'cv::dnn::computeShapeByReshapeMask']

full code is as follows.

import cv2
import numpy as np

onnx_model_path = ('C:/Users/182947/Downloads/best.onnx')
net = cv2.dnn.readNetFromONNX(onnx_model_path)

if net.empty():
    print('Network load failed.')
    exit()

class_labels = ['R_eye_open', 'L_eye_open', 'R_eye_close', 'L_eye_close']

camera = cv2.VideoCapture(0)

while True:
    ret, frame = camera.read()
    if not ret:
        break

    frame = cv2.resize(frame, (320, 320))

    input_blob = cv2.dnn.blobFromImage(frame, 1.0, (320, 320))

    net.setInput(input_blob)
    output = net.forward()

    for detection in output[0, 0]:
        score = float(detection[2])
        class_id = int(detection[1])

        if score > 0.5:

            left = int(detection[3] * frame.shape[1])
            top = int(detection[4] * frame.shape[0])
            right = int(detection[5] * frame.shape[1])
            bottom = int(detection[6] * frame.shape[0])

            cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), thickness=2)

            class_label = class_labels[class_id]
            cv2.putText(frame, f'{class_label}: {score:.2f}', (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

    cv2.imshow("Frame", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

camera.release()
cv2.destroyAllWindows()

Why does the error occur, and how can I fix it?

CW K
  • 19
  • 1
  • Would you put entire error message? – Cloud Cho May 19 '23 at 05:48
  • 1
    Entire error message needed to clarify the issue. – Cloud Cho May 19 '23 at 05:48
  • Did you export the ONNX model with correct parameters? – simeonovich May 19 '23 at 10:33
  • 1
    OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\reshape_layer.cpp:108: error: (-215:Assertion failed) total(srcShape, srcRange.start, srcRange.end) == maskTotal in function 'cv::dnn::computeShapeByReshapeMask' File "C:\Jocoding\opencv\cvversion.py", line 25, in output = net.forward() cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\reshape_layer.cpp:108: error: (-215:Assertion failed) total(srcShape, srcRange.start, srcRange.end) == maskTotal in function 'cv::dnn::computeShapeByReshapeMask' – CW K May 25 '23 at 06:17
  • The dataset used face images taken directly for YOLO v5 custom data learning. After exporting the .pt file to a .onnx file, I run it, but I get an error. – CW K May 25 '23 at 06:17
  • !python export.py --weights /content/drive/MyDrive/monitor/yolov5/runs/train/exp22/weights/best.pt --include torchscript onnx --simplify I used this code to export. – CW K May 25 '23 at 06:19

0 Answers0