IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 读取JSON文件,根据mask判断是否连通 -> 正文阅读

[人工智能]读取JSON文件,根据mask判断是否连通

自定义coco JSON文件 (instances_val2017.json)

1. 读取JSON文件

json_path = 'instances_val2017.json'
main = open(json_path)
main = json.load(main)

2. 根据mask查找

solov2源码(mmdet/datasets/pipelines/loading.py)进行了简单修改

def _poly2mask(mask_ann, img_h, img_w):
    if isinstance(mask_ann, list):
        # polygon -- a single object might consist of multiple parts
        # we merge all parts into one mask rle code
        rles = maskUtils.frPyObjects(mask_ann, img_h, img_w)
        rle = maskUtils.merge(rles)
    elif isinstance(mask_ann['counts'], list):
        # uncompressed RLE
        rle = maskUtils.frPyObjects(mask_ann, img_h, img_w)
    else:
        # rle
        rle = mask_ann
    mask = maskUtils.decode(rle)
    return mask
    
def check_mask(main):
    for i in range(len(main['annotations'])):
        h, w = main['annotations'][i]['height'], main['annotations'][i]['width']
        gt_masks = main['annotations'][i]['segmentation']
        gt_masks = _poly2mask(gt_masks, h, w)  
        image_np = gt_masks
        contours, hierarchy = cv2.findContours(image_np, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        print(len(contours))
        #可视化
        image_show = image_np * 255
        image_show = cv2.cvtColor(image_show, cv2.COLOR_GRAY2BGR)
        image_show = cv2.drawContours(image_show, contours, -1, (0, 0, 255), 3)
        cv2.namedWindow('1', 0)
        cv2.resizeWindow('1', 1024, 1300)
        cv2.imshow("1", image_show)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        print('i:', i)

3.所有需要调用的库

import json
import os
import cv2
import pycocotools.mask as maskUtils

4.整体代码

import json
import os
import cv2
import pycocotools.mask as maskUtils

def _poly2mask(mask_ann, img_h, img_w):
    if isinstance(mask_ann, list):
        # polygon -- a single object might consist of multiple parts
        # we merge all parts into one mask rle code
        rles = maskUtils.frPyObjects(mask_ann, img_h, img_w)
        rle = maskUtils.merge(rles)
    elif isinstance(mask_ann['counts'], list):
        # uncompressed RLE
        rle = maskUtils.frPyObjects(mask_ann, img_h, img_w)
    else:
        # rle
        rle = mask_ann
    mask = maskUtils.decode(rle)
    return mask


def cho_class(main):
    for i in range(len(main['annotations'])):
        h, w = main['annotations'][i]['height'], main['annotations'][i]['width']
        gt_masks = main['annotations'][i]['segmentation']
        gt_masks = _poly2mask(gt_masks, h, w)  # [_poly2mask(mask, h, w) for mask in gt_masks]
        image_np = gt_masks
        contours, hierarchy = cv2.findContours(image_np, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        print(len(contours))
        image_show = image_np * 255
        image_show = cv2.cvtColor(image_show, cv2.COLOR_GRAY2BGR)
        image_show = cv2.drawContours(image_show, contours, -1, (0, 0, 255), 3)

        cv2.namedWindow('demo', 0)
        cv2.resizeWindow('demo', 1024, 1300)
        cv2.imshow("demo", image_show)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        print('i:', i)


if __name__ == '__main__':
    json_path = 'instances_val2017.json'
    main = open(json_path)
    main = json.load(main)
    cho_class(main)
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-02-22 20:35:25  更:2022-02-22 20:36:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年6日历 -2024/6/2 16:39:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码