ImageDB_HDD.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from . import ImageDB
  2. import os, json, functools, datetime, imageio
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import miam.html.generator as MHTML
  6. import miam.image.Image as MIMG
  7. import miam.histogram.Histogram as MHIST
  8. import miam.processing.TMO_CCTF
  9. class ImageDB_HDD(ImageDB.ImageDB):
  10. """description of class"""
  11. def __init__(self,name=None, csvFile=None, jsonConfigFile = None):
  12. super().__init__(name, csvFile, jsonConfigFile)
  13. def check(self,uri2file=None):
  14. checked = []
  15. missingItems = []
  16. missingFiles = []
  17. # load config
  18. with open(self.jsonConfigFile) as json_file: config = json.load(json_file)
  19. path = config["imagePATH"]
  20. # for all item in db
  21. for item in self.db:
  22. itemOk = list(map(lambda x : os.path.isfile(path+x), item))
  23. ok = functools.reduce(lambda x,y : x and y,itemOk)
  24. if ok:
  25. checked.append(item)
  26. else:
  27. missingItems.append(item)
  28. for i, fileOk in enumerate(itemOk):
  29. if not fileOk: missingFiles.append(item[i])
  30. # results
  31. path = self.csvFileName[0: len(self.csvFileName) - len(self.csvFileName.split("/")[-1])]
  32. np.savetxt(path+name+"_"+"missingItems.csv",missingItems,delimiter=";",fmt="%s",encoding="utf8")
  33. np.savetxt(path+name+"_"+"missingImages.csv",missingFiles,delimiter=";",fmt="%s",encoding="utf8")
  34. self.db = np.asarray(checked)
  35. def buildHTMLPage(self):
  36. # get date
  37. todayString = datetime.date.today().strftime("%Y/%m/%d")
  38. # load config
  39. with open(self.jsonConfigFile) as json_file: config = json.load(json_file)
  40. path = config["imagePATH"]
  41. # <!DOCTYPE html>
  42. # <html>
  43. # <head><title>HDR databse</title></head>
  44. # <BODY>
  45. # <H1>HDR DataBase</H1>
  46. # </BODY>
  47. # </html>
  48. head = MHTML.generator.tag('head', content=MHTML.generator.tag('title', content='hdr database'))
  49. h1 = MHTML.generator.tag('h1', content='HDR DATABASE')
  50. date = MHTML.generator.tag('h3', content='Rémi Cozot - '+ todayString)
  51. pageBody =''
  52. # for all items
  53. for item in self.db:
  54. # load HDR image
  55. hdr = MIMG.Image.readImage(path+item[0]).resize((None,480)).removeZeros().removeZeros(0.5)
  56. # basic tone mapping
  57. hdrCCTF = hdr.process(miam.processing.TMO_CCTF.TMO_CCTF(), function='sRGB')
  58. # read Hand Tone Mapped image
  59. image = MIMG.Image.readImage(path+item[-1]).resize((None,480))
  60. # compute Y(HDR) and L(SDR) histograms
  61. hdrhistoY = MHIST.Histogram.build(hdr,MIMG.channel.channel.Y, nbBins=50)
  62. imagehistoL = MHIST.Histogram.build(image,MIMG.channel.channel.L, nbBins=50)
  63. # create name
  64. imageName = (item[-1].split('/')[-1]).split(".")[0]
  65. # create and save histogram images
  66. fig, ax = plt.subplots()
  67. hdrhistoY.plot(ax)
  68. plt.show(block=False)
  69. plt.savefig ( "../HTML/images/"+"HDR_"+imageName+"_YHIST.png" )
  70. plt.close(fig)
  71. fig, ax = plt.subplots()
  72. imagehistoL.plot(ax)
  73. plt.show(block=False)
  74. plt.savefig ( "../HTML/images/"+"SDR_"+imageName+"_LHIST.png" )
  75. plt.close(fig)
  76. # save CCTF and HTM images
  77. imageio.imwrite('../HTML/images/'+"HTM_"+imageName+".jpg", (image.colorData*256).astype(np.uint8))
  78. imageio.imwrite('../HTML/images/'+"CCTF_"+imageName+".jpg", (hdrCCTF.colorData*256).astype(np.uint8))
  79. # uri for web page
  80. name_CCTF_image = "images/"+"CCTF_"+imageName+".jpg"
  81. name_HTM_image = "images/"+"HTM_"+imageName+".jpg"
  82. name_HDR_Yhist = "images/"+"HDR_"+imageName+"_YHIST.png"
  83. name_SDR_Lhist = "images/"+"SDR_"+imageName+"_LHIST.png"
  84. # create <img> table
  85. pageBody = pageBody + MHTML.generator.tag('h2', imageName)+'\n' + \
  86. MHTML.generator.table([[ MHTML.generator.imgTagWidth(attributeValue=[name_CCTF_image,'50%']),MHTML.generator.imgTagWidth(attributeValue=[name_HDR_Yhist,'50%'])],
  87. [MHTML.generator.imgTagWidth(attributeValue=[name_HTM_image,'50%']),MHTML.generator.imgTagWidth(attributeValue=[name_SDR_Lhist,'50%'])]])+ '\n'
  88. print(">",imageName)
  89. # end loop
  90. all = MHTML.generator.tag('html', content= head + '\n' + \
  91. MHTML.generator.tag('body', content= h1 + '\n' + date + '\n' + \
  92. pageBody
  93. ))
  94. fileHTML = open("../HTML/hdr-database.html","w")
  95. fileHTML.write(all)
  96. fileHTML.close()