Advertisement
MaxObznyi

Untitled

Nov 7th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. #Math begin
  2. def get_dist(a, b):
  3. return sqrt((a["x"] - b["x"]) * (a["x"] - b["x"]) + (a["y"] - b["y"]) * (a["y"] - b["y"]))
  4.  
  5. def get_good_angle(a, b):
  6. A = {"x": b["x"] - a["x"], "y": b["y"] - a["y"]}
  7. B = {"x": 1, "y": 0}
  8. C = {"x": 0, "y": 0}
  9. tmp = (A["x"] * B["x"] + A["y"]*B["y"]) / get_dist(A, C)
  10. if tmp < -1:
  11. tmp = -1
  12. if tmp > 1:
  13. tmp = 1
  14. angle = acos(tmp)
  15. if A["y"] < 0:
  16. angle = -1 * angle
  17. return angle
  18. #Math end
  19.  
  20. #Coordinates transformation begin
  21. def is_good(x, y):
  22. return x > l and y > l and x + l < SIZE and y + l < SIZE
  23.  
  24. def get_good(x, y):
  25. x = x * 4032 // 800 + 984
  26. y = y * 3024 // 600 + 1488
  27. return x, y
  28. #Coordinates transformation end
  29.  
  30. def solve_dataset(old_im, obj):
  31.  
  32. UPLOAD_FOLDER_DATASET = '/tmp/kek/'
  33. ZIP_DATASET = '/tmp/photoset.zip'
  34. IMAGE_URL_DATASET = '/tmp/lol.jpg'
  35. SIZE = 6000
  36.  
  37. #Creating temporary folder for images
  38. if not os.path.exists(UPLOAD_FOLDER_DATASET):
  39. os.makedirs(UPLOAD_FOLDER_DATASET)
  40.  
  41. #Calculating drone parameters
  42. angle_x = obj["angle_x"]
  43. angle_y = obj["angle_y"]
  44. h = obj["h"]
  45. points = obj["points"]
  46. overlapping = obj["overlapping"]
  47. lx = 2 * h * tan(angle_x / 360 * 3.1415926)
  48. ly = 2 * h * tan(angle_y / 360 * 3.1415926)
  49. res = []
  50. base = {"lat": 50.015781, "lng": 36.222751} #Ivriy's house
  51. phi = base["lat"] / 180 * 3.1415926
  52. dpx = 111.321*cos(phi)
  53. dpy = 111.143
  54.  
  55. #Delete the previous version of archive(if exists)
  56. a = 0
  57. try:
  58. zipfile.ZipFile(ZIP_DATASET, 'w')
  59. except:
  60. a = 1
  61. try:
  62. os.remove(ZIP_DATASET)
  63. except:
  64. a = 2
  65. old_size = old_im.size
  66.  
  67. #Creating map image
  68. new_size = (6000, 6000)
  69. new_im = Image.new("RGB", new_size)
  70. new_im.paste(old_im, ((new_size[0]-old_size[0])//2,
  71. (new_size[1]-old_size[1])//2))
  72.  
  73.  
  74. cur = 0
  75. for point in points:
  76. point["x"], point["y"] = get_good(point["x"], point["y"])
  77. cur = points[0].copy()
  78. cur_i = 0
  79. nd = 0
  80. photo_id = 0
  81. images = []
  82. while cur_i + 1 < len(points):
  83. if get_dist(points[cur_i + 1], cur) < nd:
  84. nd = 0
  85. cur["x"] = points[cur_i+1]["x"]
  86. cur["y"] = points[cur_i+1]["y"]
  87. angle = get_good_angle(points[cur_i], points[cur_i + 1])
  88.  
  89. rotate_angle = (90 + int(angle / 3.1415926 * 180) + 360) % 360
  90. phi = -rotate_angle / 180 * 3.1415926
  91. cur["x"] -= 3000
  92. cur["y"] -= 3000
  93.  
  94. x = int(cur["x"] * cos(phi) - cur["y"] * sin(phi))
  95. y = int(cur["x"] * sin(phi) + cur["y"] * cos(phi))
  96. x += 3000
  97. y += 3000
  98. cur["x"] += 3000
  99. cur["y"] += 3000
  100.  
  101. #img = Image.open(IMAGE_URL_DATASET)
  102. img = new_im.copy()
  103. tmp = str(photo_id)
  104. while len(tmp) < 4:
  105. tmp = '0' + tmp
  106. img.rotate(rotate_angle).crop((x - lx//2, y - lx//2, x + ly//2, y + ly//2)).save(UPLOAD_FOLDER_DATASET + 'result_' + tmp + '.jpg')
  107. photo_id += 1
  108. res.append({"alt_rel": h, "yaw": 90+angle / 3.1415926 * 180, "pitch": 0, "roll": 0, "lng": cur["x"] / 1000 / dpx + base["lng"], "lat": base["lat"] - cur["y"] / 1000 / dpy})
  109. res[-1]["lat"] *= 1e7
  110. res[-1]["lng"] *= 1e7
  111. res[-1]["lat"] = int(res[-1]["lat"])
  112. res[-1]["lng"] = int(res[-1]["lng"])
  113.  
  114. cur_i = cur_i + 1
  115. if cur_i + 1 == len(points):
  116. break
  117.  
  118. else:
  119. angle = get_good_angle(points[cur_i], points[cur_i + 1])
  120. cur["x"] += nd * cos(angle)
  121. cur["y"] += nd * sin(angle)
  122. rotate_angle = (90 + int(angle / 3.1415926 * 180) + 360) % 360
  123. phi = -rotate_angle / 180 * 3.1415926
  124.  
  125. cur["x"] -= 3000
  126. cur["y"] -= 3000
  127. x = int(cur["x"] * cos(phi) - cur["y"] * sin(phi))
  128. y = int(cur["x"] * sin(phi) + cur["y"] * cos(phi))
  129. x += 3000
  130. y += 3000
  131. cur["x"] += 3000
  132. cur["y"] += 3000
  133.  
  134. img = new_im.copy()
  135. res.append({"alt_rel": h, "yaw": 90+angle / 3.1415926 * 180, "pitch": 0, "roll": 0, "lng": cur["x"] / 1000 / dpx + base["lng"], "lat": base["lat"] - cur["y"] / 1000 / dpy})
  136. res[-1]["lat"] *= 1e7
  137. res[-1]["lng"] *= 1e7
  138. res[-1]["lat"] = int(res[-1]["lat"])
  139. res[-1]["lng"] = int(res[-1]["lng"])
  140. tmp = str(photo_id)
  141. while len(tmp) < 4:
  142. tmp = '0' + tmp
  143. img.rotate(rotate_angle).crop((x - lx//2, y - lx//2, x + ly//2, y + ly//2)).save(UPLOAD_FOLDER_DATASET + 'result_' + tmp + '.jpg')
  144. photo_id += 1
  145. nd = lx * (1 - overlapping)
  146. #images[0].show()
  147. for i in range(0, len(res)):
  148. new_pos = {"meta": {"type": "CAMERA_FEEDBACK"}, "data": res[i].copy()}
  149. res[i] = new_pos.copy()
  150. with open(UPLOAD_FOLDER_DATASET + 'logs.json', 'w') as outfile:
  151. for i in range(0, len(res)):
  152. print(json.dumps(res[i]), file=outfile)
  153. with ZipFile(ZIP_DATASET, 'a') as zipObj:
  154. for folderName, subfolders, filenames in os.walk(top, topdown=False):
  155. for filename in filenames:
  156. filePath = os.path.join(folderName, filename)
  157. zipObj.write(filePath, '/' + filename)
  158. top = UPLOAD_FOLDER_DATASET
  159. for root, dirs, files in os.walk(top, topdown=False):
  160. for name in files:
  161. os.remove(os.path.join(top, name))
  162. for name in dirs:
  163. os.rmdir(os.path.join(top, name))
  164. res = ZipFile(ZIP_DATASET, 'a').copy()
  165. return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement