1 Star 0 Fork 1

impecunious/object-detection-masked-rcnn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
run_remove_background.py 4.78 KB
一键复制 编辑 原始数据 按行查看 历史
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'))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/impecunious/object-detection-masked-rcnn.git
git@gitee.com:impecunious/object-detection-masked-rcnn.git
impecunious
object-detection-masked-rcnn
object-detection-masked-rcnn
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385