How to remove the background of overlapping bounding boxes?

Viewed 386

I am using Yolov5 to detect a certain objects from a video and after detecting them i want to remove the background of that object and just have the object with a black background.Such as this one.Such as this one

I managed to do this on the detected frames however I also want to make the frames black when there is no detection. The problem is that when I try to make the whole image black it makes all images completly black. The code

                          if names[int(cls)]=="tennis racket":
        
                            plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
                            x1, y1, x2, y2= int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])
                            w1=abs(x1-x2)
                            h1=abs(y1-y2)
                            
                            
                            
                            
                            img1=im0[y1:y1+h1,x1:x1+w1]
                            
                            im0=np.ones_like(im0)*0
                            im0[y1:y1+h1,x1:x1+w1]=img1
                          else:
                            im0=im0*0


I think the problem is that in the else function when I try to make the whole image black it also makes the bounding box overlapping the object that i want to detect black thus removing the detected object.

0 Answers
Related