tasks.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # Projection type 1 is cylindrical
  16. subprocess.call(["pto_gen", "-p", "1", "-o", output_pto] + images)
  17. return output_pto
  18. @shared_task
  19. def cpfind(input_pto, output_pto):
  20. subprocess.call(["cpfind", "-o", output_pto, "--multirow", "--celeste", input_pto])
  21. return output_pto
  22. @shared_task
  23. def cpclean(input_pto, output_pto):
  24. subprocess.call(["cpclean", "-o", output_pto, input_pto])
  25. return output_pto
  26. @shared_task
  27. def linefind(input_pto, output_pto):
  28. subprocess.call(["linefind", "-o", output_pto, input_pto])
  29. return output_pto
  30. @shared_task
  31. def autooptimiser(input_pto, output_pto):
  32. subprocess.call(["autooptimiser", "-a", "-m", "-l", "-s", "-o", output_pto, input_pto])
  33. return output_pto
  34. @shared_task
  35. def autocrop(input_pto, output_pto):
  36. subprocess.call(["pano_modify", "--canvas=AUTO", "--crop=AUTO", "-o", output_pto, input_pto])
  37. return output_pto
  38. @shared_task
  39. def generate_pano(input_pto, dirname, output_image):
  40. project_name = output_image[output_image.rfind(".tif")]
  41. output_name = os.path.join(dirname, project_name)
  42. # hugin_executor is only available since hugin 2015.0
  43. # return subprocess.call(["hugin_executor", "--prefix", output_name, input_pto])
  44. makefile_name = os.path.join(dirname, "celery_pano.mk")
  45. subprocess.call(["pto2mk", "-o", makefile_name, "-p", project_name, input_pto])
  46. return subprocess.call(["make", "-f", makefile_name])
  47. @shared_task
  48. def panorama_pipeline(images, output_image):
  49. """Automatically assemble a panorama, using Hugin in headless mode.
  50. [output_image] is the name of the desired output image. Must be a .tif
  51. image for now.
  52. """
  53. d = tempfile.mkdtemp(prefix='celery_hugin')
  54. pto = lambda filename: os.path.join(d, filename + ".pto")
  55. pipeline = pto_gen.s(pto("pto_gen"), images) | \
  56. cpfind.s(pto("cpfind")) | \
  57. cpclean.s(pto("cpclean")) | \
  58. linefind.s(pto("linefind")) | \
  59. autooptimiser.s(pto("autooptimiser")) | \
  60. autocrop.s(pto("autocrop")) | \
  61. generate_pano.s(d, output_image)
  62. return pipeline()