在图像分割任务里面数据的标注主要是依靠labelme这个开源的软件来完成的,今天在处理labelme的标注数据的时候报错了,首先是第一条报错,如下所示:

原始的转化解析代码如下所示:

import os
import base64
import json
import os.path as osp
import numpy as np
import PIL.Image
from labelmes import utilsif __name__ == '__main__':jpgs_path   = "datasets/JPEGImages"pngs_path   = "datasets/SegmentationClass"classes     = ["_background_","aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]count = os.listdir("./datasets/before/") for i in range(0, len(count)):path = os.path.join("./datasets/before", count[i])if os.path.isfile(path) and path.endswith('json'):data = json.load(open(path))if data['imageData']:imageData = data['imageData']else:imagePath = os.path.join(os.path.dirname(path), data['imagePath'])with open(imagePath, 'rb') as f:imageData = f.read()imageData = base64.b64encode(imageData).decode('utf-8')img = utils.img_b64_to_arr(imageData)label_name_to_value = {'_background_': 0}for shape in data['shapes']:label_name = shape['label']if label_name in label_name_to_value:label_value = label_name_to_value[label_name]else:label_value = len(label_name_to_value)label_name_to_value[label_name] = label_valuelabel_values, label_names = [], []for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):label_values.append(lv)label_names.append(ln)assert label_values == list(range(len(label_values)))lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)print('lbl: ', lbl)print('lbl_shape: ', lbl.shape)PIL.Image.fromarray(img).save(osp.join(jpgs_path, count[i].split(".")[0]+'.jpg'))new = np.zeros([np.shape(img)[0],np.shape(img)[1]])for name in label_names:index_json = label_names.index(name)index_all = classes.index(name)print('index_all: ', index_all)new = new + index_all*(np.array(lbl) == index_json)print('new: ', new)utils.lblsave(osp.join(pngs_path, count[i].split(".")[0]+'.png'), new)print('Saved ' + count[i].split(".")[0] + '.jpg and ' + count[i].split(".")[0] + '.png')

最后定位错误是出现在导入部分:

也就是说:代码在导入部分就是触发率Python异常停止工作,查了很多网上的解决方案都是不管用的,每当这个时候我最后的办法就是去除该模块,将代码中使用到该模块的对应的方法进行替换或者实现即可。

接下来修改代码如下:

#!usr/bin/env python
#encoding:utf-8
from __future__ import division'''
__Author__:沂水寒城
功能: 图像分割数据集解析处理
'''import io
import os
import base64
import json
import uuid
import cv2
import os.path as osp
import numpy as np
import PIL.Image
from PIL import Image
import PIL.Image
import PIL.ImageDrawdef img_b64_to_arr(img_b64):'''base64转array'''imgdata=base64.b64decode(img_b64) image=io.BytesIO(imgdata)img=Image.open(image)res=np.asarray(img)return resdef shape_to_mask(img_shape, points, shape_type=None, line_width=10, point_size=5
):mask = np.zeros(img_shape[:2], dtype=np.uint8)mask = PIL.Image.fromarray(mask)draw = PIL.ImageDraw.Draw(mask)xy = [tuple(point) for point in points]if shape_type == "circle":assert len(xy) == 2, "Shape of shape_type=circle must have 2 points"(cx, cy), (px, py) = xyd = math.sqrt((cx - px) ** 2 + (cy - py) ** 2)draw.ellipse([cx - d, cy - d, cx + d, cy + d], outline=1, fill=1)elif shape_type == "rectangle":assert len(xy) == 2, "Shape of shape_type=rectangle must have 2 points"draw.rectangle(xy, outline=1, fill=1)elif shape_type == "line":assert len(xy) == 2, "Shape of shape_type=line must have 2 points"draw.line(xy=xy, fill=1, width=line_width)elif shape_type == "linestrip":draw.line(xy=xy, fill=1, width=line_width)elif shape_type == "point":assert len(xy) == 1, "Shape of shape_type=point must have 1 points"cx, cy = xy[0]r = point_sizedraw.ellipse([cx - r, cy - r, cx + r, cy + r], outline=1, fill=1)else:assert len(xy) > 2, "Polygon must have points more than 2"draw.polygon(xy=xy, outline=1, fill=1)mask = np.array(mask, dtype=bool)return maskdef shapes_to_label(img_shape, shapes, label_name_to_value):cls = np.zeros(img_shape[:2], dtype=np.int32)ins = np.zeros_like(cls)instances = []for shape in shapes:points = shape["points"]label = shape["label"]group_id = shape.get("group_id")if group_id is None:group_id = uuid.uuid1()shape_type = shape.get("shape_type", None)cls_name = labelinstance = (cls_name, group_id)if instance not in instances:instances.append(instance)ins_id = instances.index(instance) + 1cls_id = label_name_to_value[cls_name]mask = shape_to_mask(img_shape[:2], points, shape_type)cls[mask] = cls_idins[mask] = ins_idreturn cls, insdef lblsave(filename, lbl):import imgvizif osp.splitext(filename)[1] != ".png":filename += ".png"if lbl.min() >= -1 and lbl.max() < 255:lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode="P")colormap = imgviz.label_colormap()lbl_pil.putpalette(colormap.flatten())lbl_pil.save(filename)else:raise ValueError("[%s] Cannot save the pixel-wise class label as PNG. ""Please consider using the .npy format." % filename)if __name__ == '__main__':jpgs_path   = "datasets/JPEGImages"pngs_path   = "datasets/SegmentationClass"classes     = ["_background_","aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]count = os.listdir("./datasets/before/") for i in range(0, len(count)):path = os.path.join("./datasets/before", count[i])if os.path.isfile(path) and path.endswith('json'):data = json.load(open(path))if data['imageData']:imageData = data['imageData']else:imagePath = os.path.join(os.path.dirname(path), data['imagePath'])with open(imagePath, 'rb') as f:imageData = f.read()imageData = base64.b64encode(imageData).decode('utf-8')img = img_b64_to_arr(imageData)label_name_to_value = {'_background_': 0}for shape in data['shapes']:label_name = shape['label']if label_name in label_name_to_value:label_value = label_name_to_value[label_name]else:label_value = len(label_name_to_value)label_name_to_value[label_name] = label_valuelabel_values, label_names = [], []for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):label_values.append(lv)label_names.append(ln)assert label_values == list(range(len(label_values)))lbl = shapes_to_label(img.shape, data['shapes'], label_name_to_value)     PIL.Image.fromarray(img).save(osp.join(jpgs_path, count[i].split(".")[0]+'.jpg'))new = np.zeros([np.shape(img)[0],np.shape(img)[1]])print('new_shape: ', new.shape)print('label_names: ', label_names)for name in label_names:index_json = label_names.index(name)index_all = classes.index(name)print('index_all: ', index_all)new = new + index_all*(np.array(lbl) == index_json)print('new_shape: ', new.shape)lblsave(osp.join(pngs_path, count[i].split(".")[0]+'.png'), new)print('Saved ' + count[i].split(".")[0] + '.jpg and ' + count[i].split(".")[0] + '.png')

本以为能够正常运行,结果报错:

说是维度不对,这个我已经在前面把各个节点的数据的shape都打印出来了,的确new_shape的数据shape是不对的,因为这里的问题就导致后面fromarray方法报错了,接下来还是修改代码,但是没有很好的思路,在网上查了这个报错的帖子,很多回答都是版本的问题,我在想可能也是因为新版本对应方法的处理发生了变化,这里就回到一个比较老的版本来重新改写如下:

#!usr/bin/env python
# encoding:utf-8
from __future__ import division"""
__Author__:沂水寒城
功能: 图像分割数据集解析处理
"""import io
import os
import base64
import json
import uuid
import cv2
import os.path as osp
import numpy as np
import PIL.Image
from PIL import Image
import PIL.Image
import PIL.ImageDrawdef img_b64_to_arr(img_b64):"""base64转array"""imgdata = base64.b64decode(img_b64)image = io.BytesIO(imgdata)img = Image.open(image)res = np.asarray(img)return resdef shape_to_mask(img_shape, points, shape_type=None, line_width=10, point_size=5):mask = np.zeros(img_shape[:2], dtype=np.uint8)mask = PIL.Image.fromarray(mask)draw = PIL.ImageDraw.Draw(mask)xy = [tuple(point) for point in points]if shape_type == "circle":assert len(xy) == 2, "Shape of shape_type=circle must have 2 points"(cx, cy), (px, py) = xyd = math.sqrt((cx - px) ** 2 + (cy - py) ** 2)draw.ellipse([cx - d, cy - d, cx + d, cy + d], outline=1, fill=1)elif shape_type == "rectangle":assert len(xy) == 2, "Shape of shape_type=rectangle must have 2 points"draw.rectangle(xy, outline=1, fill=1)elif shape_type == "line":assert len(xy) == 2, "Shape of shape_type=line must have 2 points"draw.line(xy=xy, fill=1, width=line_width)elif shape_type == "linestrip":draw.line(xy=xy, fill=1, width=line_width)elif shape_type == "point":assert len(xy) == 1, "Shape of shape_type=point must have 1 points"cx, cy = xy[0]r = point_sizedraw.ellipse([cx - r, cy - r, cx + r, cy + r], outline=1, fill=1)else:assert len(xy) > 2, "Polygon must have points more than 2"draw.polygon(xy=xy, outline=1, fill=1)mask = np.array(mask, dtype=bool)return maskdef shapes_to_label(img_shape, shapes, label_name_to_value, type="class"):assert type in ["class", "instance"]cls = np.zeros(img_shape[:2], dtype=np.int32)if type == "instance":ins = np.zeros(img_shape[:2], dtype=np.int32)instance_names = ["_background_"]for shape in shapes:points = shape["points"]label = shape["label"]shape_type = shape.get("shape_type", None)if type == "class":cls_name = labelelif type == "instance":cls_name = label.split("-")[0]if label not in instance_names:instance_names.append(label)ins_id = instance_names.index(label)cls_id = label_name_to_value[cls_name]mask = shape_to_mask(img_shape[:2], points, shape_type)cls[mask] = cls_idif type == "instance":ins[mask] = ins_idif type == "instance":return cls, insreturn clsdef label_colormap(N=256):def bitget(byteval, idx):return (byteval & (1 << idx)) != 0cmap = np.zeros((N, 3))for i in range(0, N):id = ir, g, b = 0, 0, 0for j in range(0, 8):r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))id = id >> 3cmap[i, 0] = rcmap[i, 1] = gcmap[i, 2] = bcmap = cmap.astype(np.float32) / 255return cmapdef lblsave(filename, lbl):if osp.splitext(filename)[1] != ".png":filename += ".png"if lbl.min() >= -1 and lbl.max() < 255:lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode="P")colormap = label_colormap(255)lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())lbl_pil.save(filename)else:raise ValueError("[%s] Cannot save the pixel-wise class label as PNG. ""Please consider using the .npy format." % filename)if __name__ == "__main__":jpgs_path = "datasets/JPEGImages"pngs_path = "datasets/SegmentationClass"classes = ["_background_","aeroplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","diningtable","dog","horse","motorbike","person","pottedplant","sheep","sofa","train","tvmonitor",]count = os.listdir("./datasets/before/")for i in range(0, len(count)):path = os.path.join("./datasets/before", count[i])if os.path.isfile(path) and path.endswith("json"):data = json.load(open(path))if data["imageData"]:imageData = data["imageData"]else:imagePath = os.path.join(os.path.dirname(path), data["imagePath"])with open(imagePath, "rb") as f:imageData = f.read()imageData = base64.b64encode(imageData).decode("utf-8")img = img_b64_to_arr(imageData)label_name_to_value = {"_background_": 0}for shape in data["shapes"]:label_name = shape["label"]if label_name in label_name_to_value:label_value = label_name_to_value[label_name]else:label_value = len(label_name_to_value)label_name_to_value[label_name] = label_valuelabel_values, label_names = [], []for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):label_values.append(lv)label_names.append(ln)assert label_values == list(range(len(label_values)))lbl = shapes_to_label(img.shape, data["shapes"], label_name_to_value)PIL.Image.fromarray(img).save(osp.join(jpgs_path, count[i].split(".")[0] + ".jpg"))new = np.zeros([np.shape(img)[0], np.shape(img)[1]])print("new_shape: ", new.shape)print("label_names: ", label_names)for name in label_names:index_json = label_names.index(name)index_all = classes.index(name)print("index_all: ", index_all)new = new + index_all * (np.array(lbl) == index_json)print("new_shape: ", new.shape)lblsave(osp.join(pngs_path, count[i].split(".")[0] + ".png"), new)print("Saved "+ count[i].split(".")[0]+ ".jpg and "+ count[i].split(".")[0]+ ".png")

