Former-commit-id: 0d5b15c8877419ac69f03d538f8d1bf9c4e063b9
This commit is contained in:
2025-05-26 19:19:30 +08:00
parent 85413a8594
commit 7176eb7605
7 changed files with 0 additions and 171 deletions

View File

@ -1,15 +1,11 @@
import torch
from PIL import Image
import torchvision.transforms as transforms
import cv2
import time
import os
from model import resnet34
import serial
from datetime import datetime
import numpy as np
import joblib
import json
import Find_COM
from threading import Thread
import atexit
@ -198,37 +194,6 @@ class MAT:
else:
return "colored"
def predictor(self, im_file): # 预测分类
image = Image.open(im_file)
data_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
img = data_transform(image)
img = torch.unsqueeze(img, dim=0)
with open(self.json_path, "r") as f:
class_indict = json.load(f)
model = resnet34(num_classes=self.classes).to(self.device)
assert os.path.exists(self.weights_path), "file: '{}' dose not exist.".format(self.weights_path)
model.load_state_dict(torch.load(self.weights_path, map_location=self.device))
model.eval()
with torch.no_grad():
output = torch.squeeze(model(img.to(self.device))).cpu()
predict = torch.softmax(output, dim=0)
predict_cla = torch.argmax(predict).numpy()
class_a = "{}".format(class_indict[str(predict_cla)])
prob_a = "{:.3}".format(predict[predict_cla].numpy())
prob_b = float(prob_a)
print('class_:',class_a)
print('prob_:',prob_b)
return class_a, prob_b
def __del__(self):
self.pump_ser.close()
self.cap.release()