近日有一个需求是把检测的框的图像都截下来保存,就写了个脚本,分享出来
- 基本逻辑为循环读取每张图片的结果坐标文件,读取框坐标,再用OpenCV的函数裁剪下来并保存。
- 注意坐标文件为VOC格式,自行百度。
# coding=utf-8
import cv2
import os
import glob
import numpy as np
import re
def CutImg(imgpath,destpath):
img = glob.glob(r"/DATACENTER6/ph/traffic_model_faster101/in_pic/29-6-ori/*.jpg")
img_len= int(len(img))
#get all the images
for i in range(img_len):
temp=img[i].rstrip(".jpg") # get image name to find its' annotation file.
ori_img = cv2.imread(str(img[i]))
sp = ori_img.shape #obtain the image shape
sz1 = sp[0] #height(rows) of image
sz2 = sp[1] #width(colums) of image
np.set_printoptions(suppress=True)
txt = np.loadtxt(str(temp)+".txt") #load the pic's annotation, which is voc-style.
if txt.shape[0]==0: #if the annotation is null, drop it.
continue
if np.ndim(txt) == 1:
txt = txt.reshape(1, 5)
txt_len=len(txt)
# print(txt)
# print(txt_le