PennFudanPed数据集解析截取

数据集的标注文件是文本格式

# Compatible with PASCAL Annotation Version 1.00
Image filename : "PennFudanPed/PNGImages/FudanPed00001.png"
Image size (X x Y x C) : 559 x 536 x 3
Database : "The Penn-Fudan-Pedestrian Database"
Objects with ground truth : 2 { "PASpersonWalking" "PASpersonWalking" }
# Note there may be some objects not included in the ground truth list for they are severe-occluded
# or have very small size.
# Top left pixel co-ordinates : (1, 1)
# Details for pedestrian 1 ("PASpersonWalking")
Original label for object 1 "PASpersonWalking" : "PennFudanPed"
Bounding box for object 1 "PASpersonWalking" (Xmin, Ymin) - (Xmax, Ymax) : (160, 182) - (302, 431)
Pixel mask for object 1 "PASpersonWalking" : "PennFudanPed/PedMasks/FudanPed00001_mask.png"

# Details for pedestrian 2 ("PASpersonWalking")
Original label for object 2 "PASpersonWalking" : "PennFudanPed"
Bounding box for object 2 "PASpersonWalking" (Xmin, Ymin) - (Xmax, Ymax) : (420, 171) - (535, 486)
Pixel mask for object 2 "PASpersonWalking" : "PennFudanPed/PedMasks/FudanPed00001_mask.png"

我们使用Python正则提取(Xmin, Ymin) - (Xmax, Ymax)数据,然后使用切片操作,截取行人,利用opencv保存每个行人图像。
在这里插入图片描述

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
import re
import cv2
import numpy as np

pngpath='./PNGImages/'
oritxt='./Annotation/'
newtxt='./data/'
savepath='./SingleObject/'
 
txtfiles = os.listdir(oritxt)
matrixs  = []
index = 1
for txtpath in txtfiles:
    img = cv2.imread(pngpath + txtpath[:-4] + ".png");
    f1 = open(oritxt + txtpath, 'r')
    for line in f1.readlines():
        if re.findall('Xmin',line):
            pt = [int(x) for x in re.findall(r"\d+",line)]
            matrixs.append(pt)        
            # cv2.rectangle(img, (pt[1], pt[2]), (pt[3], pt[4]), (0, 255, 0), 2)
            tmp = img[pt[2]:pt[4], pt[1]:pt[3]]
            cv2.imwrite(savepath + txtpath[:-4] + "_" + str(index) + ".png", tmp)
            index = index + 1
    f1.close()

data = [line[1:5] for line in matrixs]
np.savetxt('data', data, fmt="%d,%d,%d,%d")
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值