菜品识别程序,增加向量输出到控制台的功能!
This commit is contained in:
@@ -66,7 +66,7 @@ class FoodClassifierApp:
|
||||
|
||||
# 定义图像预处理(与训练时相同)
|
||||
self.transform = transforms.Compose([
|
||||
transforms.Resize((32, 32)),
|
||||
transforms.Resize((32, 32),interpolation=transforms.InterpolationMode.BILINEAR),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
||||
])
|
||||
@@ -114,25 +114,26 @@ class FoodClassifierApp:
|
||||
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||
|
||||
if image is not None:
|
||||
# image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
||||
return image
|
||||
|
||||
# 方法2:如果方法1失败,尝试使用PIL
|
||||
from PIL import Image as PILImage
|
||||
pil_image = PILImage.open(file_path)
|
||||
|
||||
# 转换为RGB(如果是RGBA)
|
||||
if pil_image.mode == 'RGBA':
|
||||
pil_image = pil_image.convert('RGB')
|
||||
elif pil_image.mode == 'L': # 灰度图
|
||||
pil_image = pil_image.convert('RGB')
|
||||
|
||||
# 转换为numpy数组
|
||||
image_array = np.array(pil_image)
|
||||
|
||||
# PIL使用RGB,OpenCV使用BGR,需要转换
|
||||
image = cv2.cvtColor(image_array, cv2.COLOR_RGB2BGR)
|
||||
|
||||
return image
|
||||
# # 方法2:如果方法1失败,尝试使用PIL
|
||||
# from PIL import Image as PILImage
|
||||
# pil_image = PILImage.open(file_path)
|
||||
#
|
||||
# # 转换为RGB(如果是RGBA)
|
||||
# if pil_image.mode == 'RGBA':
|
||||
# pil_image = pil_image.convert('RGB')
|
||||
# elif pil_image.mode == 'L': # 灰度图
|
||||
# pil_image = pil_image.convert('RGB')
|
||||
#
|
||||
# # 转换为numpy数组
|
||||
# image_array = np.array(pil_image)
|
||||
#
|
||||
# # PIL使用RGB,OpenCV使用BGR,需要转换
|
||||
# image = cv2.cvtColor(image_array, cv2.COLOR_RGB2BGR)
|
||||
#
|
||||
# return image
|
||||
|
||||
except Exception as e:
|
||||
print(f"加载图片失败: {e}")
|
||||
@@ -549,12 +550,30 @@ class FoodClassifierApp:
|
||||
pil_image = Image.fromarray(image_rgb)
|
||||
|
||||
# 应用预处理
|
||||
resize_transform = transforms.Resize((32,32),interpolation=transforms.InterpolationMode.BICUBIC)
|
||||
resized_image = resize_transform(pil_image)
|
||||
if isinstance(resized_image, Image.Image):
|
||||
# 转换为tensor但不归一化
|
||||
to_tensor = transforms.ToTensor()
|
||||
resized_tensor = to_tensor(resized_image)
|
||||
print(f"缩放后tensor形状: {resized_tensor.shape}")
|
||||
|
||||
# 打印前5个像素值(每个通道)
|
||||
print("前5个像素值 (R, G, B):")
|
||||
for i in range(min(5, resized_tensor.shape[1])):
|
||||
r_val = resized_tensor[0, 0, i].item() * 255 # Red通道 (转换回0-255范围)
|
||||
g_val = resized_tensor[1, 0, i].item() * 255 # Green通道
|
||||
b_val = resized_tensor[2, 0, i].item() * 255 # Blue通道
|
||||
print(f" 像素[0,{i}]: R={r_val:.2f}, G={g_val:.2f}, B={b_val:.2f}")
|
||||
|
||||
|
||||
input_tensor = self.transform(pil_image).unsqueeze(0) # 添加batch维度
|
||||
input_tensor = input_tensor.to(self.device)
|
||||
|
||||
# 进行预测
|
||||
with torch.no_grad():
|
||||
outputs = self.model(input_tensor)
|
||||
print('outputs',outputs)
|
||||
probabilities = F.softmax(outputs, dim=1)
|
||||
confidence, predicted = torch.max(probabilities, 1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user