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.

196 lines
5.0 KiB

  1. import xml.etree.ElementTree as ET
  2. import cv2
  3. import numpy as np
  4. import os
  5. import json
  6. from pysolar.solar import *
  7. from datetime import datetime
  8. def timeAverage():
  9. data_file = open("data.json","r")
  10. time_file = open("timely.json","w")
  11. averages = json.loads(data_file.read())
  12. timely = {}
  13. altitudes = {}
  14. for key in averages:
  15. date = key[37:-13]
  16. time = key[49:-4].replace("_",":")
  17. averages[key]["time"]=time
  18. averages[key]["date"]=date
  19. data_file.close()
  20. data_file = open("data.json","w")
  21. data_file.write(json.dumps(averages))
  22. for key in averages:
  23. time = datetime.strptime(averages[key]["date"] + " " + averages[key]["time"] + " -0300","%Y-%m-%d %H:%M:%S %z")
  24. altitude = int(get_altitude(-25.4269081,-49.3318036,time))
  25. altitudes[averages[key]["date"] + " " + averages[key]["time"] ] = altitude
  26. if not (altitude in timely):
  27. timely[altitude] = {}
  28. for spot in averages[key]:
  29. if(spot == "time" or spot == "date"):
  30. continue
  31. if not spot in timely[altitude]:
  32. timely[altitude][spot]=[]
  33. timely[altitude][spot].append({"r":averages[key][spot]["r"],"b":averages[key][spot]["b"],"g":averages[key][spot]["g"]})
  34. print(altitudes)
  35. for key in timely:
  36. for id in timely[key]:
  37. total = {"r":0,"g":0,"b":0}
  38. for i in range(len(timely[key][id])):
  39. for color in total:
  40. total[color] += timely[key][id][i][color]
  41. for color in total:
  42. total[color] = total[color] / len(timely[key][id])
  43. timely[key][id] = total
  44. print(timely)
  45. timely_json = json.dumps(timely)
  46. time_file.write(timely_json)
  47. data_file.close()
  48. time_file.close()
  49. def generateData(locations,image,show,averages):
  50. loc_images = {}
  51. average_values = {}
  52. average_total=[0,0,0]
  53. for col in range(len(image)):
  54. for pix in range(len(image[col])):
  55. if (image[col][pix] == [255,255,255]).all():
  56. image[col][pix] == [255,255,254]
  57. for i in locations:
  58. temp = locations[i]
  59. pts = np.array([[int(temp[0]['x']),int(temp[0]['y'])],
  60. [int(temp[1]['x']),int(temp[1]['y'])],
  61. [int(temp[2]['x']),int(temp[2]['y'])],
  62. [int(temp[3]['x']),int(temp[3]['y'])]])
  63. rect = cv2.boundingRect(pts)
  64. x,y,w,h = rect
  65. croped = image[y:y+h, x:x+w].copy()
  66. pts = pts - pts.min(axis=0)
  67. mask = np.zeros(croped.shape[:2], np.uint8)
  68. cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
  69. dst = cv2.bitwise_and(croped, croped, mask=mask)
  70. bg = np.ones_like(croped, np.uint8)*255
  71. cv2.bitwise_not(bg,bg, mask=mask)
  72. dst2 = bg+ dst
  73. split_len = len(dst2)//3
  74. splitted = [dst2[:split_len,:],dst2[split_len:split_len*2,:],dst2[split_len*2:,:]]
  75. loc_images[i]=[dst2]
  76. for lot in loc_images:
  77. average_values[lot] = []
  78. for i in range(1):
  79. diff_pix = 0
  80. reps = 0
  81. avg_rgb = [0,0,0]
  82. rgb = ["b","g","r"]
  83. for col in loc_images[lot][i]:
  84. for pix in col:
  85. if (pix == [255,255,255]).all():
  86. continue
  87. different = False
  88. print("[",end="")
  89. for j in range(3):
  90. print(abs(int(averages[lot][rgb[i]]-pix[i])) , end=",")
  91. if abs(averages[lot][rgb[i]]-pix[i]) > 59:
  92. different = True
  93. reps += 1
  94. diff_pix += different
  95. print("]",end=" ")
  96. print("")
  97. print("\n\n")
  98. average_values[lot] = (diff_pix/reps)*100
  99. if lot in show:
  100. cv2.imshow("a" , loc_images[lot][0])
  101. cv2.waitKey(0)
  102. return average_values
  103. def findSpot(img_loc,locations):
  104. empty = []
  105. timely_values = time_file = open("timely.json","r")
  106. timely_averages = json.loads(timely_values.read())
  107. img = cv2.imread(img_loc)
  108. date = img_loc[37:-13]
  109. time = img_loc[49:-4]
  110. time = time.replace("_",":")
  111. time = datetime.strptime(date + " " + time + " -0300","%Y-%m-%d %H:%M:%S %z")
  112. altitude = int(get_altitude(-25.4269081,-49.3318036,time))
  113. base_value = timely_averages[str(altitude)]
  114. print(altitude)
  115. averages = generateData(locations,img,[],base_value)
  116. for i in averages:
  117. print(i + " " + str(averages[i]))
  118. for i in locations:
  119. if(averages[i] > 32):
  120. color=[0,0,255]
  121. empty.append(i)
  122. else:
  123. color=[0,255,0]
  124. corners = [[],[]]
  125. for j in range(0,4):
  126. val2 = j+1
  127. if(val2 == 4):
  128. val2 = 0
  129. pt1 = (int(locations[i][j]["x"]),int(locations[i][j]["y"]))
  130. pt2 = (int(locations[i][val2]["x"]),int(locations[i][val2]["y"]))
  131. cv2.line(img,pt1,pt2,color)
  132. corners[0].append(int(locations[i][j]["x"]))
  133. corners[1].append(int(locations[i][j]["y"]))
  134. x1=min(corners[0][0],corners[0][1],corners[0][2],corners[0][3]);
  135. x2=max(corners[0][0],corners[0][1],corners[0][2],corners[0][3]);
  136. y1=min(corners[1][0],corners[1][1],corners[1][2],corners[1][3]);
  137. y2=max(corners[1][0],corners[1][1],corners[1][2],corners[1][3]);
  138. pt = (int((x1+x2)/2),int((y1+y2)/2))
  139. cv2.putText(img,str(i), pt, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)
  140. cv2.imshow("Parking Lot",img)
  141. cv2.waitKey(0)
  142. return empty
  143. location_file = open("locations.json","r")
  144. location_json = json.loads(location_file.read())
  145. empty_locs = findSpot('./PKLot/PKLot/PUCPR/Sunny/2012-09-11/2012-09-11_18_08_41.jpg',location_json)
  146. closest = 1000
  147. for i in empty_locs:
  148. print (location_json[i][4])
  149. if location_json[i][4] < closest:
  150. closest = int(i)
  151. print("The closest spot is " + str(closest))
  152. print("done")