You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.9 KiB

  1. import cv2
  2. import numpy as np
  3. import json
  4. from matplotlib import pyplot as plt
  5. import cv2
  6. import numpy as np
  7. import json
  8. import os
  9. from matplotlib import pyplot as plt
  10. import xml.etree.ElementTree as ET
  11. import cv2
  12. import numpy as np
  13. import os
  14. import json
  15. from pysolar.solar import *
  16. from datetime import datetime
  17. def calcAvg(img,locations_xml):
  18. locations_extracted = []
  19. loc_images = {}
  20. average_values = {}
  21. for i in range( len( locations_xml ) ):
  22. try:
  23. locations_extracted.append([])
  24. for j in range(4):
  25. locations_extracted[i].append(locations_xml[i][1][j].attrib)
  26. locations_extracted[i].append(locations_xml[i].attrib['occupied'])
  27. locations_extracted[i].append(locations_xml[i].attrib['id'])
  28. except Exception:
  29. print("xml corrupt!")
  30. return {}
  31. for col in range(len(img)):
  32. for pix in range(len(img[col])):
  33. if (img[col][pix] == [255,255,255]).all():
  34. img[col][pix] == [255,255,254]
  35. for i in range(len(locations_extracted)):
  36. temp = locations_extracted[i]
  37. pts = np.array([[int(temp[0]['x']),int(temp[0]['y'])],
  38. [int(temp[1]['x']),int(temp[1]['y'])],
  39. [int(temp[2]['x']),int(temp[2]['y'])],
  40. [int(temp[3]['x']),int(temp[3]['y'])]])
  41. rect = cv2.boundingRect(pts)
  42. x,y,w,h = rect
  43. croped = img[y:y+h, x:x+w].copy()
  44. pts = pts - pts.min(axis=0)
  45. mask = np.zeros(croped.shape[:2], np.uint8)
  46. cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
  47. dst = cv2.bitwise_and(croped, croped, mask=mask)
  48. bg = np.ones_like(croped, np.uint8)*255
  49. cv2.bitwise_not(bg,bg, mask=mask)
  50. dst2 = bg+ dst
  51. blurred = cv2.GaussianBlur(dst2,(5,5),3)
  52. edges = cv2.Canny(blurred,100,100)
  53. if not temp[4] in loc_images:
  54. loc_images[temp[4]] = {}
  55. loc_images[temp[4]][temp[5]] = edges
  56. for state in loc_images:
  57. average_values[state] = {}
  58. for lot in loc_images[state]:
  59. reps = 0
  60. for col in loc_images[state][lot]:
  61. for pix in col:
  62. if(pix == 255):
  63. reps += 1
  64. average_values[state][lot] = reps
  65. print (average_values)
  66. return average_values
  67. def generateAvg():
  68. dates = os.listdir('./PKLot/PKLot/PUCPR/Sunny')
  69. imgs = []
  70. averages={}
  71. back_file = open("backup.json","w")
  72. back_file.write("{")
  73. for i in range(len(dates)):
  74. imgs.append(os.listdir(os.path.join('./PKLot/PKLot/PUCPR/Sunny',dates[i])))
  75. for i in range(len(imgs)):
  76. for j in range(len(imgs[i])):
  77. if(imgs[i][j][-4:] == ".jpg"):
  78. try:
  79. img_location = os.path.join('./PKLot/PKLot/PUCPR/Sunny',dates[i],imgs[i][j])
  80. locs = ET.parse(img_location[:-4] + ".xml").getroot()
  81. print(img_location,end=" ")
  82. average = calcAvg(cv2.imread(img_location),locs)
  83. if not(average == {}):
  84. averages[img_location] = average
  85. back_file.write("'"+img_location + "':" + str(average))
  86. except Exception:
  87. continue
  88. back_file.write("}")
  89. js = json.dumps(averages)
  90. print(js)
  91. fp = open('data.json', 'w')
  92. fp.write(js)
  93. fp.close()
  94. generateAvg()