Python: 根据位置显示多张图片 -> 显示图片时程序暂停

Python: Display multiple images based on position -> Program paused when image is displayed

我是 python 的新手,有一个关于“人员跟踪”的项目要做。 我正在制作一个小原型,试图根据人们的位置显示图像。左上角,右上角,右下角,...

我尝试使用 Tkinter 并设法显示图像,并且我设法集成了一些代码来显示图像(这些是“if block.x ..”之后的 5 行)在我的人员检测代码

我现在的问题是,当显示图像时,程序暂停,什么也没有 运行s,我的代码应该显示人物的位置,但一旦显示图像就不再显示了。

有人知道如何解锁吗?

目标是根据人的位置改变图像。 (如果一个人在左边,例如显示眼睛向左看的图像)

如果有帮助,我正在使用 Gravity: Huskylens 相机,目前使用 Raspberry Pi 3

    import time
    import json
    import tkinter
    from huskylib import HuskyLensLibrary
    from tkinter import Label
    from tkinter import mainloop
    from tkinter import Tk
    from PIL import Image
    from PIL.ImageTk import PhotoImage
    
    
    
    # Initalize
    # hl = HuskyLensLibrary("I2C","", address = 0x32)
    hl = HuskyLensLibrary("SERIAL", "/dev/ttyUSB0", 3000000)
    
    
    # Change to face recognition algorithms
    hl.algorithm("ALGORITHM_FACE_RECOGNITION")
    
    # Run Screen
    screen = Tk()
    #w, h = screen.winfo_screenwidth(), screen.winfo_screenheight()
    screen.overrideredirect(True) # Real fullscreen, no taskbar
    screen.geometry('1024x10124') # Size of screen, image
    
    while True:
        blocks = hl.requestAll() # Blocks and Arrows
    
        for block in blocks:
            if block.type == "BLOCK": # If block is detected
                if block.x <= 106 and block.y <= 80:
                    image = "Eyes_Top_Left.jpg" # Image to display
                    with Image.open(image) as img:
                        photo = PhotoImage(img.resize((1024, 1024)))
                        Label(screen, image = photo).pack() # Display image
                    mainloop()
    
                    print("Top left !")
                    print('x : ', block.x)
                    print('y : ', block.y)
                    time.sleep(0.5)
                if (block.x > 106 and block.x <= 214) and block.y <= 80:
                    image = "Eyes_Top.jpg" # Image to display
                    with Image.open(image) as img:
                        photo = PhotoImage(img.resize((1024, 1024)))
                        Label(screen, image = photo).pack() # Display image
                    mainloop()
    
                    print("Top !")
                    print('x : ', block.x)
                    print('y : ', block.y)
                    time.sleep(0.5)
            else:
                print("No Block")
                time.sleep(0.5)

如果不清楚,我深表歉意,请随时询问详细信息

我尝试使用 .after (),它或多或少似乎有效,但问题是图像没有改变。当显示第一个时,它并没有消失,取而代之的是另一个,但程序继续 运行。

def function_to_call():
    print("-------------------------------")
    print("Inside function_to_call()")

#while True:
    blocks = hl.requestAll() # Request for Blocks and Arrows
    print("After hl.requestAll()")

    for block in blocks:
        print("Inside for loop")
    
        if block.type == "BLOCK": # Check if a block is detected
            print("After if block detected")
        
            # Top Right
            if block.x <= 106 and block.y <= 80:
                print("Top right position")
                image = "Eyes_Top_Right.jpg" # Image to display
                print("Image chosen")
                with Image.open(image) as img:
                    photo = PhotoImage(img.resize((1024, 1024)))
                    Label(screen, image = photo).pack() # Display image
                    print("Inside with")
                screen.after(500, function_to_call) # Wait 1 sec then go to function ...
                print("After .after()")
                mainloop()
                print("After .mainloop()")

            # Top
            if (block.x > 106 and block.x <= 214) and block.y <= 80:
                print("Top position")
                image = "Eyes_Top.jpg" # Image to display
                print("Image chosen")
                with Image.open(image) as img:
                    photo = PhotoImage(img.resize((1024, 1024)))
                    Label(screen, image = photo).pack() # Display image
                    print("Inside with")
                screen.after(500, function_to_call) # Wait 1 sec then go to function ...
                print("After .after()")
                mainloop()
                print("After .mainloop()")

print("Calling my function_to_call()")
function_to_call()
print("Function is called")

这是什么打印,程序似乎是 运行ning,但是它从不打印“After .mainloop ()”

我们可以清楚地看到检测到的两个位置,Top 和Top Right,但是显示的图像一直是Top Right。

Before function_to_call()
Calling my function_to_call()
-------------------------------
Inside function_to_call()
After hl.requestAll()
Inside for loop
After if block detected
Top right position
Image chosen
Inside with
After .after()
-------------------------------
Inside function_to_call()
After hl.requestAll()
Inside for loop
After if block detected
Top position
Image chosen
Inside with
After .after()

我不知道如何解决这个问题..

多亏 canvas,我终于想出了我想要的东西。现在根据人的位置显示正确的图像。

这是我用于原型的代码。

感谢您的帮助。

# -- Import --
import time
import json
import tkinter
from huskylib import HuskyLensLibrary
from tkinter import Label
from tkinter import mainloop
from tkinter import *
from tkinter import Tk
from PIL import Image
from PIL.ImageTk import PhotoImage

# -- Initalize --
# hl = HuskyLensLibrary("I2C","", address = 0x32)
hl = HuskyLensLibrary("SERIAL", "/dev/ttyUSB0", 3000000)

# Change to face recognition algorithms
hl.algorithm("ALGORITHM_FACE_RECOGNITION")

# Run Tk
screen = Tk()
screen.overrideredirect(True) # Real fullscreen, no taskbar
screen.geometry('500x400') # Size of screen for the image ("background")
canvas1 = Canvas(screen, width = 500, height = 400)
canvas1.pack()

# Eyes Top
Eyes_Top_Right = PhotoImage(file = 'Eyes_Top_Right.jpg')
Eyes_Top = PhotoImage(file = 'Eyes_Top.jpg')
Eyes_Top_Left = PhotoImage(file = 'Eyes_Top_Left.jpg')
# Eyes Middle
Eyes_Right = PhotoImage(file = 'Eyes_Right.jpg')
Eyes_Middle = PhotoImage(file = 'Eyes_Middle.jpg')
Eyes_Left = PhotoImage(file = 'Eyes_Left.jpg')
# Eyes Bottom
Eyes_Bottom_Right = PhotoImage(file = 'Eyes_Bottom_Right.jpg')
Eyes_Bottom = PhotoImage(file = 'Eyes_Bottom.jpg')
Eyes_Bottom_Left = PhotoImage(file = 'Eyes_Bottom_Left.jpg')

while True:
    blocks = hl.requestAll() # Camera request for Blocks and Arrows

    for block in blocks: # for loop
        if block.type == "BLOCK": # Check if a block is detected
        
            # Top Right
            if block.x <= 106 and block.y <= 80:
                eyesTopRight = canvas1.create_image(230, 150, anchor = NW, image = Eyes_Top_Right) # PosX, PosY, Anchor, Image
                screen.update_idletasks()
                screen.update()
                time.sleep(0.5)

            # Top
            if (block.x > 106 and block.x <= 214) and block.y <= 80:
                eyesTop = canvas1.create_image(230, 150, anchor = NW, image = Eyes_Top) # PosX, PosY, Anchor, Image
                screen.update_idletasks()
                screen.update()
                time.sleep(0.5)

mainloop()