MaskRCNN路标:TensorFlow版本用于抠图
生活随笔
收集整理的這篇文章主要介紹了
MaskRCNN路标:TensorFlow版本用于抠图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??????????? MaskRCNN用于檢測路標,作為更詳細的目標檢測,用以得到更精準的額路標位置,路標的幾何中心點,用于構建更為精準的拓撲地圖,減少構圖誤差。
??????????? 摳圖工具已經完成,把框摳出來,用0值表示背景。
python代碼:
def mainex():#initDir();# Root directory of the projectROOT_DIR = os.getcwd()# Directory to save logs and trained modelMODEL_DIR = os.path.join(ROOT_DIR, "logs")# Path to trained weights file# Download this file and place in the root of your# project (See README file for details)#COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")COCO_MODEL_PATH= "D:/Works/PyProj/MaskRCNN-tensor/data/model/mask_rcnn_coco.h5";# Directory of images to run detection on#IMAGE_DIR = os.path.join(ROOT_DIR, "images");IMAGE_DIR = "data/MedSeaTest/";config = InferenceConfig()config.display();# 3.# Create model object in inference mode.model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)# Load weights trained on MS-COCOmodel.load_weights(COCO_MODEL_PATH, by_name=True);# 4class_names= init_classname();IMAGE_DIR = "D:/DataSet/PicStyleTest/Medsea3/deskfilter/";proDir(model, class_names, IMAGE_DIR);處理目錄: def proDir( model,class_names,IMAGE_DIR ):# Load a random image from the images folderprint(IMAGE_DIR);extention =".jpg";filelist =traverseFolder( IMAGE_DIR , extention);#traverse( IMAGE_DIR , extention);#for file in filelist:print("Is processing: ");print(file);image = skimage.io.imread( file );# Run detectionresults = model.detect([image], verbose=1);# Visualize results#r = results[0];fileName = file;getAllLabelMask(fileName, image, results[0], class_names)
def getAllLabelMask(fileName,image, maskResult,class_names ):"""boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates.masks: [num_instances, height, width]class_ids: [num_instances]class_names: list of class names of the datasetscores: (optional) confidence scores for each boxfigsize: (optional) the size of the image."""boxes = maskResult['rois'];masks = maskResult['masks']; scores = maskResult['scores'];class_ids = maskResult['class_ids'];# Number of instancesN = boxes.shape[0];if not( N<1 or boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]):returnrow = image.shape[1];col = image.shape[0];for i in range(N):# Bounding boxif not np.any(boxes[i]): continue;y1, x1, y2, x2 = boxes[i];# Labelclass_id = class_ids[i];score = scores[i] if scores is not None else Nonelabel = class_names[class_id];# Maskmask = masks[:, :, i];masked_image = np.zeros((col, row, 3), dtype=np.uint8);masked_image = apply_maskX(masked_image, mask);#frontImage = np.zeros( (col, row), dtype=np.uint8 );frontImage = image.copy();for m in range(row):for n in range(col):if(masked_image[n, m, 0]<254):#frontImage[n, m] = 255;frontImage[n,m,0] =0;frontImage[n, m, 1] = 0;frontImage[n, m, 2] = 0;#roiMask = masked_image[y1:y2, x1:x2];roiImg = frontImage[y1:y2, x1:x2];roiImg = cv2.cvtColor(roiImg, cv2.COLOR_BGR2RGB);fileMask = fileName[0: len(fileName)-4];fileMask = fileMask +"."+ str(i)+"."+label+"."+"Mask.png";cv2.imwrite(fileMask, roiImg);
結果:
總結
以上是生活随笔為你收集整理的MaskRCNN路标:TensorFlow版本用于抠图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jquery各版本下载,附Jquery官
- 下一篇: 路由器安全技术-路由器 安全