当前位置:网站首页>Crop the large size image of target detection into a fixed size image
Crop the large size image of target detection into a fixed size image
2022-07-27 13:52:00 【AICVer】
Crop fixed size pictures
It is mainly to cut the large drawing into fixed size , And transform VOC Medium xml Format , The point is as follows :
- The size is not enough to be rounded into a picture .( Can be divided into 4 part , Fixed size of upper left part , The last column , The last line , And the rest of the lower right corner )
- If the target is on the edge , Adaptively enlarge the image size
The code is as follows :
import cv2
import os
import json
import numpy as np
from xml.dom.minidom import Document
IMG_SIZE = 640
LABEL_WIDTH = 30
# Main clipping logic
def crop_image(data_type = "val"):
img_path = "xxx\\"+data_type+"\images\\"
json_path = "xxx\\"+data_type+"\labels\\"
txt_path = "xxx\VOCdevkit\VOC2007\ImageSets\Main\\"
txt_file = open(txt_path + data_type + ".txt", "w")
img_save_path = "xxx\VOCdevkit\VOC2007\JPEGImages\\"
anno_path = "xxxx\VOCdevkit\VOC2007\Annotations\\"
for file in os.listdir(img_path):
if file.__contains__("png"):
json_name = json_path + file[0:-4] + ".json"
all_points = get_json_points(json_name)
img = cv2.imread(img_path+file)
h, w, _ = img.shape
print(img.shape, round(h/IMG_SIZE),round(w/IMG_SIZE))
index = 0
row = round(h/IMG_SIZE)
col = round(w/IMG_SIZE)
# complete 640*640
for i in range(row-1):
for j in range(col-1):
index = index + 1
cur_img_points, rect = get_crop_img_points(j * IMG_SIZE, i * IMG_SIZE, (j + 1) * IMG_SIZE,
(i + 1) * IMG_SIZE, all_points)
patch_image = img[rect[1]:rect[3],rect[0]:rect[2],:]
file_name = file[:-4] + "_crop_" + str(index)
json_points_to_xml(cur_img_points, rect[2] - rect[0], rect[3] - rect[1], anno_path, file_name)
cv2.imwrite(img_save_path + file_name +".png", patch_image)
txt_file.write(file_name + "\n")
# The last column
for i in range(row - 1):
index = index + 1
cur_img_points, rect = get_crop_img_points((col-1) * IMG_SIZE, i * IMG_SIZE, w,
(i + 1) * IMG_SIZE, all_points)
patch_image = img[rect[1]:rect[3], rect[0]:rect[2], :]
file_name = file[:-4] + "_crop_" + str(index)
json_points_to_xml(cur_img_points, rect[2] - rect[0], rect[3] - rect[1], anno_path, file_name)
cv2.imwrite(img_save_path + "" + file[:-4] + "_crop_" + str(index) + ".png", patch_image)
txt_file.write(file_name + "\n")
# The last line
for j in range(col - 1):
index = index + 1
cur_img_points, rect = get_crop_img_points(j * IMG_SIZE, (row-1) * IMG_SIZE, (j + 1) * IMG_SIZE,
h, all_points)
patch_image = img[rect[1]:rect[3], rect[0]:rect[2], :]
file_name = file[:-4] + "_crop_" + str(index)
json_points_to_xml(cur_img_points, rect[2] - rect[0], rect[3] - rect[1], anno_path, file_name)
cv2.imwrite(img_save_path + "" + file[:-4] + "_crop_" + str(index) + ".png", patch_image)
txt_file.write(file_name + "\n")
# The last piece in the lower right corner
index = index + 1
cur_img_points, rect = get_crop_img_points((col-1) * IMG_SIZE, (row - 1) * IMG_SIZE, w, h, all_points)
patch_image = img[rect[1]:rect[3], rect[0]:rect[2], :]
file_name = file[:-4] + "_crop_" + str(index)
json_points_to_xml(cur_img_points, rect[2] - rect[0], rect[3] - rect[1], anno_path, file_name)
cv2.imwrite(img_save_path + "" + file[:-4] + "_crop_" + str(index) + ".png", patch_image)
txt_file.write(file_name + "\n")
# Get all the target center points of the large image
def get_json_points(file_name):
file = open(file_name)
json_file = json.load(file)
points = json_file["shapes"]
all_points = []
for one_point_label in points:
# print(one_point_label)
all_points.append(one_point_label["points"][0])
print(len(all_points),all_points)
return all_points
# Get the target center point of the cropped small graph , And return to the upper left vertex of the adaptive image , And the lower right vertex
def get_crop_img_points(left_top_x,left_top_y,right_bottom_x,right_bottom_y,points):
cur_img_points = []
for point in points:
x = point[0]
y = point[1]
if left_top_x <= x < right_bottom_x and left_top_y <= y < right_bottom_y:
cur_img_points.append([x, y])
cur_img_points = np.asarray(cur_img_points)
if cur_img_points.shape[0] > 0:
[min_x, min_y] = np.amin(cur_img_points, axis=0)
[max_x, max_y] = np.amax(cur_img_points, axis=0)
else:
return cur_img_points, [int(left_top_x),int(left_top_y),int(right_bottom_x),int(right_bottom_y)]
offset = 2
if min_x - LABEL_WIDTH / 2 < left_top_x:
left_top_x = min_x - LABEL_WIDTH / 2 - offset
if min_y - LABEL_WIDTH / 2 < left_top_y:
left_top_y = min_y - LABEL_WIDTH / 2 - offset
if max_x + LABEL_WIDTH / 2 > right_bottom_x:
right_bottom_x = max_x + LABEL_WIDTH / 2 + offset
if max_y + LABEL_WIDTH / 2 > right_bottom_y:
right_bottom_y = max_y + LABEL_WIDTH / 2 + offset
cur_img_points = cur_img_points - [left_top_x,left_top_y]
return cur_img_points, [int(left_top_x),int(left_top_y),int(right_bottom_x),int(right_bottom_y)]
# Turn into VOC in XML Format data
def json_points_to_xml(points,width,height,annotations_path, file):
xml_file = annotations_path + file + ".xml"
doc = Document()
annotation = doc.createElement("annotation")
doc.appendChild(annotation)
folder = doc.createElement("folder")
folder_text = doc.createTextNode("VOC2007")
folder.appendChild(folder_text)
filename = doc.createElement("filename")
filename_text = doc.createTextNode(file + ".png")
filename.appendChild(filename_text)
size = doc.createElement("size")
annotation.appendChild(folder)
annotation.appendChild(filename)
annotation.appendChild(size)
size_w = doc.createElement("width")
size.appendChild(size_w)
size_w_text = doc.createTextNode(str(width))
size_w.appendChild(size_w_text)
size_h = doc.createElement("height")
size.appendChild(size_h)
size_h_text = doc.createTextNode(str(height))
size_h.appendChild(size_h_text)
depth = doc.createElement("depth")
size.appendChild(depth)
depth_text = doc.createTextNode("1")
depth.appendChild(depth_text)
for center in points:
one_object = doc.createElement("object")
annotation.appendChild(one_object)
object_name = doc.createElement("name")
object_name_text = doc.createTextNode("lgd")
object_name.appendChild(object_name_text)
one_object.appendChild(object_name)
pose = doc.createElement("pose")
pose.appendChild(doc.createTextNode("center"))
truncated = doc.createElement("truncated")
truncated.appendChild(doc.createTextNode("0"))
difficult = doc.createElement("difficult")
difficult.appendChild(doc.createTextNode("0"))
one_object.appendChild(pose)
one_object.appendChild(truncated)
one_object.appendChild(difficult)
bndbox = doc.createElement("bndbox")
xmin = doc.createElement("xmin")
ymin = doc.createElement("ymin")
xmax = doc.createElement("xmax")
ymax = doc.createElement("ymax")
one_object.appendChild(bndbox)
bndbox.appendChild(xmin)
bndbox.appendChild(ymin)
bndbox.appendChild(xmax)
bndbox.appendChild(ymax)
radius = LABEL_WIDTH / 2
x1 = center[0] - radius
y1 = center[1] - radius
x2 = center[0] + radius
y2 = center[1] + radius
xmin.appendChild(doc.createTextNode(str(x1)))
ymin.appendChild(doc.createTextNode(str(y1)))
xmax.appendChild(doc.createTextNode(str(x2)))
ymax.appendChild(doc.createTextNode(str(y2)))
f = open(xml_file, 'w')
f.write(doc.toprettyxml(indent='\t'))
f.close()
crop_image()
Verify display
The code is as follows :
import os
import json
import cv2
from xml.dom.minidom import Document
import xml.etree.cElementTree as ET
def draw_img_by_xml():
img_path = "xxx\VOCdevkit\VOC2007\JPEGImages\\"
xml_path = "xxx\VOCdevkit\VOC2007\Annotations\\"
for img_name in os.listdir(img_path):
print(img_name)
img = cv2.imread(img_path+img_name)
xml_dir_name = xml_path + img_name[0:-4] + ".xml"
tree = ET.parse(xml_dir_name)
root = tree.getroot()
for object in root.findall('object'):
bndbox = object.find('bndbox')
xmin = int(float(bndbox.find('xmin').text))
ymin = int(float(bndbox.find('ymin').text))
xmax = int(float(bndbox.find('xmax').text))
ymax = int(float(bndbox.find('ymax').text))
print(xmin,ymin,xmax,ymax)
cv2.rectangle(img, (xmin,ymin), (xmax,ymax), (255, 0, 0), 2)
cv2.imshow(img_name, img)
cv2.waitKey(0)
边栏推荐
- leetcode——83,24; Machine learning - neural networks
- Selenium eight elements positioning and relative locator
- Construction and application of industrial knowledge atlas (I): overview of industrial knowledge atlas
- 小程序毕设作品之微信校园洗衣小程序毕业设计成品(3)后台功能
- Cesium region clipping, local rendering
- Leetcode error reporting and its solution
- 深度置信网络(DBN)【经典的DBN网络结构是由若干层 RBM(受限波尔兹曼机)和一层 BP 组成的一种深层神经网络】
- 产品经理经验谈100篇(十一)-策略产品经理:模型与方法论
- LeetCode报错及其解决方案
- Tencent cloud and the China Federation of industry released the research results of industrial AI quality inspection standardization to accelerate the intelligent transformation of manufacturing indus
猜你喜欢