运行输出如下:

问题完美解决,记录备忘!

图像分割标注数据labelme解析时报错【Python已停止工作Windows正在检查该问题的解决方案...】【ValueError: Too many dimensions: 3 > 2】相关推荐

  1. 【已解决】导入tensorflow报错/python已停止工作/The kernel appears to have died

    目录 背景 问题1:in future version of numpy, it will be understaood as(type,(1,)) 原因 解决 问题2:python已停止工作/The ...

  2. python已停止工作

    python已停止工作 如果不报错,说明不是python部分报错了, 曾经发现的原因:动态库异常崩溃了,比如ffmpeg 解码后处理函数达到100多ms,造成解码收数据堵住了.

  3. pycharm打开报错python已经停止工作

    https://blog.csdn.net/Hym_eric/article/details/83089063 用下面的方法1 修改注册表0->1的方式问题解决 https://blog.csd ...

  4. notebook python 已停止工作_Python/Jupyter Notebook初学遇到的一些问题总结(20201108)...

    在开始之前先写下我的系统,window10家庭版,安装的是python3.8,没有安装anaconda 虽然大多数人都推荐安装anaconda,不过我实在是搞不明白anaconda,所以在这写一写我的 ...

  5. 航天金税开票软件3.0登录报错,已停止工作

    RT,航天金税开票软件3.0,输入账号密码,登录报错 软件重装后也是这个错误提示. 解决办法: 删除安装目录下的AQJRDZ.txt文件(或者把这个文件剪切到其他地方) 注:可能有多个AQJRDZ.t ...

  6. python已停止工作appcrash_Python已经停止在Windows中工作(APPCRASH),使用GDAL读取和更新shapefile...

    我有一个复杂的程序,由许多Python脚本组成,它使用GDAL打开Esri shapefile,执行各种计算,并更新shapefile属性表.在 对于一些shapefile来说,它工作得非常好.对于其 ...

  7. python已停止工作appcrash_如何解决APPCRASH问题

    microsoft visual c++ runtime library runtime error! program: c:\program files\internet explorer\iexp ...

  8. python已停止工作appcrash_win7 APPCRASH问题解决!

    真是废了老劲了..什么清理插件,各种运行msconfig/启动都试了 问题:**.exe已停止工作 问题事件名称: APPCRASH 应用程序名:compute_image_mean.exe 应用程序 ...

  9. 夜神模拟器打开后报错:nox_adb.exe已停止工作,Appium+Python脚本执行报错:500

    一.场景回溯: 夜神模拟器打开后报错:nox_adb.exe已停止工作,Appium+Python脚本执行报错:500. 二.问题解决: 1.cmd中关闭Appium服务: 2.进入夜神模拟器安装的b ...

  10. cefsharp已停止工作_Winform下CefSharp的引用、配置、实例与报错排除(源码)

    Winform下CefSharp的引用.配置.实例与报错排除(源码) Winform 下 CefSharp 的引用, 配置, 实例与报错排除 [TOC] 1, 关于 CefSharp 装一手, 比较简 ...

