create_predict_result.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import copy
  2. import numpy as np
  3. import cv2
  4. from pathlib import Path
  5. import time
  6. from app.utils.card_inference.data_augmentation import LetterBox
  7. def point_mapTo_originImg(originImgSize,imgSize_train, now_point):
  8. letterBox = LetterBox(imgSize_train)
  9. rect_dict = letterBox.get_offset(originImgSize)
  10. x_ratio = originImgSize['width'] * 1.0 / rect_dict['width']
  11. y_ratio = originImgSize['height'] * 1.0 / rect_dict['height']
  12. new_y = round((now_point[1] - rect_dict['y']) * y_ratio)
  13. new_x = round((now_point[0] - rect_dict['x']) * x_ratio)
  14. new_point = [new_x, new_y]
  15. return new_point
  16. def create_result_singleImg(segClassDict,now_ansImgDict, originImgSize, imgSize_train,confidence = 0.5):
  17. """ansImg_list.append({"ans_img":ansImg,"probs":probs,"file_name":file_name})"""
  18. label = now_ansImgDict['ans_img']
  19. probs = now_ansImgDict['probs']
  20. file_name = now_ansImgDict['file_name']
  21. # confidence = 0.5
  22. assert confidence > 0, "置信度必须大于0"
  23. # label[label < confidence] = 0
  24. per_result = {}
  25. per_result['num'] = 0
  26. per_result['cls'] = []
  27. per_result['names'] = []
  28. per_result['conf'] = []
  29. per_result['shapes'] = []
  30. # 背景肯定不需要提取
  31. excludeClassList = ['___background___']
  32. # 遍历每个分类
  33. for key, val in segClassDict.items():
  34. if val not in excludeClassList:
  35. now_class_num = int(key)
  36. now_prob_img = probs[now_class_num]
  37. # 10、将对应分类的图片弄白,其余全黑
  38. imgZero = np.zeros(label.shape, dtype=np.uint8)
  39. imgZero[label == (now_class_num)] = 255
  40. # 20、检测轮廓
  41. contours, _ = cv2.findContours(imgZero, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  42. # drawnContourImg = np.zeros((imgZero.shape[0], imgZero.shape[1], 3), dtype=np.uint8)
  43. # 把轮廓画出来了,可能label就是对应的轮廓
  44. # drawnContourImg = cv2.drawContours(drawnContourImg, contours, -1, (255, 0, 0), 2)
  45. # 30、获取shape数据
  46. if len(contours):
  47. maxCntArea = 5
  48. # 找最大轮廓面积对应的index
  49. for index in range(len(contours)):
  50. nowPntList = []
  51. # 面积
  52. contourArea = cv2.contourArea(contours[index])
  53. # 面积要大于5
  54. if contourArea > maxCntArea:
  55. # 计算置信度
  56. # 创建一个全为零的掩码图像,大小和原图像一样
  57. mask = np.zeros_like(imgZero)
  58. # 绘制第一个轮廓(假设我们只关心第一个轮廓)
  59. cv2.drawContours(mask, contours, index, (255), thickness=cv2.FILLED)
  60. # plt.imshow(now_prob_img, cmap='gray')
  61. # plt.show()
  62. # plt.imshow(mask, cmap='gray')
  63. # plt.show()
  64. # 计算轮廓区域的均值
  65. mean_val = cv2.mean(now_prob_img, mask=mask)
  66. now_conf = mean_val[0]
  67. externalContour = contours[index]
  68. pointNum = len(externalContour)
  69. # print(pointNum)
  70. for i in range(pointNum):
  71. nowPoint = externalContour[i]
  72. nowPoint_list = nowPoint[0].tolist()
  73. # 点要放到到原图上面
  74. new_point = point_mapTo_originImg(originImgSize,imgSize_train, nowPoint_list)
  75. nowPntList.append(new_point)
  76. pre_defect = {}
  77. pre_defect['class_num'] = int(key)
  78. pre_defect['label'] = str(val)
  79. pre_defect['probability'] = now_conf
  80. pre_defect['points'] = nowPntList
  81. if now_conf >= confidence:
  82. per_result['cls'].append(int(key))
  83. per_result['names'].append(str(val))
  84. per_result['conf'] = now_conf
  85. per_result['shapes'].append(pre_defect)
  86. per_result['num'] = len(per_result['shapes'])
  87. return per_result