I am encountering an error when trying to resize an image:
cv2.error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
> - src is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'src'
Here is my code:
def remove_background(image): # Ensure image is in the correct format (BGR for OpenCV) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Resize image to expected size
resized_image = cv2.resize(image, (640, 640)) # Adjust size according to model's input size
# Perform inference to obtain masks and bounding boxes
outputs = model.signatures["default"](tf.constant(resized_image))
masks = outputs['detection_masks']
boxes = outputs['detection_boxes']
# Apply masks to remove background
processed_image = image.copy()
for i in range(len(masks)):
mask = masks[i]
box = boxes[i]
box = [int(b * 640) for b in box] # Scale bounding box coordinates
mask = np.array(mask * 255, dtype=np.uint8)
processed_image[box[0]:box[2], box[1]:box[3]] = cv2.bitwise_and(processed_image[box[0]:box[2], box[1]:box[3]], processed_image[box[0]:box[2], box[1]:box[3]], mask=mask)
# Convert image back to RGB format
processed_image = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
return processed_image
Can someone please help me? Is this because of numpy?
New contributor
Steven Epis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3