这是笔者最近参加的 [ 天池大赛>“数字人体”视觉挑战赛——宫颈癌风险智能诊断],项目数据处理开源教程,也希望能找到一些志同道合朋友一起组队。
开源1: Windows10读取KFB文件并提取Roi与pos显示保存在图片上
- 环境 windows10 python3.69 numpy opencv
- 解压 : Kfbreader-win10-python36 到项目目录下,并改文件夹名为 Kfbreader_win10_python36
注意代码中 分别代表 kfb 与 对应 json 文件 file1 = “pos_1/” sample ".kfb" label1 = “labels/” sample ".json" 请根据自己文件位置定义
Python代码如下:
代码语言:javascript复制from Kfbreader_win10_python36 import kfbReader as kr
import cv2
import numpy as np
import json
scale = 20
sample = "T2019_9"
file1 = "pos_1/" sample ".kfb"
label1 = "labels/" sample ".json"
reader = kr.reader()
kr.reader.ReadInfo(reader,file1,scale,True)
def get_roi(label):
with open(label,"r") as f:
js = json.load(f)
rois = []
roi = {}
for dic in js:
if dic["class"] == "roi":
roi = dic
roi["poses"] = []
rois.append(roi)
else :
pass
for dic in js:
if dic["class"] == "roi":
pass
else:
for roi1 in rois:
if roi1["x"] <= dic["x"] and roi1["y"] <= dic["y"] and dic["x"] dic["w"] <= roi1["x"] roi1["w"] and dic["y"] dic["h"] <= roi1["y"] roi1["h"]:
roi1["poses"].append(dic)
return rois
rois = get_roi(label1)
for i,roi1 in enumerate(rois):
roi = reader.ReadRoi(roi1["x"],roi1["y"],roi1["w"],roi1["h"],scale)
for pos in roi1["poses"]:
rx = pos["x"]-roi1["x"]
ry = pos["y"]-roi1["y"]
cv2.rectangle(roi, (rx,ry), (rx pos["w"],ry pos["h"]),(0,255,0), 4)
save_name = "roi" str(i) ".jpg"
cv2.imwrite(save_name,roi)
print("save roi img:" save_name)
输出结果:
代码语言:javascript复制save roi img:roi0.jpg
save roi img:roi1.jpg
roi0.jpg:
roi1.jpg:
图中绿色就是标记处的pos位置
开源2: Ubuntu18读取KFB文件并提取Roi与pos显示保存在图片上
- 环境 Ubuntu18 python3.60 numpy opencv
Ubuntu安装不像Windows那么简单(直接复制即可),笔者在这里一步步介绍下如何在Ubuntu安装:
- 解压 : kfbreader-linux 到项目目录下,并改文件夹名为 kfbreader_linux
- 打开bash 进入 kfbreader_linux目录
- 依次执行: ln -s libhzzt.so libhzzt.so.1 ln -s libopencv_world.so.3.4.5 libopencv_world.so.3.4
- 验证是否可以在python环境下import kfbReader python -c “import kfbReader”
如果运行失败了,那么就应该是link没有成功,解决方法如下:
首先你要先获取两个路径地址,your_kfb_path与your_python_lib_path。 其中 your_kfb_path 就是 刚解压完成的 kfbreader_linux 绝对地址,笔者的是:/home/yss/prjs/python/Cervical_Cancer_prjs/Kfbreader_linux your_python_lib_path就是你的python地址 "/lib",python地址可以通过bash命令:which python 获取(这个python要是你激活使用的环境)。笔者结果如下:/home/yss/yes/envs/TF_GPU/bin/python,那么笔者的your_python_lib_path就是:/home/yss/yes/envs/TF_GPU/bin/python/lib
下面依次执行bash指令:
代码语言:javascript复制vim ~/.bashrc
加入一行:export PYTHONPATH=your_kfb_path:$PYTHONPATH
加入一行:export LD_LIBRARY_PATH=your_python_lib_path:your_kfb_path:$LD_LIBRARY_PATH
source ~/.bashrc
其中,笔者还遇到了这个问题: ImportError: libpng12.so.0: cannot open shared object file: No such file or directory 解决方法也很简单:从这个网址下载文件,https://github.com/Reginer/File/raw/master/libpng12.so.0 保存到kfbreader_linux路径下即可。
有几点需要注意的:
- kfbreader_linux这个文件夹及内部文件不要删除或者修改,这样会导致link不到文件
- 当使用不同python环境执行时,需要为每个python安装。
注意代码中 分别代表 kfb 与 对应 json 文件 file1 = “pos_1/” sample ".kfb" label1 = “labels/” sample ".json" 请根据自己文件位置定义
Python代码如下:
代码语言:javascript复制from kfbreader_linuximport kfbReader as kr
import cv2
import numpy as np
import json
scale = 20
sample = "T2019_9"
file1 = "pos_1/" sample ".kfb"
label1 = "labels/" sample ".json"
reader = kr.reader()
kr.reader.ReadInfo(reader,file1,scale,True)
def get_roi(label):
with open(label,"r") as f:
js = json.load(f)
rois = []
roi = {}
for dic in js:
if dic["class"] == "roi":
roi = dic
roi["poses"] = []
rois.append(roi)
else :
pass
for dic in js:
if dic["class"] == "roi":
pass
else:
for roi1 in rois:
if roi1["x"] <= dic["x"] and roi1["y"] <= dic["y"] and dic["x"] dic["w"] <= roi1["x"] roi1["w"] and dic["y"] dic["h"] <= roi1["y"] roi1["h"]:
roi1["poses"].append(dic)
return rois
rois = get_roi(label1)
for i,roi1 in enumerate(rois):
roi = reader.ReadRoi(roi1["x"],roi1["y"],roi1["w"],roi1["h"],scale)
for pos in roi1["poses"]:
rx = pos["x"]-roi1["x"]
ry = pos["y"]-roi1["y"]
cv2.rectangle(roi, (rx,ry), (rx pos["w"],ry pos["h"]),(0,255,0), 4)
save_name = "roi" str(i) ".jpg"
cv2.imwrite(save_name,roi)
print("save roi img:" save_name)
输出结果:
代码语言:javascript复制save roi img:roi0.jpg
save roi img:roi1.jpg
码字不易,觉得有用的话,麻烦动动手指赏个赞。下一篇我会开源如何将比赛数据转化为coco数据集的形式,大家就可以开始训练啦。
有什么问题的话欢迎访问点击笔者知识星球与我联系:[ AI深度学习应用之路] 告诉大家一个小秘密:我将在星球开源整套项目baseline