Я взял модель, которую нашел здесь https://github.com/matterport/Mask_RCNN/blob/master/samples/balloon/balloon.py и изменил ее, чтобы она работала в режиме реального времени с камерой моего ноутбука:
import cv2
import subprocess
import sys
from mrcnn import model as modellib
import argparse
import os
import argparse
from mrcnn.config import Config
import skimage.draw
import skimage
import numpy as np
cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class BalloonConfig(Config):
"""Configuration for training on the toy dataset.
Derives from the base Config class and overrides some values.
"""
# Give the configuration a recognizable name
NAME = "balloon"
# We use a GPU with 12GB memory, which can fit two images.
# Adjust down if you use a smaller GPU.
IMAGES_PER_GPU = 2
# Number of classes (including background)
NUM_CLASSES = 1 + 1 # Background + balloon
# Number of training steps per epoch
STEPS_PER_EPOCH = 100
# Skip detections with < 90% confidence
DETECTION_MIN_CONFIDENCE = 0.9
class InferenceConfig(BalloonConfig):
# 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
def color_splash(image, mask):
"""Apply color splash effect.
image: RGB image [height, width, 3]
mask: instance segmentation mask [height, width, instance count]
Returns result image.
"""
# Make a grayscale copy of the image. The grayscale copy still
# has 3 RGB channels, though.
gray = skimage.color.gray2rgb(skimage.color.rgb2gray(image)) * 255
# Copy color pixels from the original color image where mask is set
if mask.shape[-1] > 0:
# We're treating all instances as one, so collapse the mask into one layer
mask = (np.sum(mask, -1, keepdims=True) >= 1)
#print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
#print(mask)
splash = np.where(mask, image, gray).astype(np.uint8)
else:
splash = gray.astype(np.uint8)
return splash
config = InferenceConfig()
config.display()
model = modellib.MaskRCNN(mode="inference", config=config,
model_dir='./')
model.load_weights('mask_rcnn_balloon.h5', by_name=True)
while True:
ret, frame = cap.read()
if frame is None:
break
# Detect objects
r = model.detect([frame], verbose=0)[0]
# Color splash
splash = color_splash(frame, r['masks'])
cv2.imshow('frame', frame)
cv2.imshow('detector',splash)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
Но проблема в том, что обработка занимает 10 секундодин кадр. Я обнаружил, что функция model.detect () является причиной, по которой это занимает так много времени ... Так что мой вопрос, есть ли возможность уменьшить время между кадрами ??