from typing import List
import os
os.eniron["KMP_DUPLICATE_LIB_OK"]="TRUE"
import cv2
import numpy as np
import torch
import torchvision
from torchvision import transforms
import matplotlib.pyplot as plt
import coco
import model as modellib
import visualize
%matplotlib inline
# Root directory of the project
ROOT_DIR = os.getcwd()
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_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)
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
class InferenceConfig(coco.CocoConfig):
# 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
config = InferenceConfig()
config.display()
# 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)
# 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', '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']
if __name__ == '__main__':
videos_path = R''
frames_path = R''
path = R''
vwidth = 320
vheight = 240
time_interval = 10
cap = cv2.VideoCapture(videos_path)
frame_index = 0
frame_count = 0
if cap.isOpened():
success = True
else :
success = False
print("读取失败!")
while(success):
sucess, frame = cap.read()
print(“--->正在读取第%d帧:” % frame_index, success)
if frame_index % time_interval == 0:
resize_frame = cv2.resize(frame, (vwidth, vheight), interpolation = cv2.INTER_AREA)
results = model.detect([resize_frame], verbose=1)
r = results[0]
visualize.display_top_masks(resize_frame, r['masks'], 1, class_names, frame_count)
frame_count += 1
frame_index +=1
cap.release()
#修改visualize.py中display_top_masks函数
def display_top_masks(image, mask, class_ids, class_name, frame_count, limit=4) :
......
......
......
display_images(to_display, titles=titles, cols=limit + 1, count=frame_count, cmap="Blues_r" )
#修改visualize.py中displayed_images函数
def display_images(images, titles=None, cols=4, count=None, camp=None, norm=None, interpolation=None):
titles = titles if titles is not None else [""] * len(images)
for image, title in zip(images, titles):
path2 = R''
if title == 'person' :
filename = path2 + "%d.jpg" % count
cv2.imwrite(filename, image.astype(np.uint8) * 255)
利用Mask-RCNN分割出步态数据
最新推荐文章于 2022-07-20 10:44:27 发布
本文详细介绍了如何运用深度学习框架Python中的Mask-RCNN模型,对步态数据进行精确的像素级分割,从而提高步态识别的准确性。通过实例解析了模型的训练过程和关键参数调整,帮助读者掌握这一技术在生物特征识别领域的应用。
摘要由CSDN通过智能技术生成