目录
2. 使用pyinstaller将labelme打包为exe文件
背景
由于工作原因,需要对图像进行分类,建模,作为深度学习的材料。自己造轮子造了半天,结果发现labelme这块做的很好,于是放弃造轮子,借用labelme产生的json文件,生成需要的8位黑白png图(labelme自带的生成8位png图是彩色的,不满足需求)。
软件环境
Win10 python3.8.2 labelme4.2.10 pyqt5.14.1 pip-20.0.2
黑白图像素填充原理
根据json文件中的label标签,随机生成不同的1-255之间的数值,作为region的像素值。背景像素全为0
源码
解析JSON文件,我添加了Newtonsoft.Json.dll引用。可以直接在NuGet中搜索添加。
System.Drawing也需要在“程序集-框架”中添加。
以下为程序源码
Program.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace labelme_Json_to_png8
{
class Program
{
/// <summary>
/// 根据labelme-4.2.10产生的json文件批量生成png图片
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
if(args.Length==2)
{
string strPathSrc = args[0]; //labelme产生的JSON文件所在文件夹
string strPathDes = args[1]; //产生的PNG文件存放的文件夹
try
{
List<string> srcFileNames = labelmeJson.GetJsonfiles(strPathSrc);
int srcNum = srcFileNames.Count;
for (int i = 0; i < srcNum; i++)
{
string strDes = Path.Combine(strPathDes, Path.GetFileNameWithoutExtension(srcFileNames[i])+".png");
//desFileNames.Add(str);
labelmeJson.CreateImage(srcFileNames[i], strDes);
}
Console.WriteLine("Convert OK! 目标文件夹地址:" + strPathDes);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
}
}
labelmeJson.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace labelme_Json_to_png8
{
public class labelmeJson
{
private static PointF[] GetPointFs(List<List<double>> listDou)
{
int inum = listDou.Count;
PointF[] pointf = new PointF[inum];
for (int i = 0; i < inum; i++)
{
PointF p = new PointF();
p.X = (float)listDou[i][0];
p.Y = (float)listDou[i][1];
pointf[i] = p;
}
return pointf;
}
/// <summary>
/// 随机给各个region赋1-255之间像素值,背景色像素值为0
/// </summary>

本文介绍如何使用labelme生成的JSON文件批量转换为8位黑白PNG图像,适用于深度学习图像分类任务。涵盖软件环境配置、像素填充原理及源代码实现。
最低0.47元/天 解锁文章
3132





