#注:此代码稍作修改也可以用于WFLW人脸数据集的标注文件解析,
#参见其README.md文件了解其每一行的信息,从中解析出相应字
#段即可。 import os
import cv2
def draw(image_list,src_img_dir = None, tar_img_dir = None):
if not os.path.exists(tar_img_dir):
os.mkdir(tar_img_dir)
for item in image_list:
sub_path = item["path"]
path_seg = sub_path.split("/")
path = os.path.join(src_img_dir,sub_path)
boxes = item["boxes"]
img = cv2.imread(path)
for box in boxes:
ord = box.split(" ")
x, y, w, h = int(ord[0]),int(ord[1]),int(ord[2]),int(ord[3])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0), 1)
tar_dir = os.path.join(tar_img_dir,path_seg[0])
if not os.path.exists(tar_dir):
os.mkdir(tar_dir)
tar_path = os.path.join(tar_dir,path_seg[1])
cv2.imwrite(tar_path,img) def parse(label_file_path, src_img_dir, tar_img_dir):
fr = open(label_file_path,'r')
image_list = []
line = fr.readline().rstrip()
while line:
mdict = {}
path = line
mdict["path"] = path
num = fr.readline().rstrip()
boxes_list = []
for n in range(int(num)):
box = fr.readline().rstrip()
boxes_list.append(box)
mdict["boxes"]=boxes_list
image_list.append(mdict)
line = fr.readline().rstrip()
draw(image_list,src_img_dir,tar_img_dir) if __name__=="__main__":
file_path = "/projects/DSOD/wider_face/datasets/wider_face_split/wider_face_train_bbx_gt.txt"
source_img_dir = "/projects/DSOD/wider_face/datasets/train/images"
target_img_dir = "/projects/DSOD/wider_face/datasets/drew"
parse(file_path,source_img_dir,target_img_dir)

其中,file_path是标注文件的路径。标注文件内容如下:

 0--Parade/0_Parade_marchingband_1_849.jpg
1
449 330 122 149 0 0 0 0 0 0
0--Parade/0_Parade_Parade_0_904.jpg
1
361 98 263 339 0 0 0 0 0 0
0--Parade/0_Parade_marchingband_1_799.jpg
21
78 221 7 8 2 0 0 0 0 0
78 238 14 17 2 0 0 0 0 0
113 212 11 15 2 0 0 0 0 0
134 260 15 15 2 0 0 0 0 0
163 250 14 17 2 0 0 0 0 0
201 218 10 12 2 0 0 0 0 0
182 266 15 17 2 0 0 0 0 0
245 279 18 15 2 0 0 0 0 0
304 265 16 17 2 0 0 0 2 1
328 295 16 20 2 0 0 0 0 0
389 281 17 19 2 0 0 0 2 0
406 293 21 21 2 0 1 0 0 0
436 290 22 17 2 0 0 0 0 0
522 328 21 18 2 0 1 0 0 0
643 320 23 22 2 0 0 0 0 0
653 224 17 25 2 0 0 0 0 0
793 337 23 30 2 0 0 0 0 0
535 311 16 17 2 0 0 0 1 0
29 220 11 15 2 0 0 0 0 0
3 232 11 15 2 0 0 0 2 0
20 215 12 16 2 0 0 0 2 0
0--Parade/0_Parade_marchingband_1_117.jpg
9
69 359 50 36 1 0 0 0 0 1
227 382 56 43 1 0 1 0 0 1
296 305 44 26 1 0 0 0 0 1
353 280 40 36 2 0 0 0 2 1
885 377 63 41 1 0 0 0 0 1
819 391 34 43 2 0 0 0 1 0
727 342 37 31 2 0 0 0 0 1
598 246 33 29 2 0 0 0 0 1
740 308 45 33 1 0 0 0 2 1
.....................

其中,第一行为图片路径,第二行为框的数量,紧接着是框。

以此类推。详见Widerface README.md文件。

source_dir为存储图片的根目录。

target_dir为画框过后的图片的存储路径。

最新文章

  1. Timestamp 使用
  2. Android init.rc执行顺序
  3. Error #include nested too deeply
  4. Notice: Trying to get property of non-object problem(PHP)解决办法 中间件只能跳转不能返任何数据
  5. python 调试
  6. js按值传递还是按引用传递?
  7. Data 语义学(1)
  8. 使用mobile jQuery 动态给select下拉添加数据,选中项默认不显示的解决方法。
  9. 图解SSIS监视文件夹并自动导入数据
  10. Vboxmanage改动uuid报错的解决的方法
  11. Codeforces980 D. Perfect Groups
  12. 二进制安装MySQL数据库
  13. 常用笔记:Linux
  14. [Node.js] 3、搭建hexo博客
  15. 【容器魔方解读】AWS Re:Invent 2018大会
  16. HDU 5842 Lweb and String 水题
  17. 各版本 MySQL 并行复制的实现及优缺点
  18. Mac 提交代码到Github
  19. JBPM学习(四):运行流程实例
  20. 解决Mybatis配置ORM映射 时分秒都为0

热门文章

  1. BZOJ4897 THUSC2016成绩单(区间dp)
  2. 事件驱动与异步IO--待更新
  3. [luogu1654]OSU!
  4. LOJ6303:水题——题解
  5. bzoj1052: [HAOI2007]覆盖问题(二分+构造)
  6. H5背景音乐自动播放(兼容微信IOS,进程后台切换自动停止播放,本文例子为Vue写法)
  7. 使用JavaScript OOP特性搭建Web应用
  8. 常见的Java问题
  9. UVA 11440 Help Tomisu
  10. 2015/8/31 Python基础(5):字符串