最新文章

  1. 输入过欠压保护电路原理图
  2. ubuntu16.04上安装graphy-easy
  3. C# 读取指定目录中的所有文件,并按规则生成SQL语句!
  4. 九度OJ 题目1011:最大连续子序列
  5. 微软MCITP系列课程(八)文件服务器及磁盘配额
  6. Linux编程 文件操作,linux高级编程(文件操作)
  7. python commands_python之commands模块
  8. 2018网易互娱笔试题-手势锁
  9. python演示,用经验分布逼近总体精确分布
  10. STM32多通道DMA—ADC采样
  11. 上海商报:超级表格创始人叫板Excel
  12. hyperledger fabric 2.3.3 搭建教程
  13. 光量子计算机的信息载体,如何使“孤傲”的光子改变彼此的量子态?
  14. Jupyter Notebook 自动补全、智能提示
  15. SF25 | 日内交易策略开发(一)黄金日内交易模型
  16. 第六周工作周报销售_知新周报|第六周
  17. 七海的java学习笔记(八)
  18. 制作可ssh登录镜像ascend-mindspore-armms1.5的方法
  19. iOS 热更新/热修复
  20. KEIL5出现中文字体乱码

热门文章

  1. 最小公倍数和最大公约数的简洁写法
  2. spring boot + vue + element-ui全栈开发入门——windows开发环境
  3. 微软再次强调:爱开发 爱 Linux!
  4. 《自己动手做交互系统》——第1章 欢迎来到有形交互的世界 1.1 项目简介和背景知识...
  5. Git:使用 GitHub 托管代码的简单流程
  6. CodeForces 621C 数学概率期望计算
  7. VirtualBox开发环境的搭建详解
  8. js读取json数据(php传值给js)
  9. asp.net get set用法
  10. querySelector() 选择器语法