json_to_yolo.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import glob
  2. import json
  3. import os
  4. import shutil
  5. import random
  6. import cv2
  7. import numpy as np
  8. import base64
  9. import math
  10. import io
  11. import PIL.Image
  12. import os.path as osp
  13. import shapely
  14. from shapely.geometry import box, Polygon,MultiPolygon
  15. from shapely.geometry import Point
  16. def json_to_yolo(src_path="d:/data/data_crop",dst_path="d:/data/data_yolo"):
  17. if os.path.exists(dst_path):
  18. shutil.rmtree(dst_path)
  19. os.mkdir(dst_path)
  20. os.makedirs(os.path.join(dst_path,"images"),exist_ok=True)
  21. os.makedirs(os.path.join(dst_path, "labels"), exist_ok=True)
  22. #cls_names=["white_crack","white_hole","white_debonding","black_crack","black_hole","black_debonding","rarefaction","structure_circle","structure_smallrect","structure_longrect"]
  23. #cls_names=["white_crack","white_hole","white_debonding","black_crack","black_hole","black_debonding","rarefaction","circle_obj","rect_obj"]
  24. cls_names=["crack","hole","debonding","rarefaction"]
  25. for phase in ["train","val"]:
  26. if os.path.exists(dst_path+"/images/"+phase):
  27. shutil.rmtree(dst_path+"/images/"+phase)
  28. os.mkdir(dst_path+"/images/"+phase)
  29. if os.path.exists(dst_path+"/labels/"+phase):
  30. shutil.rmtree(dst_path+"/labels/"+phase)
  31. os.mkdir(dst_path+"/labels/"+phase)
  32. for file in glob.glob(src_path+"/"+phase+"/*"):
  33. bsname, ext = os.path.basename(file).rsplit(".", 1)
  34. if ext in ["tif","tiff", "png", "jpg", "jpeg","bmp"]:
  35. json_file = os.path.join(os.path.dirname(file),bsname+".json")
  36. #print("yolo json_file: ", json_file)
  37. if os.path.exists(json_file):
  38. f_json=open(json_file,"rb")
  39. json_dict = json.load(f_json)
  40. #img_path = os.path.dirname(file)+"/"+json_dict["imagePath"]
  41. #parent = os.path.basename(os.path.dirname(file))
  42. shutil.copy(file,dst_path+"/images/"+phase+"/"+json_dict["imagePath"])
  43. h=json_dict["imageHeight"]
  44. w=json_dict["imageWidth"]
  45. img_bsname = json_dict["imagePath"].rsplit(".", 1)[0]
  46. #print(dst_path+"/labels/"+phase+"/"+img_bsname+".txt")
  47. with open(dst_path+"/labels/"+phase+"/"+img_bsname+".txt","w") as f_yolo:
  48. for shape in json_dict["shapes"]:
  49. lines = ""
  50. # if shape["label"] not in cls_names:
  51. # cls_names.append(shape["label"])
  52. if shape["label"] not in cls_names:
  53. continue
  54. # if cls_names.index(shape["label"])>9:
  55. # print(shape['label'])
  56. lines+=str(cls_names.index(shape["label"]))+" "
  57. # if shape["label"]=="crack":
  58. # print(file)
  59. for p in shape["points"]:
  60. lines+=str(p[0]*1.0/w)+" "+str(p[1]*1.0/h)+" "
  61. lines+="\n"
  62. f_yolo.writelines(lines)
  63. elif phase in ["val"]: #如果val中图片不存在对应的json文件,也加入到yolo的验证中
  64. shutil.copy(file, dst_path + "/images/" + phase + "/" + bsname + "." + ext)
  65. f_yolo = open(dst_path + "/labels/" + phase + "/" + bsname + ".txt", "w")
  66. f_yolo.close()
  67. #print("cls_name: ",cls_names)