mmpretrain两种预测方式

1.API预测

from mmpretrain import ImageClassificationInferencer

import os

import cv2

import numpy as np

import matplotlib.pyplot as plt

from PIL import Image, ImageDraw, ImageFont

inf=ImageClassificationInferencer(r'F:\openmmlab\mmpretrain-main\fruits\fruits_config.py',pretrained=r'F:\openmmlab\mmpretrain-main\fruits\epoch_50.pth')

img_path=r'C:\Users\17219\Desktop\xiangjiao.jpg'

res=inf(img_path,show=True)

print(res)

pred_label=res[0]['pred_label']

pred_score=res[0]['pred_score']

pred_class=res[0]['pred_class']

print(pred_label,pred_score,pred_class)

img2=cv2.imread(img_path)

# cv2.putText(img2, u'label:{} socre:{:.2f} class:{}'.format(pred_label,pred_score,pred_class), (100, 250), font, 1, (255, 255, 255), 2, cv2.LINE_AA,fontFile=font_file)

img = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))

draw = ImageDraw.Draw(img)

fontText = ImageFont.truetype("E:\anaconda\envs\OPENMMLAB\Lib\site-packages\matplotlib\mpl-data\fonts\ttf\msyh.ttc", 30, encoding="utf-8")

draw.text((50,50 ), 'label:{} socre:{:.2f} class:{}'.format(pred_label,pred_score,pred_class), (255,255,255), font=fontText)

img = np.asarray(img)

img =cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

new_path=img_path.split('/')[-1]

cv2.imwrite(f'outputs/{new_path}',img)

plt.imshow(img)

2. image_demo.py推理

python \
    demo/image_demo.py \
    inputs/apple.png \
    myconfig/resnet50_fruit30.py \
    --checkpoint work_dirs/best_accuracy_top1_epoch_8.pth \
    --show-dir outputs/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值