ayoolaolafenwa / PixelLib

Visit PixelLib's official documentation https://pixellib.readthedocs.io/en/latest/
MIT License
1.04k stars 267 forks source link

Make segment overlay invisible #179

Open Niutonian opened 7 months ago

Niutonian commented 7 months ago

Hello, I love pixellib, I'm currently working on project that requires the background to be blacked out and the person to be in normal color, my problem is that right now no matter what I try, there's always the segmentation overlay on top of my selection, I have tried a few solution, but none work as well and as fast as the one below.

can anyone tell me how to make my overlay invisible

'''''import pixellib from pixellib.torchbackend.instance import instanceSegmentation import cv2 import numpy as np

capture = cv2.VideoCapture(0)

segment_video = instanceSegmentation() segment_video.load_model("pointrend_resnet50.pkl", detection_speed="fast") target_classes = segment_video.select_target_classes(person=True)

while capture.isOpened(): ret, frame = capture.read() if not ret: break

# Perform the segmentation
results = segment_video.segmentFrame(frame, show_bboxes=False, segment_target_classes=target_classes)

# The segmentation result is the first element in the results tuple
result = results[0]

# Extract the mask for the person class (assuming person class ID is 0)
masks = result['masks']
if masks.shape[2] > 0:
    person_mask = np.sum(masks, axis=2).astype(bool)

    # Use the original frame where the mask is True, and set the background to black
    frame = np.where(person_mask[:,:,None], frame, 1)

# Display the resulting frame
cv2.imshow('frame', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
    break

capture.release() cv2.destroyAllWindows()'''''

Thank you