atom-predict/msunet/DataPreProcess_test.ipynb

291 lines
12 KiB
Plaintext
Executable File

{
"cells": [
{
"cell_type": "code",
"execution_count": 46,
"id": "66ee10ca-9a42-4e52-a56c-3c4e8b470afa",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.162368Z",
"start_time": "2024-06-20T05:57:02.157402Z"
}
},
"outputs": [],
"source": [
"import os\n",
"import cv2\n",
"import glob\n",
"import copy\n",
"import json\n",
"import shutil\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from PIL import Image\n",
"from labelme import utils\n",
"from skimage.feature import peak_local_max"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "40711569-6f55-49d2-8a88-152c7b677218",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.167292Z",
"start_time": "2024-06-20T05:57:02.165442Z"
}
},
"outputs": [],
"source": [
"class_dict = {\n",
" 1: 'Norm', \n",
" 2: 'SV',\n",
" 3: 'LineSV',\n",
"}\n",
"\n",
"class_dict_rev = {\n",
" '0': 0, \n",
" '1': 1,\n",
" '2': 2,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "1762751c-7d19-482c-96ff-477b9ea50e5b",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.173565Z",
"start_time": "2024-06-20T05:57:02.170089Z"
}
},
"outputs": [],
"source": [
"def crop_slide(img_path, save_path, patch_size=256, step=128):\n",
" #2048*2048,裁减出来的size是256*256,滑动窗口128\n",
" \n",
" base_name = img_path.split('/')[-1].split('.')[0]\n",
" json_path = img_path.replace('.jpg', '.json')\n",
" img = cv2.imread(img_path, 0)\n",
" # img = cv2.equalizeHist(img)\n",
" # img = cv2.GaussianBlur(img, (5, 5), 0)\n",
" h, w = img.shape\n",
" \n",
" with open(json_path) as f:\n",
" json_data = json.load(f)\n",
"\n",
" points = np.array([item['points'][0][::-1] for item in json_data['shapes']], np.int32)\n",
" labels = np.array([class_dict_rev[item['label']] for item in json_data['shapes']], np.int32)\n",
" \n",
" mask = np.zeros_like(img)\n",
" mask[points[:, 0], points[:, 1]] = labels\n",
"\n",
" for i in range(0, h-patch_size+1, step):\n",
" for j in range(0, w-patch_size+1, step):\n",
" v_nums = np.sum(mask[i:i+patch_size, j:j+patch_size] > 1)\n",
" \n",
" Image.fromarray(img[i:i+patch_size, j:j+patch_size]).save(\n",
" os.path.join(save_path, 'img', '{}_{}_{}_{}.png'.format(base_name, str(i), str(j), str(v_nums)))\n",
" )\n",
" \n",
" Image.fromarray(mask[i:i+patch_size, j:j+patch_size]).save(\n",
" os.path.join(save_path, 'lbl', '{}_{}_{}_{}.png'.format(base_name, str(i), str(j), str(v_nums)))\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "1417b046-4e1c-4b9c-9aff-cf5295701601",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:37.902137Z",
"start_time": "2024-06-20T05:57:37.881766Z"
}
},
"outputs": [],
"source": [
"def process_slide(img_path, save_path):\n",
" base_name = img_path.split('/')[-1].split('.')[0]\n",
" json_path = img_path.replace('.jpg', '.json')\n",
" img = cv2.imread(img_path, 0)\n",
" # img = cv2.equalizeHist(img)\n",
" # img = cv2.GaussianBlur(img, (5, 5), 0)\n",
" \n",
" h, w = img.shape\n",
" \n",
" with open(json_path) as f:\n",
" json_data = json.load(f)\n",
"\n",
" points = np.array([item['points'][0][::-1] for item in json_data['shapes']], np.int32)\n",
" labels = np.array([class_dict_rev[item['label']] for item in json_data['shapes']], np.int32)\n",
" \n",
" mask = np.zeros_like(img)\n",
" for idx, point in enumerate(points):\n",
" cv2.circle(mask, point[::-1], 8, int(labels[idx]), -1)\n",
" \n",
" mask = np.zeros_like(img)\n",
" mask[points[:, 0], points[:, 1]] = labels\n",
" \n",
" Image.fromarray(img).save(\n",
" os.path.join(save_path, 'img', '{}.png'.format(base_name))\n",
" )\n",
" \n",
" Image.fromarray(mask).save(\n",
" os.path.join(save_path, 'lbl', '{}.png'.format(base_name))\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "59c532e0-39ec-406c-8d77-e0a17a9181cd",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.185924Z",
"start_time": "2024-06-20T05:57:02.183624Z"
}
},
"outputs": [
{
"data": {
"text/plain": "10"
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"img_lst = glob.glob('../../data/linesv/slide/*.jpg') \n",
"img_lst.sort(); len(img_lst)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "41c0bdb9-f5ca-4744-bc8a-d5d9723716c9",
"metadata": {
"tags": [],
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.189160Z",
"start_time": "2024-06-20T05:57:02.186688Z"
}
},
"outputs": [],
"source": [
"# for _type in ['test']: \n",
"# os.makedirs('/home/gao/下载/process/test/{}/img'.format(_type), exist_ok=True)\n",
"# os.makedirs('/home/gao/下载/process/test/{}/lbl'.format(_type), exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "378409b3-66b2-4325-8822-104c9c2519a1",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.192446Z",
"start_time": "2024-06-20T05:57:02.189668Z"
}
},
"outputs": [],
"source": [
"# crop_slide('../../data/linesv/slide/0.jpg', save_path='../../data/linesv/patch_unet/train/', step=64)\n",
"# crop_slide('../../data/linesv/slide/3.jpg', save_path='../../data/linesv/patch_unet/valid/', step=256)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "14d437d6-a43c-4471-a5ff-5df43e273819",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:02.195706Z",
"start_time": "2024-06-20T05:57:02.193215Z"
}
},
"outputs": [],
"source": [
"# for name in [2, 4, 6, 8, 10, 20, 30, 40]:\n",
"# process_slide('../../data/linesv/slide/{}.jpg'.format(name), save_path='../../data/linesv/patch_unet/test/')"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "ecdb4f79-cfa5-4511-bf67-21bbe26ba403",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-20T05:57:47.740520Z",
"start_time": "2024-06-20T05:57:40.815902Z"
}
},
"outputs": [
{
"ename": "KeyError",
"evalue": "'Norm'",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mKeyError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[0;32mIn[57], line 3\u001B[0m\n\u001B[1;32m 1\u001B[0m img_lst \u001B[38;5;241m=\u001B[39m glob\u001B[38;5;241m.\u001B[39mglob(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m/home/gao/mouclear/cc/data/end-to-end-result-xj/end-to-end-gnn/raw/*.jpg\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m img_lst:\n\u001B[0;32m----> 3\u001B[0m \u001B[43mprocess_slide\u001B[49m\u001B[43m(\u001B[49m\u001B[43mitem\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43msave_path\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43m/home/gao/mouclear/cc/data/end-to-end-result-xj/end-to-end-gnn/raw\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n",
"Cell \u001B[0;32mIn[56], line 14\u001B[0m, in \u001B[0;36mprocess_slide\u001B[0;34m(img_path, save_path)\u001B[0m\n\u001B[1;32m 11\u001B[0m json_data \u001B[38;5;241m=\u001B[39m json\u001B[38;5;241m.\u001B[39mload(f)\n\u001B[1;32m 13\u001B[0m points \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([item[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpoints\u001B[39m\u001B[38;5;124m'\u001B[39m][\u001B[38;5;241m0\u001B[39m][::\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m] \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m json_data[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mshapes\u001B[39m\u001B[38;5;124m'\u001B[39m]], np\u001B[38;5;241m.\u001B[39mint32)\n\u001B[0;32m---> 14\u001B[0m labels \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([class_dict_rev[item[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mlabel\u001B[39m\u001B[38;5;124m'\u001B[39m]] \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m json_data[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mshapes\u001B[39m\u001B[38;5;124m'\u001B[39m]], np\u001B[38;5;241m.\u001B[39mint32)\n\u001B[1;32m 16\u001B[0m mask \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39mzeros_like(img)\n\u001B[1;32m 17\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m idx, point \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28menumerate\u001B[39m(points):\n",
"Cell \u001B[0;32mIn[56], line 14\u001B[0m, in \u001B[0;36m<listcomp>\u001B[0;34m(.0)\u001B[0m\n\u001B[1;32m 11\u001B[0m json_data \u001B[38;5;241m=\u001B[39m json\u001B[38;5;241m.\u001B[39mload(f)\n\u001B[1;32m 13\u001B[0m points \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([item[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpoints\u001B[39m\u001B[38;5;124m'\u001B[39m][\u001B[38;5;241m0\u001B[39m][::\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m] \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m json_data[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mshapes\u001B[39m\u001B[38;5;124m'\u001B[39m]], np\u001B[38;5;241m.\u001B[39mint32)\n\u001B[0;32m---> 14\u001B[0m labels \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([\u001B[43mclass_dict_rev\u001B[49m\u001B[43m[\u001B[49m\u001B[43mitem\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mlabel\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m]\u001B[49m \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m json_data[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mshapes\u001B[39m\u001B[38;5;124m'\u001B[39m]], np\u001B[38;5;241m.\u001B[39mint32)\n\u001B[1;32m 16\u001B[0m mask \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39mzeros_like(img)\n\u001B[1;32m 17\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m idx, point \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28menumerate\u001B[39m(points):\n",
"\u001B[0;31mKeyError\u001B[0m: 'Norm'"
]
}
],
"source": [
"img_lst = glob.glob('/home/gao/mouclear/cc/data/end-to-end-result-xj/end-to-end-gnn/raw/*.jpg')\n",
"for item in img_lst:\n",
" process_slide(item, save_path='/home/gao/mouclear/cc/data/end-to-end-result-xj/end-to-end-gnn/raw')\n",
" "
]
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "6171b6259ef615c7",
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee0f80af-0c81-43fc-b879-002ad63413e8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}