writeMD.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from mdutils.mdutils import MdUtils
  2. import os
  3. import cv2
  4. import numpy as np
  5. import requests
  6. # url = "http://192.168.199.249:4869"
  7. # 标签是经过归一化的,需要变回来
  8. # xywh格式 ---> box四个顶点的坐标
  9. def xywh2lrbt(img_w, img_h, box):
  10. c, x, y, w, h = float(box[0]), float(box[1]), float(box[2]), float(box[3]), float(box[4])
  11. x = x * img_w # 中心坐标x
  12. w = w * img_w # box的宽
  13. y = y * img_h # 中心坐标y
  14. h = h * img_h # box的高
  15. lt_x, lt_y = x - w / 2, y - h / 2 # left_top_x, left_top_y
  16. lb_x, lb_y = x - w / 2, y + h / 2 # left_bottom_x, left_bottom_y
  17. rb_x, rb_y = x + w / 2, y + h / 2 # right_bottom_x, right_bottom_y
  18. rt_x, rt_y = x + w / 2, y - h / 2 # right_top_x, right_bottom_y
  19. lrbt = [[lt_x, lt_y], [lb_x, lb_y], [rb_x, rb_y], [rt_x, rt_y]]
  20. return lrbt, c
  21. def IOU(rect1, rect2):
  22. xmin1, ymin1, xmax1, ymax1 = rect1
  23. xmin2, ymin2, xmax2, ymax2 = rect2
  24. s1 = (xmax1 - xmin1) * (ymax1 - ymin1)
  25. s2 = (xmax2 - xmin2) * (ymax2 - ymin2)
  26. sum_area = s1 + s2
  27. left = max(xmin2, xmin1)
  28. right = min(xmax2, xmax1)
  29. top = max(ymin2, ymin1)
  30. bottom = min(ymax2, ymax1)
  31. if left >= right or top >= bottom:
  32. return 0
  33. intersection = (right - left) * (bottom - top)
  34. return intersection / (sum_area - intersection) * 1.0
  35. def send_requests():
  36. return result
  37. img_path = "./images/test1.jpg"
  38. b_img_list = []
  39. b_img_list.append(('file_list', ('image.jpeg', open(img_path, 'rb'), 'image/jpeg')))
  40. payload = {'model_name': 'ocr-layout', 'img_size': 800, 'download_image': False}
  41. response = requests.request("POST", "http://192.168.199.107:18089/detect", headers={}, data=payload, files=b_img_list)
  42. result = eval(str(response.text))
  43. result = eval(str(result['result']))
  44. # print(len(result))
  45. mdFile = MdUtils(file_name='yolov5_yili_0818_1', title='Markdown File Example')
  46. mdFile.new_header(level=1, title="yolov5预测结果")
  47. list_item = ["ori_cla", "predict_cla", "cut_ori_img", "cut_predict_img", "isTrue"]
  48. # list_item.extend(["ori_cla", "predict_cla", "cut_ori_img", "cut_predict_img", "isTrue"])
  49. # for i in range(len(result[0])):
  50. # # print(stencil_list[0][i])
  51. #
  52. # predict_cla = result[0][i]["class"]
  53. # 获取真实的标签
  54. label = "./images/test1.txt"
  55. with open(label, mode="r", encoding="utf-8") as f:
  56. lines = f.readlines()
  57. img = cv2.imread(img_path)
  58. img_h, img_w, _ = img.shape
  59. arr_label = [] # labels的边界框,存储四个顶点坐标
  60. ori_clas = [] # label的真实类别
  61. for line in lines:
  62. line = line.split(" ")
  63. lrbt, c = xywh2lrbt(img_w, img_h, line)
  64. arr_label.append(lrbt)
  65. ori_clas.append(c)
  66. # cut_imgs = [] # 被剪切的图像
  67. num = 0
  68. for i in range(len(arr_label)):
  69. lt_x, lt_y = int(arr_label[i][0][0]), int(arr_label[i][0][1])
  70. rb_x, rb_y = int(arr_label[i][2][0]), int(arr_label[i][2][1])
  71. rect1 = [lt_x, lt_y, rb_x, rb_y]
  72. cut_ori_img = img[lt_y:rb_y, lt_x:rb_x, :]
  73. save_cut_ori_img = "./images/cut_imgs/" + str(i) + "_" + str(num) + ".jpg"
  74. cv2.imwrite(save_cut_ori_img, cut_ori_img)
  75. save_cut_ori_img = mdFile.new_inline_image(text='', path=save_cut_ori_img)
  76. num += 1
  77. ori_cla = ori_clas[i]
  78. for j in range(len(result[0])):
  79. res = []
  80. predict_cla = result[0][j]['class']
  81. if ori_cla == predict_cla:
  82. rect2 = result[0][j]['bbox']
  83. iou = IOU(rect1, rect2)
  84. if iou >= 0.5:
  85. cut_predict_img = img[rect2[1]:rect2[3], rect2[0]:rect2[2], :]
  86. save_cut_predict_img = "./images/cut_imgs/" + str(j) + ".jpg"
  87. cv2.imwrite(save_cut_predict_img, cut_predict_img)
  88. save_cut_predict_img = mdFile.new_inline_image(text='', path=save_cut_predict_img)
  89. isTrue = True
  90. list_item.extend([ori_cla, predict_cla, save_cut_ori_img, save_cut_predict_img, isTrue])
  91. # list_item.extend(res)
  92. break
  93. elif j == len(result[0]) - 1:
  94. isTrue = False
  95. list_item.extend([ori_cla, predict_cla, save_cut_ori_img, "", isTrue])
  96. # res.append([ori_cla, predict_cla, "", "", isTrue])
  97. list_item.extend(res)
  98. #
  99. # print(list_item)
  100. # print(len(arr_label))
  101. print(len(list_item))
  102. # print(len(result[0]))
  103. mdFile.new_line()
  104. mdFile.new_table(columns=5, rows=len(list_item)//5, text=list_item, text_align='center')
  105. mdFile.create_md_file()
  106. if __name__ == "__main__":
  107. images_path = "./test_imgs/images/"
  108. labels_path = "./test_imgs/labels/"
  109. list_images = os.listdir(images_path)
  110. list_labels = os.listdir(labels_path)
  111. for image in list_images:
  112. img_name = image.split(".")[0]
  113. mdFile = MdUtils(file_name='yq_0819_' + str(img_name), title='yq_0819_' + str(img_name) + '_iou>=0.5')
  114. #
  115. # images = os.listdir(images_path)
  116. # labels = os.listdir(labels_path)
  117. #
  118. # assert len(images) == len(labels)
  119. #
  120. # for i in range(len(images)):
  121. # image = images[i]
  122. # label = labels[i]
  123. # with open(labels_path + label, mode="r", encoding="utf-8") as f:
  124. # lines = f.readlines()
  125. #
  126. # img = images_path + images[i]
  127. # img = cv2.imread(img)
  128. # img_h, img_w, _ = img.shape
  129. #
  130. # arr_label = [] # labels的边界框,存储四个顶点坐标
  131. # ori_cla = [] # label的真实类别
  132. # for line in lines:
  133. # line = line.split(" ")
  134. # lrbt, c = xywh2lrbt(img_w, img_h, line)
  135. # arr_label.append(lrbt)
  136. # ori_cla.append(c)
  137. #
  138. # cut_path = "./valid_img/cut/"
  139. #
  140. # for i in range(len(arr_label)):
  141. # lt_x, lt_y = int(arr_label[i][0][0]), int(arr_label[i][0][1])
  142. # rb_x, rb_y = int(arr_label[i][2][0]), int(arr_label[i][2][1])
  143. # cut_img = img[lt_y:rb_y, lt_x:rb_x, :]
  144. # cv2.imwrite(cut_path + str(i) + ".jpg", cut_img)
  145. #
  146. # box = np.reshape(np.array(arr_label[i]), [-1, 1, 2]).astype(np.int32)
  147. # img = cv2.polylines(np.array(img), [box], True, (255, 0, 0), 5)
  148. #
  149. #
  150. #
  151. #
  152. # mdFile.create_md_file()
  153. #
  154. #