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.

145 lines
3.4 KiB

  1. import cv2
  2. import numpy as np
  3. import json
  4. from pysolar.solar import *
  5. from datetime import datetime
  6. from PIL import Image
  7. from matplotlib import pyplot as plt
  8. def generateAvg(locs, img, avgs):
  9. time = datetime.strptime( "2019-04-27 17:52:00 -0300","%Y-%m-%d %H:%M:%S %z")
  10. altitude = int(get_altitude(39.9127938,32.8073577,time))
  11. loc_images = {}
  12. for i in locs:
  13. temp = locs[i]
  14. crop_img = img[temp["y1"]:temp["y2"], temp["x1"]:temp["x2"]]
  15. loc_images[i]=[crop_img]
  16. vals = {}
  17. if str(altitude) in avgs:
  18. vals = avgs[str(altitude)]
  19. else:
  20. for spot in loc_images:
  21. vals[spot] = loc_images[spot]
  22. for spot in loc_images:
  23. for col in range(len(vals[spot][0])):
  24. for pix in range(len(vals[spot][0][col])):
  25. vals[spot][0][col][pix] = [
  26. np.uint8((int(vals[spot][0][col][pix][0]) + int(loc_images[spot][0][col][pix][0]))/2),
  27. np.uint8((int(vals[spot][0][col][pix][1]) + int(loc_images[spot][0][col][pix][1]))/2),
  28. np.uint8((int(vals[spot][0][col][pix][2]) + int(loc_images[spot][0][col][pix][2]))/2)]
  29. for i in vals:
  30. vals[i] = vals[i][0].tolist()
  31. avgs[altitude] = vals
  32. return avgs
  33. def generateData(locs, img, avgs,show):
  34. time = datetime.strptime( "2019-04-27 17:52:00 -0300","%Y-%m-%d %H:%M:%S %z")
  35. altitude = int(get_altitude(39.9127938,32.8073577,time))
  36. loc_images = {}
  37. distances = {}
  38. for i in locs:
  39. temp = locs[i]
  40. crop_img = img[temp["y1"]:temp["y2"], temp["x1"]:temp["x2"]]
  41. loc_images[i]=[crop_img]
  42. vals = {}
  43. if str(altitude) in avgs:
  44. for spot in avgs[str(altitude)]:
  45. vals[spot] = np.array(avgs[str(altitude)][spot])
  46. else:
  47. for spot in loc_images:
  48. vals[spot] = loc_images[spot]
  49. for spot in loc_images:
  50. foo = np.zeros((len(vals[spot]),len(vals[spot][0])),dtype=int)
  51. distances[spot] = 0
  52. for col in range(len(vals[spot])):
  53. for pix in range(len(vals[spot][col])):
  54. vals[spot][col][pix] = [
  55. np.uint8(abs(int(vals[spot][col][pix][0]) - int(loc_images[spot][0][col][pix][0]))),
  56. np.uint8(abs(int(vals[spot][col][pix][1]) - int(loc_images[spot][0][col][pix][1]))),
  57. np.uint8(abs(int(vals[spot][col][pix][2]) - int(loc_images[spot][0][col][pix][2])))]
  58. distances[spot] += np.sum(vals[spot][col][pix])
  59. foo[col][pix] = np.max(vals[spot][col][pix])
  60. distances[spot] = int(distances[spot]/vals[spot].size)
  61. vals[spot] = foo
  62. if spot in show:
  63. plt.imshow(vals[spot], interpolation='nearest')
  64. plt.show()
  65. return distances
  66. plt.axis("off")
  67. with open("databases/locations.json","r") as f:
  68. locs = json.loads(f.read())
  69. with open("databases/park_data.json","r") as f:
  70. data = json.loads(f.read())
  71. while 0:
  72. data = generateAvg(locs,cv2.imread("parking_images/8.jpg"),data)
  73. with open("databases/park_data.json","w") as f:
  74. f.write(json.dumps(data,indent=4))
  75. exit(0)
  76. image = cv2.imread("parking_images/7.jpg")
  77. spot_data = generateData(locs,image,data,["0","1","2"])
  78. print(spot_data)
  79. best_spot = -1
  80. for loc in spot_data:
  81. spot_data[loc] = spot_data[loc] < 30
  82. color = (0,255*spot_data[loc],255*(not spot_data[loc]))
  83. cv2.rectangle(image,(locs[loc]["x1"],locs[loc]["y1"]),(locs[loc]["x2"],locs[loc]["y2"]),color,5)
  84. if spot_data[loc]:
  85. if best_spot == -1:
  86. best_spot = loc
  87. continue
  88. if locs[loc]["priority"] < locs[best_spot]["priority"]:
  89. best_spot = loc
  90. print(spot_data)
  91. if best_spot == -1:
  92. print("Sorry, no spot found :(")
  93. else:
  94. print("Empty spot found at {}".format(int(best_spot) + 1))
  95. plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  96. plt.show()