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?