我使用yolov5进行对象检测。现在我正在尝试将其改进为YoloV7。我训练了我的数据集,并将其从.pt转换为.onx。但我无法将其实现到我的代码中。我在下面分享了代码。我得到了错误:
v2.error: OpenCV(4.5.5) /Users/runner/work/opencv-python/opencv-python/opencv/modules/dnn/src/onnx/onnx_importer.cpp:928: error: (-2:Unspecified error) in function 'handleNode'
> Node [NonMaxSuppression@ai.onnx]:(onnx::Gather_626) parse error: OpenCV(4.5.5) /Users/runner/work/opencv-python/opencv-python/opencv/modules/dnn/src/dnn.cpp:621: error: (-2:Unspecified error) Can't create layer "onnx::Gather_626" of type "NonMaxSuppression" in function 'getLayerInstance'
import cv2
import numpy as np
from PIL import Image
import webcolors
import time
import requests
start = time.time()
path = "/Users/admin/Desktop/ML/"
productsArray = []
products = []
classNames = []
allProductsArray = []
def format_yolov5(frame):
row, col, _ = frame.shape
_max = max(col, row)
result = np.zeros((_max, _max, 3), np.uint8)
result[0:row, 0:col] = frame
return result
# Loading image
image = cv2.imread(path+"Images/2.jpg")
img = format_yolov5(image) # making the image square
#######DETECTION###########
def Detect():
net = cv2.dnn.readNet(path+"Config/data.onnx")
# Detecting objects
blob = cv2.dnn.blobFromImage(img , 1/255.0, (640, 640), swapRB=True)
net.setInput(blob)
predictions = net.forward()
class_list = []
with open(path+"Config/obj.names", "r") as f:
class_list = [cname.strip() for cname in f.readlines()]
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
output_data = predictions[0]
image_width, image_height, _ = img.shape
x_factor = image_width / 640
y_factor = image_height / 640
for r in range(25200):
row = output_data[r]
confidence = row[4]
if confidence >= 0.55:
classes_scores = row[5:]
_, _, _, max_indx = cv2.minMaxLoc(classes_scores)
class_id = max_indx[1]
if (classes_scores[class_id] > .25):
confidences.append(confidence)
class_ids.append(class_id)
x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
left = int((x - 0.5 * w) * x_factor)
top = int((y - 0.5 * h) * y_factor)
width = int(w * x_factor)
height = int(h * y_factor)
box = np.array([left, top, width, height])
boxes.append(box)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.3, 0.4)
result_class_ids = []
result_confidences = []
result_boxes = []
for i in indexes:
result_confidences.append(confidences[i])
result_class_ids.append(class_ids[i])
result_boxes.append(boxes[i])
for i in range(len(result_class_ids)):
box = result_boxes[i]
class_id = result_class_ids[i]
label =(class_list[class_id])
allProductsArray.append(label)
cv2.rectangle(img, box, (0, 255, 255), 2)
cv2.rectangle(img, (box[0], box[1] - 20), (box[0] + box[2], box[1]), (0, 255, 255), -1)
cv2.putText(img, class_list[class_id], (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0,0,0))
cv2.putText(img, str(result_confidences[i]), (box[0]+60, box[1]), cv2.FONT_HERSHEY_SIMPLEX, .6, (0,0,0))
Detect()
print(allProductsArray)
不确定我是否完全理解你想做什么(运行Yolov7推理或Yolov5(,但我希望这个repo能有所帮助:
https://github.com/ibaiGorordo/ONNX-YOLOv7-Object-Detection