Randomly crashing of livestream with overlays

Hi,
i have EXE which is doing overlays on a webcam livestream. Its perfectly working on my Laptop with internal and external webcam.

On the other pc the livestream is randomly crashing (without doing anything) after some minutes. I start the EXE with cmd/Windows command and there is no error message when its crashing. So i guess the problem is not the python code? Its maybe a deeper problem: opencv or driver? Do you have a solution to the problem? How i can check whats the error.

here is my working code

  def cap_img(self, cap):
        ret, img = cap.read()
        img = cv2.flip(img,1)#flip cam
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).copy()
        height, width, channel = img.shape
        step = channel * width        
        img = QImage(img.data, width, height, step, QImage.Format_RGB888).copy()

        if height != HEIGHT and width != WIDTH:
            img = img.scaled(WIDTH, HEIGHT)
        
        return img

    def snap_img(self, cap):
        ret, img = cap.read()
        img = cv2.flip(img,1)
        self.snap_cv = img.copy()        

        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        height, width, channel = img.shape
        step = channel * width        
            
        self.snap = QImage(img.data, width, height, step, QImage.Format_RGB888).copy()
        if height != HEIGHT and width != WIDTH:
            self.snap = self.snap.scaled(WIDTH, HEIGHT)  

           
    # Show Livestream
    def livestream_overlays(self):
        super().__init__()
        
        self.overlay_base()  
        
        # red lines as overlays
        if self.state == 1 or self.state == 2:           
            self.overlay_red()       


        elif self.state == 3:
            self.overlay_blue()
            

        # Green lines as overlays
        if self.lvalue:
            self.overlay_green()
  
        # Show livestream with overlays
        if self.spsn == True:                        
            img = self.img.scaled(int(w/2), int(h)) 
            self.img2 = self.img2.scaled(int(w/2), int(h))
            
            if self.state_cams == True:                  
                self.show_cam(self.img2,2) 
                self.show_cam(img,3) 
            else:                              
                self.show_cam(self.img2,3) 
                self.show_cam(img,2) 
                  
        else:
            img = self.img.scaled(w, h) 
            self.show_cam(img,1)    
            
 
    def insertShape(self, img1, lines, linewidth, color):        
        img = img1.copy()
        
        painter = QPainter()
        pixmap = QPixmap(img)
       
        painter.begin(img)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.drawPixmap(img.rect(), pixmap)
        painter.setPen(QPen(colors[color], linewidth))

        for line in lines:
            painter.drawLine(line[0],line[1],line[2],line[3])
        
        return img  
    
           

    def overlay_base(self):

        if self.pop != None:
            if self.pop.chns != None: 
                self.ns = self.pop.chns
                self.pop = None
        

        img = self.cap_img(self.cap).copy()  

Thank You Guys

There might be deeper problems, but there definitely is a shallow one: you try do lots of things to an image without checking that reading it was successful. That’s what your ret variable is supposed to be for…

1 Like

what do you think, the ret value is for, and why dont you check it ?

ok and how to check the ret within my code? i cant finde examples for this.
Sorry i have done for lots of copy & paste without fully understanding the ret.

THank You

look at the docs, do less copypasta
get rid of the exe, and run it as an ordinary python script. see if you get a proper exception / stacktrace then

Is this OK?

def cap_img(self, cap):
    ret, img = cap.read()

    if not ret
            print("Can't receive frame (stream end?). Exiting ...")
    break

    img = cv2.flip(img,1)#flip cam
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).copy()
    height, width, channel = img.shape
    step = channel * width        
    img = QImage(img.data, width, height, step, QImage.Format_RGB888).copy()

    if height != HEIGHT and width != WIDTH:
        img = img.scaled(WIDTH, HEIGHT)
    
    return img

well, the break needs one more indent
well, make it return None instead and give it one more indent
(you only can break out of loops, and there is no)

ok so i dont must use while (example) ?

while True
    ret, frame = cap.read()
    if ret==True:
    ...
    else:
        break

or use that. as you like.

i will test it later, but if there is still the problem and i get no error message, what else i can do? is there some kind of debugging or error filter possible (in python or windows at all)?

again, stop doing that .exe nonsense. it’ll hide errors.

1 Like

my company does not accept installation of python on the working PC. So just EXE is possible but i will ask them again. Thank You all

they prevent you from developing and debugging? they’re shooting their own foot.

haha yes but its our IT department is scared of the whole cyber secruity hacking thing…