File size: 865 Bytes
6d3c96e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27


import os
import json
from mmpretrain import ImageClassificationInferencer


path = './testimg/'
config = 'convnext-v2-tiny_32xb32_in1k-384px.py'
checkpoint = 'ConvNeXt_v2-v2_ep90.pth'


inferencer = ImageClassificationInferencer(model=config, pretrained=checkpoint, device='cuda')

result={}

for root, dirs, files in os.walk(path):
    for file in files:
        if file.lower().endswith(('.png', '.jpg','jpeg')):
            # print(os.path.join(root, file))
            inf_result = inferencer(os.path.join(root, file))[0]
            # print(result['pred_class'])
            print(result,os.path.join(root, file))
            result[os.path.join(root, file)]= [{'pred_class' : inf_result['pred_class']},{'pred_score' : inf_result['pred_score']}]

with open(path + "predict_result.json", "w") as file:
    json.dump(result, file, ensure_ascii=False,indent=2)