views.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # django imports
  2. from django.shortcuts import render
  3. from django.http import HttpResponse
  4. from django.conf import settings
  5. # main imports
  6. import os
  7. import json
  8. import base64
  9. import random
  10. import numpy as np
  11. from datetime import datetime
  12. import pickle
  13. import time
  14. # expe imports
  15. from .expes.quest_plus import QuestPlus
  16. from .expes.quest_plus import psychometric_fun
  17. from .expes.run import run_quest_one_image
  18. # image processing imports
  19. import io
  20. from PIL import Image
  21. # module imports
  22. from .utils import api
  23. from .utils import functions
  24. from .utils.processing import crop_images
  25. from . import config as cfg
  26. def expe_list(request):
  27. # get all scenes from dataset
  28. scenes = api.get_scenes()
  29. # get list of experiences
  30. expes = cfg.expe_name_list
  31. return render(request, 'expe/expe_list.html', {'scenes': scenes, 'expes': expes})
  32. def indications(request):
  33. # get param
  34. expe_name = request.GET.get('expe')
  35. # expe parameters
  36. data = {
  37. 'expe_name': expe_name,
  38. 'question': cfg.expes_configuration[expe_name]['text']['question'],
  39. 'indication': cfg.expes_configuration[expe_name]['text']['indication']
  40. }
  41. return render(request, 'expe/expe_indications.html', data)
  42. # Create your views here.
  43. def expe(request):
  44. # get param
  45. expe_name = request.GET.get('expe')
  46. scene_name = request.GET.get('scene')
  47. # default filepath name
  48. filepath_img = ''
  49. # unique user ID during session (user can launch multiple exeperiences)
  50. if 'id' not in request.session:
  51. request.session['id'] = functions.uniqueID()
  52. # first time expe is launched add expe information
  53. if 'expe' not in request.session or expe_name != request.session.get('expe'):
  54. refresh_data(request, expe_name, scene_name)
  55. # refresh if scene_name changed
  56. if 'scene' not in request.session or scene_name != request.session.get('scene'):
  57. refresh_data(request, expe_name, scene_name)
  58. # create output folder for expe_result
  59. current_day = datetime.strftime(datetime.utcnow(), "%Y-%m-%d")
  60. results_folder = os.path.join(settings.MEDIA_ROOT, cfg.output_expe_folder.format(current_day))
  61. if not os.path.exists(results_folder):
  62. os.makedirs(results_folder)
  63. result_filename = expe_name + '_' + scene_name + '_' + request.session.get('id') + '_' + request.session.get('timestamp') +".csv"
  64. results_filepath = os.path.join(results_folder, result_filename)
  65. if not os.path.exists(results_filepath):
  66. output_file = open(results_filepath, 'w')
  67. functions.write_header_expe(output_file, expe_name)
  68. else:
  69. output_file = open(results_filepath, 'a')
  70. # create `quest` object if not exists
  71. models_folder = os.path.join(settings.MEDIA_ROOT, cfg.model_expe_folder.format(current_day))
  72. if not os.path.exists(models_folder):
  73. os.makedirs(models_folder)
  74. model_filename = result_filename.replace('.csv', '.obj')
  75. model_filepath = os.path.join(models_folder, model_filename)
  76. # run `quest` expe
  77. img_merge = run_quest_one_image(request, model_filepath, output_file)
  78. if not request.session.get('expe_finished'):
  79. # create output folder for tmp files if necessary
  80. tmp_folder = os.path.join(settings.MEDIA_ROOT, cfg.output_tmp_folder)
  81. if not os.path.exists(tmp_folder):
  82. os.makedirs(tmp_folder)
  83. # generate tmp merged image (pass as BytesIO was complicated..)
  84. # TODO : add crontab task to erase generated img
  85. filepath_img = os.path.join(tmp_folder, request.session.get('id') + '_' + scene_name + '' + expe_name + '.png')
  86. img_merge.save(filepath_img)
  87. else:
  88. # reinit session as default value
  89. del request.session['expe']
  90. del request.session['scene']
  91. del request.session['qualities']
  92. del request.session['timestamp']
  93. # expe parameters
  94. data = {
  95. 'expe_name': expe_name,
  96. 'img_merged_path': filepath_img,
  97. 'end_text': cfg.expes_configuration[expe_name]['text']['end_text']
  98. }
  99. return render(request, 'expe/expe.html', data)
  100. def refresh_data(request, expe_name, scene_name):
  101. request.session['expe'] = expe_name
  102. request.session['scene'] = scene_name
  103. request.session['expe_started'] = False
  104. request.session['expe_finished'] = False
  105. request.session['qualities'] = api.get_scene_qualities(scene_name)
  106. # update unique timestamp each time new experience is launched
  107. request.session['timestamp'] = datetime.strftime(datetime.utcnow(), "%Y-%m-%d_%Hh%Mm%Ss")
  108. # TODO : add in cache ref_image
  109. # get reference image
  110. #ref_image = api.get_image(scene_name, 'max')
  111. # save ref image as list (can't save python object)
  112. #request.session['ref_img'] = np.array(ref_image).tolist()