亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法解決 RecursionError:調用 Python 對象時超出最大遞歸深度?

有沒有辦法解決 RecursionError:調用 Python 對象時超出最大遞歸深度?

蠱毒傳說 2022-06-07 17:09:29
我正在制作一個調用自身的函數,但出現錯誤:RecursionError:調用 Python 對象時超出了最大遞歸深度,有沒有辦法解決這個問題。更具體地說,我在第 35 行得到它,好的,frame = cap.read() 由于我的程序中的 .after 函數,我無法放置一個 while 循環。import cv2from tkinter import *import PILfrom PIL import Image, ImageTkroot = Tk()root.bind('<Escape>', lambda e: root.quit())lmain = Label(root)lmain.pack()print("[INFO] Making variables")ImageSource = 0window_name = "AutoCam"width = 600height = 800cap = cv2.VideoCapture(ImageSource)cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)print("[INFO] Made variables ")def ShowFrame(frame):    print("[INFO] making image.")    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)    img = PIL.Image.fromarray(cv2image)    imgtk = ImageTk.PhotoImage(image=img)    lmain.imgtk = imgtk    lmain.configure(image=imgtk)    print("[INFO] After 10 initializing")    lmain.after(10, CheckSource)    print("[INFO] Showed image")def CheckSource():    ok, frame = cap.read()    if ok:        print("[INFO] Ok is triggered")        if cv2.waitKey(1) & 0xFF == ord('q'):            cv2.destroyAllWindows()            cv2.waitKey(0)            print("[INFO] Exiting app after command")        ShowFrame(frame)    else:        lmain.after(10, CheckSource())CheckSource()root.mainloop()任何和所有的幫助將不勝感激。有人還可以解釋如何避免這種情況以備將來使用嗎?[編輯]the error message is:  Traceback (most recent call last):   File "C:/Users/Gotta/Documents/AutoCamPy.py", line 52, in <module>   CheckSource()File "C:/Users/Gotta/Documents/AutoCamPy.py", line 49, in CheckSource  lmain.after(10, CheckSource())File "C:/Users/Gotta/Documents/AutoCamPy.py", line 49, in CheckSource  lmain.after(10, CheckSource())File "C:/Users/Gotta/Documents/AutoCamPy.py", line 49, in CheckSource  lmain.after(10, CheckSource()) [Previous line repeated 995 more times]File "C:/Users/Gotta/Documents/AutoCamPy.py", line 35, in CheckSource  ok, frame = cap.read()RecursionError: maximum recursion depth exceeded while calling a Python object
查看完整描述

2 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

通過堆棧跟蹤,您可以確定遞歸發生在第 49 行(這是被執行多次的行)


File "C:/Users/Gotta/Documents/AutoCamPy.py", line 49, in CheckSource

  lmain.after(10, CheckSource())

File "C:/Users/Gotta/Documents/AutoCamPy.py", line 49, in CheckSource

  lmain.after(10, CheckSource())

File "C:/Users/Gotta/Documents/AutoCamPy.py", line 49, in CheckSource

  lmain.after(10, CheckSource())

 [Previous line repeated 995 more times]

它達到遞歸限制的原因是因為該.after函數(https://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method)需要一個回調函數作為第二個參數,但您正在傳遞而是調用的結果。CheckSource您應該傳遞CheckSource而不是CheckSource()作為第二個參數:


lmain.after(10, CheckSource)


查看完整回答
反對 回復 2022-06-07
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

要以簡單的方式處理此問題,您可以編寫 try/except 語句來捕獲此錯誤。在您確切知道錯誤是什么之前,您可以使用全部捕獲,但我建議您在知道具體錯誤后處理具體錯誤。


也就是說,我在您的代碼中更改了一些內容以對其進行一些清理并更嚴格地遵循 PEP8 標準。


你真的應該做import tkinter as tk而不是使用*. 這將有助于防止覆蓋已導入的方法。


接下來,您的 lambda 結束了,只需執行此操作即可root.quit。我們想保存對命令的引用而不是執行它,我們通過刪除括號來做到這一點。您的第二個后聲明也存在同樣的問題。


最后你導入PIL,然后專門從 PIL 導入。您不需要兩者都做。如果您只需要Image然后ImageTk就做from PIL import Image, ImageTk,如果您需要 PIL 的很多東西,那么您可以簡單地做import PIL并使用PIL.那里的前綴。


這是帶有 try/except 語句的清理代碼。如果您有任何問題,請告訴我。


import tkinter as tk

from PIL import Image, ImageTk

import cv2



root = tk.Tk()

root.bind('<Escape>', root.quit)

lmain = tk.Label(root)

lmain.pack()

ImageSource = 0

window_name = "AutoCam"

width = 600

height = 800

cap = cv2.VideoCapture(ImageSource)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)



def show_frame(frame):

    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)

    img = Image.fromarray(cv2image)

    imgtk = ImageTk.PhotoImage(image=img)

    lmain.imgtk = imgtk

    lmain.configure(image=imgtk)

    lmain.after(10, check_source)



def check_source():

    try:

        ok, frame = cap.read()

        if ok:

            if cv2.waitKey(1) & 0xFF == ord('q'):

                cv2.destroyAllWindows()

                cv2.waitKey(0)

            show_frame(frame)

        else:

            lmain.after(10, check_source)

    except:

        print('Connection failed for some reason!')



check_source()

root.mainloop()


查看完整回答
反對 回復 2022-06-07
  • 2 回答
  • 0 關注
  • 570 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號