代码拉取完成,页面将自动刷新
import cv2
import numpy as np
import os
# os.environ["CUDA_VISIBLE_DEVICES"]="-1"
import sys
# from samples import coco
ROOT_DIR = os.getcwd()
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))
import coco
from mrcnn import utils
from mrcnn import model as modellib
import PIL.Image
import sys
# import coco
# Load the pre-trained model data
OUTPUT_DIR = os.path.join(ROOT_DIR, 'output')
INPUT_DIR = os.path.join(ROOT_DIR, 'demo_dataset')
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Change the config infermation
class InferenceConfig(coco.CocoConfig):
GPU_COUNT = 1
# Number of images to train with on each GPU. A 12GB GPU can typically
# handle 2 images of 1024x1024px.
# Adjust based on your GPU memory and image sizes. Use the highest
# number that your GPU can handle for best performance.
IMAGES_PER_GPU = 1
config = InferenceConfig()
# COCO dataset object names
model = modellib.MaskRCNN(
mode="inference", model_dir=MODEL_DIR, config=config
)
model.load_weights(COCO_MODEL_PATH, by_name=True)
class_names = [
'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush'
]
def apply_mask(image, mask):
image[:, :, 0] = np.where(
mask == 0,
125,
image[:, :, 0]
)
image[:, :, 1] = np.where(
mask == 0,
12,
image[:, :, 1]
)
image[:, :, 2] = np.where(
mask == 0,
15,
image[:, :, 2]
)
return image
# This function is used to show the object detection result in original image.
def display_instances(image, boxes, masks, ids, names, scores):
# max_area will save the largest object for all the detection results
max_area = 0
# n_instances saves the amount of all objects
n_instances = boxes.shape[0]
if not n_instances:
print('NO INSTANCES TO DISPLAY')
else:
assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]
for i in range(n_instances):
if not np.any(boxes[i]):
continue
# compute the square of each object
y1, x1, y2, x2 = boxes[i]
square = (y2 - y1) * (x2 - x1)
# use label to select person object from all the 80 classes in COCO dataset
label = names[ids[i]]
if label == 'person':
# save the largest object in the image as main character
# other people will be regarded as background
if square > max_area:
max_area = square
mask = masks[:, :, i]
else:
continue
else:
continue
# apply mask for the image
# by mistake you put apply_mask inside for loop or you can write continue in if also
image = apply_mask(image, mask)
return image
def transparent_back(image):
image = image.convert('RGBA')
L,H = image.size
color_0 = image.getpixel((0, 0))
# print("Color_0 : ", color_0)
for h in range(H):
for l in range(L):
dot = (l, h)
color_1 = image.getpixel(dot)
# print("Color_1 : ", color_1)
if color_1 == color_0:
color_1 = color_1[:-1] + (0,)
image.putpixel(dot, (255,255,255,0))
return image
if __name__ == "__main__":
# image = cv2.imread(sys.argv[1], -1)
image = cv2.imread(os.path.join(INPUT_DIR, 'pourab.jpg'))
height, width, channels = image.shape
results = model.detect([image], verbose=0)
r = results[0]
frame = display_instances(
image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']
)
temp = 'temp.png'
cv2.imwrite(os.path.join(OUTPUT_DIR, temp), image)
image = PIL.Image.open('./output/' + temp)
image = transparent_back(image)
os.remove(os.path.join(OUTPUT_DIR, temp))
image.save(os.path.join(OUTPUT_DIR, 'removed_background.png'))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。