修改可视化客户端ImageTk为ctk,消除警告。

This commit is contained in:
zhanghuan
2025-09-22 14:09:19 +08:00
parent 8791b4f2c0
commit 2d5096a0b2
2 changed files with 14 additions and 11 deletions
+12 -10
View File
@@ -4,7 +4,7 @@ import cv2
import numpy as np import numpy as np
import customtkinter as ctk import customtkinter as ctk
from tkinter import filedialog, messagebox from tkinter import filedialog, messagebox
from PIL import Image, ImageTk from PIL import Image
import torch import torch
import torch.nn.functional as F import torch.nn.functional as F
from typing import List, Optional, Tuple 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 = self.resize_image_for_display(img_info['image'], 100, 100)
display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB) display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(display_image) 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 = ctk.CTkLabel(img_frame, image=ctk_image, text="")
img_label.image = tk_image # 保持引用 img_label.image = ctk_image # 保持引用
img_label.pack(side="left", padx=10, pady=10) img_label.pack(side="left", padx=10, pady=10)
img_label.bind("<Button-1>", lambda e, idx=i: self.preview_image(idx)) img_label.bind("<Button-1>", 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 = self.resize_image_for_display(img_info['image'], 750, 550)
display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB) display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(display_image) 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 = ctk.CTkLabel(preview_window, image=ctk_image, text="")
img_label.image = tk_image img_label.image = ctk_image
img_label.pack(expand=True, padx=20, pady=20) img_label.pack(expand=True, padx=20, pady=20)
def remove_image(self, index): 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 = self.resize_image_for_display(img_info['image'], 120, 120)
display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB) display_image = cv2.cvtColor(display_image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(display_image) 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 = ctk.CTkLabel(result_frame, image=ctk_image, text="")
img_label.image = tk_image img_label.image = ctk_image
img_label.pack(side="left", padx=10, pady=10) img_label.pack(side="left", padx=10, pady=10)
img_label.bind("<Button-1>", lambda e, idx=result['image_index']: self.preview_image(idx)) img_label.bind("<Button-1>", lambda e, idx=result['image_index']: self.preview_image(idx))
+2 -1
View File
@@ -30,7 +30,8 @@ class ResNet50EmbeddingNet(nn.Module):
# 加载预训练的ResNet50 (使用新的weights参数) # 加载预训练的ResNet50 (使用新的weights参数)
if pretrained: if pretrained:
self.backbone = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1) # V2效果会好一点
self.backbone = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2)
else: else:
self.backbone = models.resnet50(weights=None) self.backbone = models.resnet50(weights=None)