Removing whitespace from a Numpy Array

Asked

Viewed 58 times

1

I need to manipulate an image and identify the objects inside it. But the answer I get comes with white space and I’m not able to handle to remove these white spaces.

Code:

from imageai.Detection import ObjectDetection
import numpy as np
import os
import cv2

executing_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join (executing_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
custom_objects = detector.CustomObjects(bottle=True)
detections = detector.detectCustomObjectsFromImage(custom_objects=custom_objects,input_image=os.path.join(executing_path, "mercado1.jpg"), 
                                             output_image_path=os.path.join(executing_path, "mercado111.jpg"),
                                             minimum_percentage_probability=10, display_percentage_probability=True, display_object_name=False)

for eachObject in detections:    
    y = eachObject["box_points"]
    print(y)

Answer:

[ 990   33 1011   91]
[970  52 990  91]
[ 986   49 1006   92]
[1011   32 1040   91]

How I’d like it to stay:

[990 33 1011 91]
[970 52 990 91]
[986 49 1006 92]
[1011 32 1040 91]

I’m using Imageai to search for objects in the image.

1 answer

0


Use a function to remove unnecessary spaces.

def remove_unneeded_spaces(x):
    x = str(x)
    x = re.sub(" +", " ", x)  # Remove espaços repetidos.
    x = re.sub("\[ ", "[", x) # Remove espaços após o [.
    x = re.sub(" \]", "]", x) # Remove espaços antes do ].
    x = re.sub(" \[", "[", x) # Remove espaços antes do [.
    x = re.sub("\] ", "]", x) # Remove espaços após o ].
    return x

And then, instead of that:

print(y)

You use that:

print(remove_unneeded_spaces(y))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.