简体   繁体   中英

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

I am making adding a character selection to a queue accept tool using radiobuttons for the characters. the character selection is based on object detection using cv2, and I need to make it so whatever character you select affects the file cv2 opens.

This is what I'm using:

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,
    )

This errors saying it can't find the path. the print however is able to print selection. So I need to find a way to create a dynamic str with cv2.imread() in order to do this. or another option entirely, like if I can read it from a list of images loaded via tkinter, that would work too as I have a list built, so if i could index that, that would also be more optimal.

Edit: Adding the error received:

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'

I was able to figure this out when I found this: https://stackoverflow.com/a/48562343/20593304

creating a dynamic path using the string I created.

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

which then implements to my class that performs the object detection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM