You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
962 B
38 lines
962 B
5 months ago
|
#coding:utf-8
|
||
|
from ultralytics import YOLO
|
||
|
import cv2
|
||
|
|
||
|
# 所需加载的模型目录
|
||
|
path = 'models/best.pt'
|
||
|
# 需要检测的图片地址
|
||
|
img_path = "TestFiles/aa.jpg"
|
||
|
|
||
|
# 加载预训练模型
|
||
|
model = YOLO(path, task='detect')
|
||
|
|
||
|
# 检测图片
|
||
|
results = model(img_path)
|
||
|
|
||
|
# 读取原始图片
|
||
|
img = cv2.imread(img_path)
|
||
|
|
||
|
# 遍历所有检测结果
|
||
|
for result in results:
|
||
|
# 获取检测框的坐标
|
||
|
for i, box in enumerate(result.boxes):
|
||
|
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
||
|
# 裁剪检测框内的图像
|
||
|
cropped_img = img[y1:y2, x1:x2]
|
||
|
# 显示裁剪后的图像
|
||
|
cv2.imshow("Cropped Image", cropped_img)
|
||
|
cv2.waitKey(0)
|
||
|
# 构建保存路径
|
||
|
save_path = f"TestFiles/cropped_license_plate_{i}.jpg"
|
||
|
# 保存裁剪后的图像
|
||
|
cv2.imwrite(save_path, cropped_img)
|
||
|
|
||
|
# 显示原始图像上的检测结果
|
||
|
res = results[0].plot()
|
||
|
cv2.imshow("YOLOv5 Detection", res)
|
||
|
cv2.waitKey(0)
|