iT邦幫忙

2024 iThome 鐵人賽

DAY 27
0
Software Development

LSTM結合Yolo v8對於多隻斑馬魚行為分析系列 第 27

Day 27Lstm與Yolo多斑馬魚行為分析介面mock test

  • 分享至 

  • xImage
  •  

今天是第二十七天我們可以寫一個lstm結合yolo 分析多隻斑馬魚行為系統介面的mock test,前面我幾天的功能都完成最後是介面的呈現,以下是程式碼

import unittest
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import random

# 假設這裡有一個 YOLO 和 LSTM 的模擬模型
def mock_yolo_detect(image_path):
    # 模擬YOLO檢測的結果(簡單隨機生成)
    return [
        {"x": random.randint(50, 450), "y": random.randint(50, 450), "status": random.choice(["正常", "焦慮", "害怕", "壓抑"])}
        for _ in range(random.randint(1, 5))
    ]

def mock_lstm_predict(behavior_history):
    # 模擬LSTM行為預測(簡單隨機生成)
    return random.choice(["正常", "焦慮", "害怕", "壓抑"])

class TestZebrafishAnalysisInterface(unittest.TestCase):

    def setUp(self):
        # 初始化測試介面
        self.root = tk.Tk()
        self.root.title("YOLO 和 LSTM 多斑馬魚行為分析")
        self.main_frame = ttk.Frame(self.root, padding="10")
        self.main_frame.grid(row=0, column=0, sticky="nsew")
        self.image_label = ttk.Label(self.main_frame)
        self.image_label.grid(row=4, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
        
        # 初始化介面變數
        self.file_path_var = tk.StringVar()
        self.result_var = tk.StringVar()
        self.behavior_history = []

    def test_interface_components(self):
        # 測試介面的所有元件是否正常顯示
        ttk.Label(self.main_frame, text="Select Image/Video:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
        ttk.Entry(self.main_frame, textvariable=self.file_path_var, width=50).grid(row=0, column=1, padx=5, pady=5, sticky="ew")
        ttk.Button(self.main_frame, text="Browse", command=self.mock_select_file).grid(row=0, column=2, padx=5, pady=5, sticky="w")
        self.assertEqual(self.main_frame.grid_size(), (3, 5))

    def test_yolo_detection(self):
        # 測試YOLO檢測功能
        file_path = "mock_image_path.gif"
        self.file_path_var.set(file_path)
        yolo_result = mock_yolo_detect(file_path)
        
        # 模擬檢測結果顯示
        for fish in yolo_result:
            x, y, status = fish["x"], fish["y"], fish["status"]
            label = ttk.Label(self.main_frame, text=f"位置: ({x}, {y}) 狀態: {status}")
            label.grid(row=5, column=0, columnspan=3, padx=5, pady=5)
        
        self.assertGreaterEqual(len(yolo_result), 1)  # 至少應該檢測到一條魚

    def test_lstm_prediction(self):
        # 測試LSTM行為預測功能
        self.behavior_history = ["正常", "焦慮", "正常", "壓抑", "害怕"]  # 模擬歷史數據
        prediction = mock_lstm_predict(self.behavior_history)
        
        # 顯示預測結果
        ttk.Label(self.main_frame, text=f"未來行為預測: {prediction}").grid(row=6, column=0, columnspan=3, padx=5, pady=5)
        self.assertIn(prediction, ["正常", "焦慮", "害怕", "壓抑"])

    def test_integration(self):
        # 整合測試,模擬整個流程
        self.mock_select_file()
        self.test_yolo_detection()
        self.test_lstm_prediction()

    def mock_select_file(self):
        # 模擬文件選擇功能
        file_path = "mock_image_path.gif"
        self.file_path_var.set(file_path)
        self.result_var.set(f"選擇的文件: {file_path}")

    def tearDown(self):
        # 測試結束後關閉窗口
        self.root.destroy()

if __name__ == "__main__":
    unittest.main()

1. 引入模組

import unittest
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import random
  • unittest: Python內建的測試框架,用來編寫和運行測試。
  • tkinterttk: 用於創建GUI界面,tkinter是Python的標準GUI庫,而ttk提供了更現代化的控件。
  • random: 用來生成隨機數值,模擬YOLO和LSTM的檢測和預測結果。

2. YOLO與LSTM的模擬函數

def mock_yolo_detect(image_path):
    return [
        {"x": random.randint(50, 450), "y": random.randint(50, 450), "status": random.choice(["正常", "焦慮", "害怕", "壓抑"])}
        for _ in range(random.randint(1, 5))
    ]

def mock_lstm_predict(behavior_history):
    return random.choice(["正常", "焦慮", "害怕", "壓抑"])
  • mock_yolo_detect: 模擬YOLO檢測結果。它生成一個隨機數量(1到5)的斑馬魚,每條魚的位置(x, y)和狀態(status)都是隨機選擇的。
  • mock_lstm_predict: 根據斑馬魚的行為歷史模擬LSTM預測的行為狀態,隨機選擇"正常"、"焦慮"、"害怕"或"壓抑"。

3. 測試類定義

class TestZebrafishAnalysisInterface(unittest.TestCase):
  • TestZebrafishAnalysisInterface: 定義了一個測試類,繼承自unittest.TestCase,其中包含了多個測試方法。

4. 測試的初始化

    def setUp(self):
        self.root = tk.Tk()
        self.root.title("YOLO 和 LSTM 多斑馬魚行為分析")
        self.main_frame = ttk.Frame(self.root, padding="10")
        self.main_frame.grid(row=0, column=0, sticky="nsew")
        self.image_label = ttk.Label(self.main_frame)
        self.image_label.grid(row=4, column=0, columnspan=3, padx=5, pady=5, sticky="nsew")
        
        self.file_path_var = tk.StringVar()
        self.result_var = tk.StringVar()
        self.behavior_history = []
  • setUp: 在每個測試方法之前運行,用來初始化測試環境。
    • tk.Tk(): 創建主窗口。
    • ttk.Frame: 創建一個主框架,作為放置其他控件的容器。
    • self.image_label: 用來顯示圖像或影片的標籤。
    • self.file_path_var: 保存選擇的圖像或影片路徑。
    • self.result_var: 保存結果信息。
    • self.behavior_history: 保存斑馬魚行為的歷史數據。

5. 測試介面元件

    def test_interface_components(self):
        ttk.Label(self.main_frame, text="Select Image/Video:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
        ttk.Entry(self.main_frame, textvariable=self.file_path_var, width=50).grid(row=0, column=1, padx=5, pady=5, sticky="ew")
        ttk.Button(self.main_frame, text="Browse", command=self.mock_select_file).grid(row=0, column=2, padx=5, pady=5, sticky="w")
        self.assertEqual(self.main_frame.grid_size(), (3, 5))
  • test_interface_components: 測試介面中的控件是否正確顯示。
    • 創建了一個標籤、輸入框和瀏覽按鈕,並檢查框架中的佈局是否如預期的那樣(3列5行)。

6. 測試YOLO檢測功能

    def test_yolo_detection(self):
        file_path = "mock_image_path.gif"
        self.file_path_var.set(file_path)
        yolo_result = mock_yolo_detect(file_path)
        
        for fish in yolo_result:
            x, y, status = fish["x"], fish["y"], fish["status"]
            label = ttk.Label(self.main_frame, text=f"位置: ({x}, {y}) 狀態: {status}")
            label.grid(row=5, column=0, columnspan=3, padx=5, pady=5)
        
        self.assertGreaterEqual(len(yolo_result), 1)
  • test_yolo_detection: 測試YOLO檢測的模擬功能。
    • 模擬選擇一個圖片文件並運行YOLO檢測,然後將每條斑馬魚的位置和狀態顯示在界面上。
    • 檢查YOLO是否檢測到至少一條斑馬魚。

7. 測試LSTM預測功能

    def test_lstm_prediction(self):
        self.behavior_history = ["正常", "焦慮", "正常", "壓抑", "害怕"]
        prediction = mock_lstm_predict(self.behavior_history)
        
        ttk.Label(self.main_frame, text=f"未來行為預測: {prediction}").grid(row=6, column=0, columnspan=3, padx=5, pady=5)
        self.assertIn(prediction, ["正常", "焦慮", "害怕", "壓抑"])
  • test_lstm_prediction: 測試LSTM預測的模擬功能。
    • 使用模擬的斑馬魚行為歷史數據,運行LSTM預測並將結果顯示在界面上。
    • 檢查預測結果是否在預期的狀態列表中。

8. 整合測試

    def test_integration(self):
        self.mock_select_file()
        self.test_yolo_detection()
        self.test_lstm_prediction()
  • test_integration: 測試整個流程的整合,從文件選擇到YOLO檢測再到LSTM預測。
    • 依次調用之前測試的各個方法,檢查整體功能是否正常運行。

9. 模擬文件選擇

    def mock_select_file(self):
        file_path = "mock_image_path.gif"
        self.file_path_var.set(file_path)
        self.result_var.set(f"選擇的文件: {file_path}")
  • mock_select_file: 模擬文件選擇功能,用來設置選擇的文件路徑。

10. 測試的清理

    def tearDown(self):
        self.root.destroy()
  • tearDown: 在每個測試方法結束後運行,用來清理測試環境。
    • 關閉窗口,釋放資源。

11. 測試運行

if __name__ == "__main__":
    unittest.main()
  • unittest.main(): 運行所有在TestZebrafishAnalysisInterface類中定義的測試方法。

總結

這個程式碼構建了一個GUI測試框架,模擬YOLO和LSTM的分析過程,測試從介面組件、模型檢測到預測行為的各個步驟。每個測試方法都驗證了不同的系統功能,確保所有部分都能正常工作。


上一篇
day 26 lstm多隻斑馬魚模型分析
下一篇
day 28 Lstm結合yolo v8 多隻斑馬魚行為分析效能評估
系列文
LSTM結合Yolo v8對於多隻斑馬魚行為分析29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言