根据label-Image的.txt标签提取图片中相应的内容

  • Post author:
  • Post category:其他


#from PIL import Image
import cv2
import sys

for i in range(1,9):
     txt_path = "C:\\Users\\PC\\Desktop\\tiqu\\tiqushuju\\heselabels\\" + str(i) + '.txt' #标签存放位置   
     dir = open(txt_path)
     lines = dir.readlines()
     lists = []  # 直接用一个数组存起来就好了
     for line in lines:
         lists.append(line.split())
     print(lists)

        
     im_path = "C:\\Users\\PC\\Desktop\\tiqu\\tiqushuju\\heisesuliaodai\\"+str(i)+'.jpg' #图片存放位置
     picture = cv2.imread(im_path)
     print(picture.shape[0]) #高 H y
     print(picture.shape[1]) #宽 x
     im = cv2.imread(im_path)

     for j in range(0, len(lists)):
         #h = lists[j][0] #标签 火15;烟16
         a = lists[j][1]  #宽
         b = lists[j][2] #高
         c = lists[j][3] #宽度
         d = lists[j][4] #高度


        #将标签中归一化的坐标还原为图片的大小尺寸
         e = int((float(a) * picture.shape[1]) - (float(c) * picture.shape[1]/2))
         f = int((float(b) * picture.shape[0]) - (float(d) * picture.shape[0]/2))
         q = int((float(a) * picture.shape[1]) + (float(c) * picture.shape[1]/2))
         s = int((float(b) * picture.shape[0]) + (float(d) * picture.shape[0]/2))
         #cropedIm = im[374:429,685:837]
         cropedIm = im[f:s,e:q]
         # cropedIm = im.crop((700, 100, 1200, 1000))
         path1 = 'C:\\Users\\PC\\Desktop\\tiqu\\tiqushuju\\hei\\' + str(i) + str(j) + '.jpg' #提取的图片存放位置
         #path2 = 'F:/results/smoke/' + str(i) + str(j) + '.jpg'
         #{提取火和烟目标
         #if int(h) == 15:
         #   cv2.imwrite(path1,cropedIm)
         #else:
         #   cv2.imwrite(path2, cropedIm)
         #   }
         #只提取火区域
         cv2.imwrite(path1,cropedIm)



关于label-image标注图片时,出现   ZeroDivisionError: float division by zero 的情况,解决办法:

以下为引用部分

链接:

https://github.com/tzutalin/labelImg/issues/386

使用如下代码:

import cv2
import os

def loadImages(path = r"C:\Users\ldx\Desktop\1111\picture"):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jpeg')]
filenames = loadImages()
images = []
for file in filenames:
    images.append(cv2.imread(file, cv2.IMREAD_UNCHANGED))

num = 64
for image in images:
    cv2.imwrite(str(num)+".jpg",image)
    num += 1

修改版:

import cv2
import os


def crop(labeltxt_path,image_path,label_dict,save_path):
    label_list = os.listdir(labeltxt_path)
    label_list = sorted(label_list, key=lambda item:item[0:1])
    img_list = os.listdir(image_path)
    img_list = sorted(img_list, key=lambda item: item[0:1])
    for i in range(len(label_list)):
        txt_path = os.path.join(labeltxt_path,label_list[i])
        dir = open(txt_path)
        lines = dir.readlines()
        lists = []  # 直接用一个数组存起来就好了
        for line in lines:
            lists.append(line.split())
        #print(lists)

        im_path = os.path.join(image_path,img_list[i])

        picture = cv2.imread(im_path)
        #print(picture.shape[0])  # 高 H y
        #print(picture.shape[1])  # 宽 x
        im = cv2.imread(im_path)

        for j in range(0, len(lists)):
            label = lists[j][0] #标签
            a = lists[j][1]  # x
            b = lists[j][2]  # y
            c = lists[j][3]  # 宽度
            d = lists[j][4]  # 高度

            if int(label) in label_dict.values():
                # 将标签中归一化的坐标还原为图片的大小尺寸
                e = int((float(a) * picture.shape[1]) - (float(c) * picture.shape[1] / 2))
                f = int((float(b) * picture.shape[0]) - (float(d) * picture.shape[0] / 2))
                q = int((float(a) * picture.shape[1]) + (float(c) * picture.shape[1] / 2))
                s = int((float(b) * picture.shape[0]) + (float(d) * picture.shape[0] / 2))
                # cropedIm = im[374:429,685:837]
                cropedIm = im[f:s, e:q]
                # cropedIm = im.crop((700, 100, 1200, 1000))
                path1 = save_path + str(i) + str(j) + '.jpg'  # 提取的图片存放位置
                cv2.imwrite(path1, cropedIm)
            else:
                continue

if __name__ =="__main__":
    '''
    target 0
    fire   1
    smoke  2
    target标签的顺序是0   添加想要提取的标签类型
    '''
    label_dict = {"target":0}   
    labeltxt_path = "./111/label/"
    image_path = "./111/image/"
    save_path = "./111/save/"
    crop(labeltxt_path,image_path,label_dict,save_path)
    





版权声明:本文为qq_29543997原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。