diff --git a/classifier/embedding_food_classifier_app.py b/classifier/embedding_food_classifier_app.py index ff5e715..a0c2177 100644 --- a/classifier/embedding_food_classifier_app.py +++ b/classifier/embedding_food_classifier_app.py @@ -4,7 +4,7 @@ import cv2 import numpy as np import customtkinter as ctk from tkinter import filedialog, messagebox -from PIL import Image, ImageTk +from PIL import Image import torch import torch.nn.functional as F from typing import List, Optional, Tuple @@ -387,11 +387,11 @@ class EmbeddingFoodClassifierApp: display_image = self.resize_image_for_display(img_info['image'], 100, 100) display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB) pil_image = Image.fromarray(display_image) - tk_image = ImageTk.PhotoImage(pil_image) + ctk_image = ctk.CTkImage(light_image=pil_image, dark_image=pil_image, size=(100, 100)) # 图片标签(可点击预览) - img_label = ctk.CTkLabel(img_frame, image=tk_image, text="") - img_label.image = tk_image # 保持引用 + img_label = ctk.CTkLabel(img_frame, image=ctk_image, text="") + img_label.image = ctk_image # 保持引用 img_label.pack(side="left", padx=10, pady=10) img_label.bind("", lambda e, idx=i: self.preview_image(idx)) @@ -483,10 +483,12 @@ class EmbeddingFoodClassifierApp: display_image = self.resize_image_for_display(img_info['image'], 750, 550) display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB) pil_image = Image.fromarray(display_image) - tk_image = ImageTk.PhotoImage(pil_image) + # 获取实际图片尺寸 + img_width, img_height = pil_image.size + ctk_image = ctk.CTkImage(light_image=pil_image, dark_image=pil_image, size=(img_width, img_height)) - img_label = ctk.CTkLabel(preview_window, image=tk_image, text="") - img_label.image = tk_image + img_label = ctk.CTkLabel(preview_window, image=ctk_image, text="") + img_label.image = ctk_image img_label.pack(expand=True, padx=20, pady=20) def remove_image(self, index): @@ -691,11 +693,11 @@ class EmbeddingFoodClassifierApp: display_image = self.resize_image_for_display(img_info['image'], 120, 120) display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB) pil_image = Image.fromarray(display_image) - tk_image = ImageTk.PhotoImage(pil_image) + ctk_image = ctk.CTkImage(light_image=pil_image, dark_image=pil_image, size=(120, 120)) # 图片标签 - img_label = ctk.CTkLabel(result_frame, image=tk_image, text="") - img_label.image = tk_image + img_label = ctk.CTkLabel(result_frame, image=ctk_image, text="") + img_label.image = ctk_image img_label.pack(side="left", padx=10, pady=10) img_label.bind("", lambda e, idx=result['image_index']: self.preview_image(idx)) diff --git a/net/resnet_embedding.py b/net/resnet_embedding.py index 7d8c952..9cd2822 100644 --- a/net/resnet_embedding.py +++ b/net/resnet_embedding.py @@ -30,7 +30,8 @@ class ResNet50EmbeddingNet(nn.Module): # 加载预训练的ResNet50 (使用新的weights参数) if pretrained: - self.backbone = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1) + # V2效果会好一点 + self.backbone = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2) else: self.backbone = models.resnet50(weights=None)