-2

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Kimwaga Makono
  • 121
  • 1
  • 8
  • 1
    Please do not post images of code or error messages. They are hard to read. Post the actual text. – fmw42 Nov 27 '21 at 06:10
  • Sorry for any inconveniences that I might have caused, but when you click the image, you can easily read without any problems. By the way, It won't be repeated anymore. – Kimwaga Makono Nov 27 '21 at 06:43
  • don't use `[]` as a sentinel. use `None`. test with `is None`, not equality comparison. – Christoph Rackwitz Nov 27 '21 at 18:12
  • 1
    [Here's a reason why the images should still be replaced by text](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). – Gert Arnold Nov 28 '21 at 19:01

1 Answers1

-1

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)

For the deprecation warning, in case the OP want to check for None image, as Christoph Rackwitz pointed out in the comments: don't use [] as a sentinel. use None. test with is None, not equality comparison.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
manaclan
  • 816
  • 9
  • 20
  • glad you find it helpful :) – manaclan Nov 27 '21 at 06:46
  • @maclan, It could take me some time to figure it out. By the way how about the deprecation warning, how can I avoid it? do you have any idea? after using this code `(image[CamIdx] != [])` – Kimwaga Makono Nov 27 '21 at 06:50
  • what deprecation warning? Usually we can just ignore them. Is image[CamIdx] a image or a list? if it is a image then `image[CamIdx] != []` will always be True – manaclan Nov 27 '21 at 07:01
  • `257: Deprecation warning: elementwise comparison failed. This will raise an error in future` Such kind of a warning on `image[CamIdx] != []` – Kimwaga Makono Nov 27 '21 at 08:17
  • in case you want to make sure `image[CamIdx]` is not empty image, try: `image[CamIdx] != [0,0,0]` or `image[CamIdx] != None` – manaclan Nov 27 '21 at 08:19
  • 1
    Thanks for all the trials, however, all the options are not working, they throw an error that suggests using `a.any()` 0r `a.all()`. All in all the concerned problem was solved. – Kimwaga Makono Nov 27 '21 at 11:29
  • do not use `[]` as sentinel/placeholder value. just use `None` and test with `is None`. all the other advice on that part of the question is bad. – Christoph Rackwitz Nov 27 '21 at 18:14