# coding: utf-8

# In[323]:

import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import cv2
import time
from mrcnn.config import Config
from datetime import datetime

# In[324]:

# Root directory of the project
ROOT_DIR = os.getcwd()

# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "coco/")) # To find local version
from samples.coco import coco

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs-test")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(MODEL_DIR ,"mask_rcnn_shapes_0140.h5")
#COCO_MODEL_PATH = os.path.join(MODEL_DIR ,"mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
  utils.download_trained_weights(COCO_MODEL_PATH)
  print("cuiwei***********************")

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images201902")

# In[325]:

class ShapesConfig(Config):
  """Configuration for training on the toy shapes dataset.
  Derives from the base Config class and overrides values specific
  to the toy shapes dataset.
  """
  # Give the configuration a recognizable name
  NAME = "shapes"

  # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
  # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
  GPU_COUNT = 1
  IMAGES_PER_GPU = 1

  # Number of classes (including background)
  NUM_CLASSES = 1 + 2 # background + 3 shapes

  # Use small images for faster training. Set the limits of the small side
  # the large side, and that determines the image shape.
  IMAGE_MIN_DIM = 80
  IMAGE_MAX_DIM = 512

  # Use smaller anchors because our image and objects are small
  RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6) # anchor side in pixels
  #RPN_ANCHOR_SCALES = (128 * 6, 256 * 6, 512 * 6)
  #RPN_ANCHOR_SCALES = (32 * 6, 64 * 6, 128 * 6, 256 * 6, 512 * 6)

  # Reduce training ROIs per image because the images are small and have
  # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
  TRAIN_ROIS_PER_IMAGE =100

  # Use a small epoch since the data is simple
  STEPS_PER_EPOCH = 100

  # use small validation steps since the epoch is small
  VALIDATION_STEPS = 50

# In[326]:

#import train_tongue
#class InferenceConfig(coco.CocoConfig):
class InferenceConfig(ShapesConfig):
  # Set batch size to 1 since we'll be running inference on
  # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
  GPU_COUNT = 1
  IMAGES_PER_GPU = 1

# In[327]:

config = InferenceConfig()

model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

# In[328]:

# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'human','ladder']
# Load a random image from the images folder
file_names = next(os.walk(IMAGE_DIR))[2]
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))

a=datetime.now()
# Run detection
results = model.detect([image], verbose=1)
b=datetime.now()
# Visualize results
print("time:",(b-a).seconds)
r = results[0]

visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
              class_names, r['scores'])

最新文章

  1. QT 删除文件指定目录
  2. The new day of my blog
  3. hdu-------(1848)Fibonacci again and again(sg函数版的尼姆博弈)
  4. ios 免书籍入门站点
  5. wpf DataGrid CheckBox列全选
  6. 单点登录CAS使用记(八):使用maven的overlay实现无侵入的改造CAS
  7. NET Core 中的依赖注入
  8. switch case多值匹配
  9. Python3基础 add() 向集合中加入新的元素
  10. .Net Core 连输入中文都变坑了...
  11. python 函数 装饰器 内置函数
  12. TIME_WAIT
  13. JavaScript 的基础学习(一)
  14. HDU6205
  15. CocoaPod 问题(I)
  16. matlab的三维绘图和四维绘图
  17. xlrd与xlwt的下载
  18. ext4.2常用的几种弹框
  19. Centos配置jdk和tomcat环境
  20. sencha touch tpl 实现按钮功能

热门文章

  1. open和close函数
  2. [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
  3. Spring Boot 入门之缓存和 NoSQL 篇(四)
  4. Windows10+CUDA8.0+VS2015+CUDNN5下配置caffe
  5. JVM内存管理之垃圾搜集器参数精解
  6. Django在Eclipse下配置启动端口号
  7. ROS的ovpn设置
  8. 启动 Eclipse 报错 “An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException”
  9. java软件设计模式——单例设计模式中的【饿汉式】与 【懒汉式】示例
  10. 【游记】noip2017酱油记