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.

190 lines
4.7 KiB

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