tasks.py 2.5 KB

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