pip安装python包:
pip install yolov9pip
在https://github.com/WongKinYiu/yolov9/tree/main中下载好权重文件yolov9-c.pt。
运行下面代码:
import yolov9model = yolov9.load("yolov9-c.pt", device="cpu") # load pretrained or custom model
model.conf = 0.25 # NMS confidence threshold
model.iou = 0.45 # NMS IoU threshold
model.classes = None # (optional list) filter by class
results = model("zidane.jpg") # perform inference
predictions = results.pred[0] # parse results
boxes = predictions[:, :4] # x1, y1, x2, y2
scores = predictions[:, 4]
categories = predictions[:, 5]
results.show() # show detection bounding boxes on image
结果显示:
如果报错:
Showing images is not supported in this environment: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
> - img marked as output argument, but provided NumPy array marked as readonly
> - Expected Ptr<cv::UMat> for argument 'img'
> - argument for rectangle() given by name ('thickness') and position (4)
> - argument for rectangle() given by name ('thickness') and position (4)
说明opencv-python-headless版本不支持,运行下面pip命令则可以解决:
pip install opencv-python-headless==4.8.0.74