当前位置:   article > 正文

YOLOV5实现目标检测后先验框裁剪_yolov5怎么裁剪检测区域

yolov5怎么裁剪检测区域
  1. # -*- coding: utf-8 -*-
  2. # @Author : 大叔azhe
  3. # @Time : 2022/9/12 20:00
  4. # @Function:图片裁剪
  5. def caijian():
  6. # 仅支持JPEG,PNG,JPG格式图片
  7. path = "/root/cxc/python/yolov5-3.1/inference/output" # jpg图片和对应的生成结果的txt标注文件,放在一起
  8. path3 = "/root/cxc/python/yolov5-3.1/inference/cut/roi" # 裁剪出来的小图保存的根目录
  9. path6 = "/root/cxc/python/yolov5-3.1/inference/cut" # 裁剪出来的小图保存的根目录
  10. w = 400 # 原始图片resize
  11. h = 400
  12. img_total = []
  13. txt_total = []
  14. file = os.listdir(path)
  15. for filename in file:
  16. first, last = os.path.splitext(filename)
  17. # if (last in [".jpg",".jpeg","png"] ): # 图片的后缀名
  18. # img_total.append(first)
  19. # # print(img_total)
  20. # else:
  21. # txt_total.append(first)
  22. if (last in [".txt"]): # 图片的后缀名
  23. txt_total.append(first)
  24. # print(img_total)
  25. else:
  26. img_total.append(first)
  27. if os.path.exists(path3):
  28. shutil.rmtree(path3)
  29. os.mkdir(path3)
  30. else:
  31. os.mkdir(path3)
  32. for img_ in img_total:
  33. if img_ in txt_total:
  34. filename_img = img_ + ".jpg" # 图片的后缀名
  35. # print('filename_img:', filename_img)
  36. path1 = os.path.join(path, filename_img)
  37. a = os.path.exists(path1)
  38. if (a == False):
  39. filename_img = img_ + ".jpeg" # 图片的后缀名
  40. # print('filename_img:', filename_img)
  41. path1 = os.path.join(path, filename_img)
  42. a = os.path.exists(path1)
  43. if (a == False):
  44. filename_img = img_ + ".png" # 图片的后缀名
  45. # print('filename_img:', filename_img)
  46. path1 = os.path.join(path, filename_img)
  47. a = os.path.exists(path1)
  48. print("文件是否存在{}".format(a))
  49. img = cv2.imread(path1)
  50. img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC) # resize 图像大小,否则提取先验框时因原图差异区域可能会报错
  51. filename_txt = img_ + ".txt"
  52. # print('filename_txt:', filename_txt)
  53. n = 1
  54. with open(os.path.join(path, filename_txt), "r+", encoding="utf-8", errors="ignore") as f:
  55. for line in f:
  56. aa = line.split(" ")
  57. x_center = w * float(aa[1]) # aa[1]左上点的x坐标
  58. y_center = h * float(aa[2]) # aa[2]左上点的y坐标
  59. width = int(w * float(aa[3])) # aa[3]图片width
  60. height = int(h * float(aa[4])) # aa[4]图片height
  61. lefttopx = int(x_center - width / 2.0)
  62. lefttopy = int(y_center - height / 2.0)
  63. roi = img[lefttopy + 1:lefttopy + height + 3,
  64. lefttopx + 1:lefttopx + width + 1] # [左上y:右下y,左上x:右下x] (y1:y2,x1:x2)需要调参,否则裁剪出来的小图可能不太好
  65. print('roi:', roi)
  66. filename_last = img_ + "_" + str(n) + ".jpg" # 裁剪出来的小图文件名
  67. # print(filename_last)
  68. path2 = os.path.join(path6, "roi") # 需要在path3路径下创建一个roi文件夹
  69. print('path2:', path2) # 裁剪小图的保存位置
  70. cv2.imwrite(os.path.join(path2, filename_last), roi)
  71. n = n + 1
  72. else:
  73. continue

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/666955
推荐阅读
相关标签
  

闽ICP备14008679号