簡體   English   中英

如何根據單選按鈕選擇將圖像動態加載到 cv2

[英]How can I load an image into cv2 dynamically based on radiobutton selection

我正在使用字符的單選按鈕將字符選擇添加到隊列接受工具。 字符選擇基於使用 cv2 進行的 object 檢測,我需要這樣做,以便您 select 影響文件 cv2 打開的任何字符。

這就是我正在使用的:

def selection():
    
    if select:
        screenshot = wincap.get_screenshot()

        # Import selection to Vision Class
        test=0
        test=v.get()
        characterSearch = charSearch('./CharacterSelect/' + str(characters[test]+'.jpg'))
        print('./CharacterSelect/' + str(characters[test]+'.jpg'))
        characterMatch = characterSearch.find(screenshot, .63, 'rectangles')
        if len(characterMatch):
            charSelect_off()
    
    window.after(500, selection)

#Stores interger values
v = IntVar()
v.set = ()

# Radio button loops for character select
for index in range(len(characters)):
    Radiobutton(bottomframe,
        compound = TOP,
        image = portraits[index],
        text = characters[index],
        variable = v,
        value = index,
    ).grid(
        row = index//5,
        column = index%5,
    )

這個錯誤說它找不到路徑。 然而,打印能夠打印選擇。 因此,我需要找到一種使用 cv2.imread() 創建動態 str 的方法才能執行此操作。 或者完全是另一個選項,比如如果我可以從通過 tkinter 加載的圖像列表中讀取它,那也可以工作,因為我已經構建了一個列表,所以如果我可以索引它,那也會更優化。

編輯:添加收到的錯誤:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\lugex\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\lugex\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861, in callit
    func(*args)
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 182, in start
    win32gui.SetForegroundWindow(handle)
pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')
[ WARN:0@11.286] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('./CharacterSelect/Adriana.jpg'): can't open/read file: check file path/integrity
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\lugex\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\lugex\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861, in callit
    func(*args)
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 188, in start
    search_on()
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 154, in search_on
    Char_Search()
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 207, in Char_Search
    search_off()
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 160, in search_off
    charSelect_on()
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 165, in charSelect_on
    selection()
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\main.py", line 220, in selection
    characterSearch = charSearch('./CharacterSelect/' + str(characters[test]+'.jpg'))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\lugex\OneDrive\Documents\Projects\Queue-Companion\vision.py", line 206, in __init__
    self.accept_w = self.accept_img.shape[1]
                    ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'shape'

當我發現這個時,我能夠弄清楚: https://stackoverflow.com/a/48562343/20593304

使用我創建的字符串創建動態路徑。

    charVariable=0
    charVariable=v.get()
    pathtoCharacters = os.path.join(os.getcwd(), "CharacterSearch", str(characters[charVariable]+".jpg"))
    characterSearch = charSearch(pathtoCharacters)

然后對我的 class 執行 object 檢測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM