【问题标题】:How to avoid this deprecation warning from a logical statement in python?如何从 python 中的逻辑语句中避免这种弃用警告?
【发布时间】:2022-01-04 23:31:57
【问题描述】:

我正在使用 Basler 相机开发一个 4 的 GUI。在我的程序中,我面临两个导致 GUI 无法正常运行的问题。但是,主要目标是根据相机索引保存 4 张图像。我被告知使用多线程技术开发GUI,这些都是我遇到的问题。这些是代码:

def LiveThread(strIdx):
        CamIdx = int(strIdx)

        try:
            panel[CamIdx] = None
            image[CamIdx] = []
            # Start Grabbing
            camera[CamIdx].StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
            print("Cam",CamIdx,': Start Grabbing')
            
            iterator = 0

            while bLiveThraed[CamIdx]:
                grabResult = camera[CamIdx].RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)

                if grabResult.GrabSucceeded():
                    image[CamIdx] = converter[CamIdx].Convert(grabResult) # Access the openCV image data
                    image[CamIdx] = image[CamIdx].GetArray() # change them to an array for easy access

                    if(image[CamIdx] != []):
                        image[CamIdx] = cv2.cvtColor(image[CamIdx], cv2.COLOR_BGR2RGB)
                        image[CamIdx] = cv2.resize(image[CamIdx], (345,270))
                        image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))

                        if panel[CamIdx] is None:
                            panel[CamIdx] = tk.Label(image=image[CamIdx])
                            panel[CamIdx].image = image[CamIdx]
                            panel[CamIdx].pack(side="left")
                            panel[CamIdx].place(x=(345*CamIdx)+(20*CamIdx)+20, y=100)
                            
                            panel[CamIdx] = tk.Label(image=image[CamIdx])
                            panel[CamIdx].image = image[CamIdx]
                            panel[CamIdx].pack(side="bottom")
                            panel[CamIdx].place(x=(345*CamIdx)+(20*CamIdx)+20, y=400)
                            #cv2.imwrite('./trial/camera'+str(CamIdx)+str(iterator)+'.jpg', image[CamIdx])
                            #iterator +=1
                        else:
                            panel[CamIdx].configure(image=image[CamIdx])
                            panel[CamIdx].image = image[CamIdx]
                        
                else:
                    print("Error: ", grabResult.ErrorCode)
        
                grabResult.Release()

        except genicam.GenericException as e:
            # Error handling
            print("An exception occurred.", e.GetDescription())
  1. 不推荐使用的警告显示在下图中

  1. 执行cv2.imwrite('./trial/camera'+str(CamIdx)+str(iterator)+'.jpg', image[CamIdx]) 后保存与其相机索引对应的每张图像(如相机00、相机10、相机20、相机30)的问题。下图给出了产生的错误。

生成的 GUI 如下图所示。

【问题讨论】:

  • 请不要张贴代码图片或错误信息。它们很难阅读。发布实际文本。
  • 很抱歉给您带来不便,但是当您单击图像时,您可以轻松阅读,没有任何问题。顺便说一句,不再重复了。
  • 不要使用[] 作为哨兵。使用None。使用is None 进行测试,而不是相等比较。

标签: python numpy opencv tkinter basler


【解决方案1】:

在下面的代码中,最后一行将 opencv 图像中的 image[CamIdx] 分配为 ImageTk.PhotoImage cv2 无法写下。考虑在分配image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx])) 之前将image[CamIdx] 存储到另一个变量中。

if(image[CamIdx] != []):
                        image[CamIdx] = cv2.cvtColor(image[CamIdx], cv2.COLOR_BGR2RGB)
                        image[CamIdx] = cv2.resize(image[CamIdx], (345,270))
                        image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))

save_img = None
if(image[CamIdx] != []):
                        image[CamIdx] = cv2.cvtColor(image[CamIdx], cv2.COLOR_BGR2RGB)
                        image[CamIdx] = cv2.resize(image[CamIdx], (345,270))
                        save_img = image[CamIdx]
                        image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))
cv2.imwrite('img.jpg',save_img)

对于弃用警告,如果 OP 想要检查 None 图像,正如 Christoph Rackwitz 在 cmets 中指出的那样:don't use [] as a sentinel. use None. test with is None, not equality comparison.

【讨论】:

  • 很高兴你觉得它有帮助:)
  • @maclan,我可能需要一些时间才能弄清楚。顺便说一句,弃用警告怎么样,我该如何避免呢?你有什么主意吗?使用此代码后(image[CamIdx] != [])
  • 什么弃用警告?通常我们可以忽略它们。 image[CamIdx] 是图像还是列表?如果是图像,则image[CamIdx] != [] 将始终为 True
  • 257: Deprecation warning: elementwise comparison failed. This will raise an error in futureimage[CamIdx] != []上的这种警告
  • 感谢所有的试验,然而,所有的选项都不起作用,他们抛出一个错误,建议使用a.any() 0r a.all()。总而言之,所关心的问题都解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 2015-01-11
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多