SQL tutorial: introduction to SQL aggregate functions

What are the precautions for using carbon brushes

Redis cluster setup - use docker to quickly build a test redis cluster
![[internship experience] add your own implementation method to the date tool class](/img/56/54c5f62438a627c96f8b4ad06ba863.jpg)
[internship experience] add your own implementation method to the date tool class
idea Gradle7.0+ :Could not find method compile()

Structural thinking

期货开户的条件和流程

How can electric complete set enterprises do well in cost and profit management with the help of ERP system?

期货手续费标准和保证金比例

【实习经验】Date工具类中添加自己实现的方法
随机推荐
Wechat campus laundry applet graduation design finished product of applet completion work (8) graduation design thesis template
JS callback function (callback)
字节跳动 AI Lab 总监李航:语言模型的过去、现在和未来
Summary of scaling and coding methods in Feature Engineering
【2022-07-25】
网络异常流量分析系统设计
Structural thinking
Cesium region clipping, local rendering
软考 系统架构设计师 简明教程 | 软件测试
腾讯云联合中国工联院发布工业AI质检标准化研究成果加速制造业智能化转型
eBPF/Ftrace
NoSQL -- three theoretical cornerstones of NoSQL -- cap -- Base -- final consistency
《C语言》函数栈帧的创建与销毁--(内功)
Come and watch, 17 practical skills of operation and maintenance~
Product manager experience 100 (XI) - Strategic Product Manager: model and methodology
Verilog's system tasks - $fopen, $fclose and $fddisplay, $fwrite, $fstrobe, $fmonitor
Conditions and procedures of futures account opening
Use recyclerview to realize the left sliding menu of the list
Software testing system architecture designer concise tutorial | software testing
2022ACM夏季集训周报(四)