main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import cv2
  2. import os
  3. from processor import Detector
  4. def c_main(path, model, ext) -> tuple:
  5. ''' 主函数,返回图片的路径和图片的信息
  6. Args: path: 图片的路径
  7. model: 模型
  8. ext: 图片的后缀名
  9. Return: tuple: (图片的路径, 图片的信息)
  10. '''
  11. image_data = pre_process(path)
  12. image_info = predict.predict(image_data, model, ext)
  13. return image_data[1] + '.' + ext, image_info
  14. def pre_process(data_path):
  15. ''' 路径预处理
  16. 从data_path中提取文件名,去除文件后缀
  17. Args: data_path: 图片的路径
  18. Return: tuple: (图片的路径, 图片的信息)
  19. '''
  20. file_name = os.path.split(data_path)[1].split('.')[0]
  21. return data_path, file_name
  22. def predict(dataset, model :Detector, ext):
  23. ''' 预测,保存到服务器本地临时的目录
  24. Args: dataset: tuple: (图片的路径, 图片的信息)
  25. model: 模型
  26. ext: 图片的后缀名
  27. Return: tuple: (图片的路径, 图片的信息)
  28. '''
  29. global img_y
  30. x = dataset[0].replace('\\', '/')
  31. file_name = dataset[1]
  32. print(x)
  33. print(file_name)
  34. x = cv2.imread(x)
  35. img_y, image_info = model.detect(x)
  36. if cv2.imwrite('./tmp/draw/{}.{}'.format(file_name, ext), img_y):
  37. raise Exception('保存图片时出错.Error saving thepicture.')
  38. return image_info