可视化界面

import tkinter as tk
from tkinter import filedialog, messagebox

from PIL import Image, ImageTk
import time

import cv2
import numpy as np
from yolo import YOLO, YOLO_ONNX
yolo = YOLO()
class YOLOv3CrackDetectionApp:
    def __init__(self, root):
        self.root = root
        self.root.title("YOLOV5 Crack Detection")  # 设置标题
        self.root.geometry("800x600")  # 设置窗口大小

        # 设置标题标签
        self.title_label = tk.Label(root, text="YOLOV3 Crack Detection", font=("Helvetica", 24))
        self.title_label.pack(pady=20)

        # 设置用于显示图片的画布
        self.canvas = tk.Canvas(root, width=640, height=480, bg="gray")
        self.canvas.pack(pady=20)

        # 添加一个框架来放置按钮
        button_frame = tk.Frame(root)
        button_frame.pack(side=tk.BOTTOM, pady=20)

        self.btn_open_camera = tk.Button(button_frame, text="Open Camera", width=20, command=self.open_camera)
        self.btn_open_camera.pack(side=tk.LEFT, padx=10)

        self.btn_select_video = tk.Button(button_frame, text="Select Video", width=20, command=self.select_video)
        self.btn_select_video.pack(side=tk.LEFT, padx=10)

        self.btn_select_image = tk.Button(button_frame, text="Select Image", width=20, command=self.select_image)
        self.btn_select_image.pack(side=tk.LEFT, padx=10)

        self.video_capture = None  # 初始化摄像头和视频捕获对象

    def open_camera(self):
        """打开摄像头并显示"""
        self.video_capture = cv2.VideoCapture(0)  # 打开默认摄像头
        if not self.video_capture.isOpened():
            messagebox.showerror("Error", "Cannot open camera!")
            return
        self.show_camera_frame()

    def show_camera_frame(self):
        """显示摄像头实时画面"""
        ret, frame = self.video_capture.read()
        if ret:
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            img = Image.fromarray(frame_rgb)
            img_tk = ImageTk.PhotoImage(img)
            self.canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
            self.canvas.image = img_tk
            self.canvas.after(10, self.show_camera_frame)

    def select_video(self):
        video_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4;*.avi;*.mov;*.mkv")])
        if video_path:
            self.play_video(video_path)

    def play_video(self, video_path):
        self.video_capture = cv2.VideoCapture(video_path)
        if not self.video_capture.isOpened():
            messagebox.showerror("Error", "Cannot open video file!")
            return
        self.show_video_frame()

    def show_video_frame(self):
        ret, frame = self.video_capture.read()
        if ret:
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            img = Image.fromarray(frame_rgb)
            img_tk = ImageTk.PhotoImage(img)
            self.canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
            self.canvas.image = img_tk
            self.canvas.after(10, self.show_video_frame)
        else:
            self.video_capture.release()

    def select_image(self):
        image_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.png;*.jpeg")])
        if image_path:
            self.display_image(image_path)

    def display_image(self, image_path):



        img = Image.open(image_path)
        r_image = yolo.detect_image(img, crop=False, count=False)
        img = img.resize((640, 480))  # 调整大小以适应画布
        img_tk = ImageTk.PhotoImage(img)
        self.canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
        self.canvas.image = img_tk


if __name__ == "__main__":
    root = tk.Tk()
    app = YOLOv3CrackDetectionApp(root)
    root.mainloop()

在这里插入图片描述
原本的可视化界面,我就是直接按Chatgpt写的,描述你所需要的功能,就给你写出来,基本没错。但是有些代码需要添加上去,比如这段代码是从yolov5里面复制粘贴,因为你要检测,所以要调用YOLO()
在这里插入图片描述
这句话也需要用yolov5里面复制到可视化界面,yolo: 这指的是一个YOLO对象检测模型的实例,可能是通过加载预训练的权重和配置文件来初始化的。

detect_image(img): 这是yolo对象的一个方法,用于在提供的图像img上执行对象检测。img参数应该是一个图像对象,可以是PIL图像、NumPy数组或任何其他被YOLO库支持的图像格式。

crop=False: 这个参数指定是否在检测过程中对图像进行裁剪。如果设置为True,YOLO可能会在检测前对图像进行裁剪,这有时可以提高检测的准确性,特别是在处理大尺寸图像时。默认值False意味着不对图像进行裁剪。

count=False: 这个参数指定是否返回检测到的对象数量。如果设置为True,方法将返回一个包含检测到的对象数量的值。默认值False意味着不返回对象计数,只返回检测结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值