From 6a6ae135f4b54fc841ac646b57b3c4c2c2713f79 Mon Sep 17 00:00:00 2001 From: hcy <202152320217@stu.zzut.edu.cn> Date: Wed, 19 Jun 2024 06:53:13 +0000 Subject: [PATCH] update obj_tkinterapp.py. Signed-off-by: hcy <202152320217@stu.zzut.edu.cn> --- obj_tkinterapp.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/obj_tkinterapp.py b/obj_tkinterapp.py index 8b3a5e2..6ba7731 100644 --- a/obj_tkinterapp.py +++ b/obj_tkinterapp.py @@ -5,16 +5,14 @@ from PIL import Image, ImageTk # 用于将 OpenCV 处理的图像转换为 tkin from ultralytics import YOLO # 导入 YOLO 对象侦测模型 import numpy as np # 导入 numpy 作为 np,用于处理数组 import logging # 导入日志模块,方便查看程序运行状态 -import utils # 导入 utils.py 文件,用于初始化模型和处理图像 -import os # 导入 os 模块,用于获取文件路径 +import utils # 导入 utils.py 文件,用于初始化模型和处理图像 +import os # 导入 os 模块,用于获取文件路径 # 设置日志 logging.basicConfig( format='%(asctime)s:%(levelname)s:%(message)s', level=logging.INFO) # 创建应用类,用于创建界面 - - class Application: # 初始化应用类,设置界面窗口和标题,以及初始化相关参数、UI组件和事件处理函数 def __init__(self, window, window_title): @@ -68,6 +66,11 @@ class Application: top_frame, text="读取视频", command=self.open_video) self.video_button.grid(row=0, column=4) + # 增加一个按钮用于切换到安全帽检测模型 + self.helmet_button = tk.Button( + top_frame, text="安全帽检测", command=self.load_helmet_model) + self.helmet_button.grid(row=0, column=5) + logging.info("创建画布用于展示视频") self.canvas = tk.Canvas(window, width=800, height=600) self.canvas.grid(row=2, column=0) @@ -120,7 +123,7 @@ class Application: # 获取第二组单选按钮的当前值(模型大小) model_opt2 = self.model_var2.get() - # TODO: 获取选择的模型内容构建模型的名称 model_name + # 获取选择的模型内容构建模型的名称 model_name if model_opt1 == "定位": model_name = "yolov8"+model_opt2 elif model_opt1 == "分割": @@ -128,9 +131,10 @@ class Application: elif model_opt1 == "姿势": model_name = "yolov8"+model_opt2+"-pose" - # TODO: 根据 model_name 构建模型完整路径 - model_path = os.path.join("weights", model_name) - self.model =utils.init_model(model_path) # TODO: 利用utils函数获取模型 + # 根据 model_name 构建模型完整路径 + model_path = os.path.join('weights', model_name) + + self.model = utils.init_model(model_path) # 利用utils函数获取模型 # 用日志记录当前更改的模型名称 logging.info(f"更改模型为 {model_name}") @@ -186,9 +190,8 @@ class Application: new_width = 800 new_height = int(new_width * (height / width)) frame = cv2.resize(frame, (new_width, new_height)) - if self.detecting: # 如果检测标志为 True,则进行目标检测 - frame =utils.process_frame(self.model,frame,show_box=True,show_mask=True) # TODO: 仔细阅读本文件中的代码,利用 utils 中相关函数对视频帧进行处理 - + if self.detecting: + frame = utils.process_frame(self.model, frame, show_box=True, show_mask=True) # TODO: 仔细阅读本文件中的代码,利用 utils 中相关函数对视频帧进行处理 self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame)) self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) @@ -220,6 +223,12 @@ class Application: self.running = False self.window.after(self.delay, self.update) + # 加载安全帽检测模型 + def load_helmet_model(self): + logging.info("加载安全帽检测模型") + model_path = os.path.join('weights', 'yolov8n-helmet.pt') + self.model = utils.init_model(model_path) + logging.info(f"更改模型为 yolov8n-helmet") # 创建一个窗口并将其传递给 Application 对象 App = Application(tk.Tk(), "Tkinter and OpenCV") -- Gitee