matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.69k stars 11.7k forks source link

A question about final output #1464

Open kuang7qi opened 5 years ago

kuang7qi commented 5 years ago

I want to know the location of objections( in the form of coordinate of the rectangular box), how cloud I get this output.

oferbentovim commented 5 years ago

Here is what i have this is in the splash function, it has some unneeded data and some doubles but it helps to find a lot of options for me `if image_path:

Run model detection and generate the color splash effect

    print("Running on {}".format(args.image))
    # Read image
    image = skimage.io.imread(args.image)
    # Detect objects
    r = model.detect([image], verbose=1)[0]
    print ("r['class_ids'] = ", r['class_ids']) # show classes sorted
    print ("r['scores'] = ", r['scores'])

    # Color splash
    splash = color_splash(image, r['masks'])

    ################################
    masked_image = create_masked_image(image, r['masks'], r['class_ids'])        

    # Get input and output to classifier and mask heads.
    mrcnn = model.run_graph([image], [
        ("proposals", model.keras_model.get_layer("ROI").output),
        ("probs", model.keras_model.get_layer("mrcnn_class").output),
        ("deltas", model.keras_model.get_layer("mrcnn_bbox").output),
        ("masks", model.keras_model.get_layer("mrcnn_mask").output),
        ("detections", model.keras_model.get_layer("mrcnn_detection").output),
        ])

    # Get detection class IDs. Trim zero padding.
    det_class_ids = mrcnn['detections'][0, :, 4].astype(np.int32)
    det_count = np.where(det_class_ids == 0)[0][0]
    det_class_ids = det_class_ids[:det_count]
    detections = mrcnn['detections'][0, :det_count]
    print("det_class_ids = ", det_class_ids)        
    #print("detections = ", detections)`

this will paint the masks on your image - mind you i have 4 categories and background `class_names = ["Background", "good", "ring", "yellow", "blist"] colors= [(0,0,0),(0,1,0),(0,0,0.5),(1,0,0),(1,1,1),(1,1,0)]

def create_masked_image(image, masks, class_ids):

global mask_sky
mask_sky_bool = False

# Generate random colors
N = masks.shape[-1]
#print("Number of instances: " + str(N))
#if (N == 0): 
#   return image.astype(np.uint8)

# Show area outside image boundaries.
height, width = image.shape[:2]
masked_image = image.astype(np.uint32).copy()

for i in range(N):
    #Label
    class_id = class_ids[i]
    label = class_names[class_id]
    #print(label)

    global colors
    color = colors[class_id]

    # Mask
    mask = masks[:, :, i]
    masked_image = apply_mask(masked_image, mask, color, 0.35)
    #cv2.imshow(str(label),masked_image.astype(np.uint8))

return masked_image.astype(np.uint8)`

to show my image (debug results) i use 'font = cv2.FONT_HERSHEY_SIMPLEX fontScale = 0.6 fontColor = (255,255,255) lineType = 2

    t = 0;
    for box in r['rois']: #image = visualize.draw_box(image, box, [128,128,0])            
        y = box[0]
        x = box[1]
        #print("x,y = ", x, ",",y)
        cv2.putText(masked_image,captions[t],(int(x+5),int(y+20)),font, fontScale, fontColor, lineType)
        cv2.rectangle(masked_image,(x,y),(box[3],box[2]),(0,255,0),3)
        cv2.putText(splash,captions[t],(int(x+5),int(y+20)),font, fontScale, fontColor, lineType)
        cv2.rectangle(splash,(x,y),(box[3],box[2]),(0,255,0),3)            
        t = t+1        

    ###########################
    # Save output
    file_name = "splash_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
    skimage.io.imsave(file_name, splash)

    file_name = "try{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
    skimage.io.imsave(file_name, masked_image)'