Home > Mobile >  How to avoid this deprecation warning from a logical statement in python?
How to avoid this deprecation warning from a logical statement in python?

Time:11-27

I am developing a GUI of 4 using Basler cameras. Within my program, I am facing two problems that cause the GUI to fail to run properly. However, The main goal is to save 4 images based on the camera index. I was told to use the multi-threading technique to develop the GUI, and these are the problems I encountered. These are the codes:

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. The deprecated warning is displayed in the following image

error

  1. The problem of saving every image corresponding to its camera index like camera00, camera10, camera20, camera30 after executing the cv2.imwrite('./trial/camera' str(CamIdx) str(iterator) '.jpg', image[CamIdx]). The resulting error is given in the following image.

error term

The resulting GUI is as shown in the image attached below.

GUI

CodePudding user response:

In below code, the last line assign image[CamIdx] from opencv image to become ImageTk.PhotoImage which cv2 can not write down. Consider storing image[CamIdx] to another varible before assigning image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(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]))

To

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)
  • Related