2025-12-13 16:13:12 +08:00
|
|
|
|
import tempfile
|
2025-12-17 09:10:51 +08:00
|
|
|
|
import time
|
2025-12-11 13:41:07 +08:00
|
|
|
|
|
2025-12-13 16:13:12 +08:00
|
|
|
|
import cv2
|
|
|
|
|
|
import torch
|
|
|
|
|
|
from ultralytics import YOLO
|
2025-12-11 13:41:07 +08:00
|
|
|
|
|
2025-12-17 09:10:51 +08:00
|
|
|
|
from log import logger
|
|
|
|
|
|
|
2025-12-11 13:41:07 +08:00
|
|
|
|
if __name__ == "__main__":
|
2025-12-13 16:13:12 +08:00
|
|
|
|
model_config = {
|
2025-12-18 09:13:18 +08:00
|
|
|
|
'model_path': r"F:\PyModelScope\Yolov\models\car1.pt",
|
|
|
|
|
|
'imgs': r"E:\DC\Yolov\smart_tiles\smart_tile_002_004_1325x1326.png"
|
2025-12-13 16:13:12 +08:00
|
|
|
|
}
|
2025-12-11 13:41:07 +08:00
|
|
|
|
|
2025-12-18 09:13:18 +08:00
|
|
|
|
# encryption_key = "O3^doTYEpyT%VCYdI6u#YKKi7YFWjGZQ"
|
2025-12-13 16:13:12 +08:00
|
|
|
|
local_path = model_config.get('model_path')
|
2025-12-18 09:13:18 +08:00
|
|
|
|
# from mandatory_model_crypto import MandatoryModelValidator
|
|
|
|
|
|
#
|
|
|
|
|
|
# validator = MandatoryModelValidator()
|
|
|
|
|
|
# decrypt_result = validator.decrypt_and_verify(local_path, encryption_key)
|
|
|
|
|
|
# decrypted_data = decrypt_result['decrypted_data']
|
|
|
|
|
|
# # 保存到临时文件并加载
|
|
|
|
|
|
# with tempfile.NamedTemporaryFile(delete=False, suffix='.pt') as tmp:
|
|
|
|
|
|
# tmp.write(decrypted_data)
|
|
|
|
|
|
# temp_path = tmp.name
|
2025-12-13 16:13:12 +08:00
|
|
|
|
|
|
|
|
|
|
# 加载YOLO模型
|
2025-12-18 09:13:18 +08:00
|
|
|
|
model = YOLO(local_path).to('cuda:0')
|
2025-12-17 09:10:51 +08:00
|
|
|
|
start = time.time()
|
|
|
|
|
|
|
2025-12-13 16:13:12 +08:00
|
|
|
|
with torch.no_grad():
|
|
|
|
|
|
results = model.predict(
|
|
|
|
|
|
source=model_config['imgs'],
|
|
|
|
|
|
stream=False,
|
|
|
|
|
|
verbose=False,
|
2025-12-17 09:10:51 +08:00
|
|
|
|
conf=0.2,
|
2025-12-13 16:13:12 +08:00
|
|
|
|
iou=0.45,
|
2025-12-18 09:13:18 +08:00
|
|
|
|
imgsz=1920,
|
2025-12-17 09:10:51 +08:00
|
|
|
|
)
|
|
|
|
|
|
logger.info(f'推理时间:{time.time() - start}')
|
2025-12-13 16:13:12 +08:00
|
|
|
|
# 提取检测结果
|
|
|
|
|
|
for result in results:
|
|
|
|
|
|
boxes = result.boxes.xyxy # 边界框坐标
|
|
|
|
|
|
scores = result.boxes.conf # 置信度分数
|
|
|
|
|
|
classes = result.boxes.cls # 类别索引
|
|
|
|
|
|
|
|
|
|
|
|
# 如果有类别名称,可以通过类别索引获取
|
|
|
|
|
|
class_names = [model.names[int(cls)] for cls in classes]
|
|
|
|
|
|
|
|
|
|
|
|
# 打印检测结果
|
|
|
|
|
|
for box, score, class_name in zip(boxes, scores, class_names):
|
|
|
|
|
|
print(f"Class: {class_name}, Score: {score:.2f}, Box: {box}")
|
|
|
|
|
|
# # 可视化检测结果,图像数据格式是BGR
|
|
|
|
|
|
annotated_img = result.plot(
|
|
|
|
|
|
line_width=1,
|
|
|
|
|
|
font_size=9
|
|
|
|
|
|
)
|
2025-12-17 09:10:51 +08:00
|
|
|
|
logger.info(f'startTime:{start},endTime:{time.time()},时间差:{time.time() - start}')
|
2025-12-13 16:13:12 +08:00
|
|
|
|
result.save(r'E:\DC\Yolov\uploads\result5.jpg')
|
|
|
|
|
|
# 显示图像
|
|
|
|
|
|
cv2.destroyAllWindows()
|