tasks.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, division, print_function, absolute_import
  3. import os
  4. import subprocess
  5. import tempfile
  6. from celery import shared_task, chain
  7. from .gen_tiles import gen_tiles
  8. @shared_task
  9. def generate_tiles(*args, **kwargs):
  10. return gen_tiles(*args, **kwargs)
  11. # Pipeline of hugin-based panorama generation
  12. # TODO: do something with the return code of the subprocess calls
  13. @shared_task
  14. def pto_gen(output_pto, images):
  15. subprocess.call(["pto_gen", "-o", output_pto] + images)
  16. return output_pto
  17. @shared_task
  18. def cpfind(input_pto, output_pto):
  19. subprocess.call(["cpfind", "-o", output_pto, "--multirow", "--celeste", input_pto])
  20. return output_pto
  21. @shared_task
  22. def cpclean(input_pto, output_pto):
  23. subprocess.call(["cpclean", "-o", output_pto, input_pto])
  24. return output_pto
  25. @shared_task
  26. def linefind(input_pto, output_pto):
  27. subprocess.call(["linefind", "-o", output_pto, input_pto])
  28. return output_pto
  29. @shared_task
  30. def autooptimiser(input_pto, output_pto):
  31. subprocess.call(["autooptimiser", "-a", "-m", "-l", "-s", "-o", output_pto, input_pto])
  32. return output_pto
  33. @shared_task
  34. def autocrop(input_pto, output_pto):
  35. subprocess.call(["pano_modify", "--canvas=AUTO", "--crop=AUTO", "-o", output_pto, input_pto])
  36. return output_pto
  37. @shared_task
  38. def generate_pano(input_pto, dirname, output_image):
  39. project_name = output_image[output_image.rfind(".tif")]
  40. output_name = os.path.join(dirname, project_name)
  41. # hugin_executor is only available since hugin 2015.0
  42. # return subprocess.call(["hugin_executor", "--prefix", output_name, input_pto])
  43. makefile_name = os.path.join(dirname, "celery_pano.mk")
  44. subprocess.call(["pto2mk", "-o", makefile_name, "-p", project_name, input_pto])
  45. return subprocess.call(["make", "-f", makefile_name])
  46. @shared_task
  47. def panorama_pipeline(images, output_image):
  48. """Automatically assemble a panorama, using Hugin in headless mode.
  49. [output_image] is the name of the desired output image. Must be a .tif
  50. image for now.
  51. """
  52. d = tempfile.mkdtemp(prefix='celery_hugin')
  53. pto = lambda filename: os.path.join(d, filename + ".pto")
  54. pipeline = pto_gen.s(pto("pto_gen"), images) | \
  55. cpfind.s(pto("cpfind")) | \
  56. cpclean.s(pto("cpclean")) | \
  57. linefind.s(pto("linefind")) | \
  58. autooptimiser.s(pto("autooptimiser")) | \
  59. autocrop.s(pto("autocrop")) | \
  60. generate_pano.s(d, output_image)
  61. return pipeline()