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.

162 lines
4.0 KiB

6 years ago
  1. import cv2
  2. import numpy as np
  3. import json
  4. from pysolar.solar import *
  5. from datetime import datetime
  6. from flask import Flask, request
  7. from flask_restful import Resource, Api, abort
  8. import base64
  9. import pickle
  10. from PIL import Image
  11. from matplotlib import pyplot as plt
  12. from io import BytesIO
  13. app = Flask(__name__)
  14. api = Api(app)
  15. def generateAvg(locs, img, avgs):
  16. time = datetime.strptime( "2019-04-27 17:52:00 -0300","%Y-%m-%d %H:%M:%S %z")
  17. altitude = int(get_altitude(39.9127938,32.8073577,time))
  18. loc_images = {}
  19. for i in locs:
  20. temp = locs[i]
  21. crop_img = img[temp["y1"]:temp["y2"], temp["x1"]:temp["x2"]]
  22. loc_images[i]=[crop_img]
  23. vals = {}
  24. if str(altitude) in avgs:
  25. vals = avgs[str(altitude)]
  26. else:
  27. for spot in loc_images:
  28. vals[spot] = loc_images[spot]
  29. for spot in loc_images:
  30. for col in range(len(vals[spot][0])):
  31. for pix in range(len(vals[spot][0][col])):
  32. vals[spot][0][col][pix] = [
  33. np.uint8((int(vals[spot][0][col][pix][0]) + int(loc_images[spot][0][col][pix][0]))/2),
  34. np.uint8((int(vals[spot][0][col][pix][1]) + int(loc_images[spot][0][col][pix][1]))/2),
  35. np.uint8((int(vals[spot][0][col][pix][2]) + int(loc_images[spot][0][col][pix][2]))/2)]
  36. for i in vals:
  37. vals[i] = vals[i][0].tolist()
  38. avgs[altitude] = vals
  39. return avgs
  40. def generateData(locs, img, avgs,show):
  41. time = datetime.strptime( "2019-04-27 17:52:00 -0300","%Y-%m-%d %H:%M:%S %z")
  42. altitude = int(get_altitude(39.9127938,32.8073577,time))
  43. loc_images = {}
  44. distances = {}
  45. for i in locs:
  46. temp = locs[i]
  47. crop_img = img[temp["y1"]:temp["y2"], temp["x1"]:temp["x2"]]
  48. loc_images[i]=[crop_img]
  49. vals = {}
  50. if str(altitude) in avgs:
  51. for spot in avgs[str(altitude)]:
  52. vals[spot] = np.array(avgs[str(altitude)][spot])
  53. else:
  54. for spot in loc_images:
  55. vals[spot] = loc_images[spot]
  56. for spot in loc_images:
  57. foo = np.zeros((len(vals[spot]),len(vals[spot][0])),dtype=int)
  58. distances[spot] = 0
  59. for col in range(len(vals[spot])):
  60. for pix in range(len(vals[spot][col])):
  61. vals[spot][col][pix] = [
  62. np.uint8(abs(int(vals[spot][col][pix][0]) - int(loc_images[spot][0][col][pix][0]))),
  63. np.uint8(abs(int(vals[spot][col][pix][1]) - int(loc_images[spot][0][col][pix][1]))),
  64. np.uint8(abs(int(vals[spot][col][pix][2]) - int(loc_images[spot][0][col][pix][2])))]
  65. distances[spot] += np.sum(vals[spot][col][pix])
  66. foo[col][pix] = np.max(vals[spot][col][pix])
  67. distances[spot] = int(distances[spot]/vals[spot].size)
  68. vals[spot] = foo
  69. if spot in show:
  70. plt.imshow(vals[spot], interpolation='nearest')
  71. #plt.show()
  72. return distances
  73. def im2str(im):
  74. imdata = pickle.dumps(im)
  75. return base64.b64encode(imdata).decode('ascii')
  76. plt.axis("off")
  77. with open("modules/databases/locations.json","r") as f:
  78. locs = json.loads(f.read())
  79. with open("modules/databases/park_data.json","r") as f:
  80. data = json.loads(f.read())
  81. cam = cv2.VideoCapture(5)
  82. if 1:
  83. ret,im = cam.read()
  84. data = generateAvg(locs,im,data)
  85. with open("modules/databases/park_data.json","w") as f:
  86. f.write(json.dumps(data,indent=2))
  87. class Empty(Resource):
  88. def get(self):
  89. image = cv2.imread("modules/lot.jpg")
  90. backup = image.copy()
  91. spot_data = generateData(locs,image,data,["0","1","2"])
  92. print(spot_data)
  93. best_spot = -1
  94. for loc in spot_data:
  95. spot_data[loc] = spot_data[loc] < 30
  96. color = (0,255*spot_data[loc],255*(not spot_data[loc]))
  97. cv2.rectangle(image,(locs[loc]["x1"],locs[loc]["y1"]),(locs[loc]["x2"],locs[loc]["y2"]),color,5)
  98. if spot_data[loc]:
  99. if best_spot == -1:
  100. best_spot = loc
  101. continue
  102. if locs[loc]["priority"] < locs[best_spot]["priority"]:
  103. best_spot = loc
  104. print(spot_data)
  105. if best_spot == -1:
  106. print("Sorry, no spot found :(")
  107. return
  108. else:
  109. print("Empty spot found at {}".format(int(best_spot) + 1))
  110. foo = locs[best_spot]
  111. crop_img = backup[foo["y1"]:foo["y2"], foo["x1"]:foo["x2"]].copy(order='C')
  112. crop_img = Image.fromarray(crop_img,"RGB")
  113. buffered = BytesIO()
  114. crop_img.save(buffered, format="JPEG")
  115. img = base64.b64encode(buffered.getvalue()).decode("ascii")
  116. return {"lat":foo["lat"], "lng":foo["lng"], "img":img}