detect.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import argparse
  2. import json
  3. from detection.detection import detect, discretize, print_results
  4. from detection.refine import simulate, save
  5. from os.path import splitext, basename, join
  6. def main():
  7. ap = argparse.ArgumentParser()
  8. ap.add_argument("-i", "--input", required=True, help="Uses this input as image for detection")
  9. ap.add_argument("-s", "--scale", type=float, default=0.10, help="Scales images by this factor (default: x0.1)")
  10. ap.add_argument("-c", "--config", type=str, default="detection/config.json",
  11. help="Loads config from this file (default: detection/config.json)")
  12. ap.add_argument("--save", type=str, default="save/",
  13. help="Pass the directory where saves are stored. (default: save/)")
  14. ap.add_argument("--hide", action='store_true', default=False, help="Hide images if parameter is set")
  15. ap.add_argument("--refine", type=str, help="Pass a json file to refine model")
  16. args = ap.parse_args()
  17. with open(args.config, 'r') as file:
  18. config = json.load(file)
  19. if args.refine is not None:
  20. with open(args.refine, 'r') as file:
  21. refine = json.load(file)
  22. else:
  23. refine = None
  24. orig, blob_mask, blob, food_mask, food_img = detect(args.input, config)
  25. dsc_img, dsc_blob, dsc_food_list = discretize(blob, food_mask, config['Discrete Width'], config['Discrete Height'])
  26. if args.save is not None:
  27. filename = splitext(basename(args.input))[0] + "-detect"
  28. file_path = join(args.save, filename)
  29. board, player, img = simulate(dsc_img, dsc_blob, dsc_food_list, config, refine)
  30. save(file_path, board, player, img)
  31. # Prepare file_path for details if any to save
  32. file_path += "-details"
  33. else:
  34. file_path = None
  35. print_results(orig, blob_mask, blob, food_mask, food_img, dsc_img, args.scale, file_path, args.hide)
  36. if __name__ == "__main__":
  37. main()