paddlex.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import numpy as np
  2. import cv2
  3. import os
  4. from .get_image_info import main
  5. rate = 0.5
  6. def predict(path, model):
  7. ''' predict
  8. Args: path - image path
  9. Returns: image path, image info
  10. '''
  11. global img_y
  12. file_name = os.path.split(path)[1].split('.')[0]
  13. x = path.replace('\\', '/')
  14. file_name = file_name
  15. print(x)
  16. print(file_name)
  17. img_y = model.predict(x)['label_map']
  18. img_y = img_y * 255
  19. img_y = img_y.astype(np.int)
  20. cv2.imwrite(f'./tmp/mask/{file_name}_mask.png', img_y,
  21. (cv2.IMWRITE_PNG_COMPRESSION, 0))
  22. last_process(file_name)
  23. image_info = main(file_name)
  24. return file_name + '.png', image_info
  25. def data_in_one(inputdata):
  26. if not inputdata.any():
  27. return inputdata
  28. inputdata = (inputdata - inputdata.min()) / (inputdata.max() - inputdata.min())
  29. return inputdata
  30. def pre_process(data_path):
  31. file_name = os.path.split(data_path)[1].split('.')[0]
  32. return data_path, file_name
  33. def last_process(file_name):
  34. ''' last process'''
  35. image = cv2.imread(f'./tmp/ct/{file_name}.png')
  36. mask = cv2.imread(f'./tmp/mask/{file_name}_mask.png', 0)
  37. contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  38. cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
  39. cv2.imwrite('./tmp/draw/{}.png'.format(file_name), image)