SHARE
TWEET

Untitled

a guest Apr 8th, 2020 220 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # In[18]:
  5. import numpy as np
  6. from PIL import Image
  7. import cv2
  8. import requests
  9. import numpy
  10. import pprint
  11.  
  12. URL = 'https://api.arstand-lab.ru'
  13. token = '69b8b503205c6a1ca49728f082686624d07b137d'
  14. HEADERS = {'Authorization': 'Token ' + token}
  15.  
  16.  
  17. def transform_triangle(image, start_points=[], triangle_end_points=[], size=300):
  18.     image = image.astype('uint8')
  19.  
  20.     skip = False
  21.     rows, cols = image.shape[:2]
  22.     pts1 = np.float32(start_points)
  23.     pts2 = np.float32(triangle_end_points)
  24.     print(start_points, triangle_end_points)
  25.     M = cv2.getAffineTransform(pts1, pts2)
  26.     transformed_image = cv2.warpAffine(image, M, (round(size), round(size)))
  27.     return transformed_image
  28.  
  29.  
  30. def get_faces_npz():
  31.     #res = requests.get(f'{URL}/api/0/task/get_task/pyramid_faces_morphing',
  32.      #                  headers=HEADERS)
  33.  
  34.     #with open('file.npz', 'wb') as f:
  35. #        f.write(res.content)
  36. #        data = numpy.load('file.npz')
  37.  
  38.     data = numpy.load('file.npz')
  39.     dict = {
  40.         "projector": data['projector'],
  41.         "side": data['side'],
  42.         "step": data['step'],
  43.         "ij_coords": data['ij_coords'],
  44.         "xyz_faces": data['xyz_faces'],
  45.         "face_img": data['face_img'],
  46.         "img_coords": data['img_coords']
  47.     }
  48.  
  49.     return dict
  50.  
  51.  
  52. def post_faces_projections_npz(mas):
  53.     numpy.savez('file1.npz', answer=mas)
  54.  
  55.     files = {'answer': open('file1.npz', 'rb')}
  56.     res = requests.post(f'{URL}/api/0/task/check_task/pyramid_faces_morphing',
  57.                         headers=HEADERS, files=files)
  58.     if res.ok:
  59.         print(res.content)
  60.         return True
  61.     else:
  62.         print(res.content)
  63.         return False
  64.  
  65.  
  66. def projection_coordinates(projector, side, step, ij_coords, xyz_faces, face_img, img_coords):
  67.     ij_coords = ij_coords.tolist()
  68.     xyz_faces = xyz_faces.tolist()
  69.     side = side.tolist()
  70.     xp, yp, zp = projector
  71.     images = []
  72.     for figure in xyz_faces:
  73.         figure_number = xyz_faces.index(figure)
  74.         cell_coordinate = [ij_coords[figure_number][0] * step, ij_coords[figure_number][1] * step]
  75.         i = 0
  76.         size = [300, 300]
  77.         combined = None
  78.         for polygon in figure:
  79.             side_projection = []
  80.             for coordinates in polygon:
  81.                 xv, yv, zv = coordinates
  82.                 xv = cell_coordinate[0] + side / 2 + xv
  83.                 yv = cell_coordinate[1] + side / 2 + yv
  84.                 coefficient = - zp / (zv - zp)
  85.                 xt = (coefficient*(xv - xp) + xp) - cell_coordinate[0]
  86.                 yt = (coefficient*(yv - yp) + yp) - cell_coordinate[1]
  87.                 zt = 0
  88.                 xt = 300 * xt / side
  89.                 yt = 300 * yt / side
  90.                 projection_point = [xt, yt]
  91.                 print(projection_point)
  92.                 side_projection.append(projection_point)
  93.             polygon_number = [xyz_faces.index(figure), figure.index(polygon)]
  94.             print('pn', polygon_number)
  95.             polygon_image = face_img[polygon_number[0]][polygon_number[1]]
  96.             vertices_position = img_coords[polygon_number[0]][polygon_number[1]]
  97.             print(vertices_position, side_projection, side)
  98.             cv2.imshow('im', polygon_image)
  99.             cv2.waitKey()
  100.             end_matrix = [[0, 0], [0, size[1]], [size[0], size[1]], [size[0], 0]]
  101.             a = i
  102.             if a > 3:
  103.                 a = a - 4
  104.             b = a - 1
  105.             if b < 0:
  106.                 b = 3
  107.  
  108.             cv2.imshow('im', polygon_image)
  109.             cv2.waitKey()
  110.             warped = transform_triangle(image=polygon_image, start_points=vertices_position,
  111.                                         triangle_end_points=[side_projection[0], end_matrix[b], end_matrix[a]],
  112.                                         size=300)
  113.  
  114.             cv2.imshow('im', warped)
  115.             cv2.waitKey()
  116.             if combined is None:
  117.                 combined = warped
  118.             else:
  119.                 combined = cv2.add(combined, warped)
  120.             i += 1
  121.         cv2.imshow('im', combined)
  122.         cv2.waitKey()
  123.         images.append(combined)
  124.     return images
  125.  
  126.  
  127. server_response = get_faces_npz()
  128. print(server_response['face_img'].shape)
  129.  
  130.  
  131. images = server_response['face_img'][0]
  132. for f in images:
  133.     cv2.imshow('im', f)
  134.     cv2.waitKey()
  135.  
  136. res = projection_coordinates(
  137.     server_response['projector'],
  138.     server_response['side'],
  139.     server_response['step'],
  140.     server_response['ij_coords'],
  141.     server_response['xyz_faces'],
  142.     server_response['face_img'],
  143.     server_response['img_coords']
  144. )
  145. print(res[0].shape)
  146. c = 0
  147. for im in res:
  148.     c += 1
  149.     cv2.imwrite(f'{c}.png', im)
  150. print(len(res))
  151.  
  152. #post_faces_projections_npz(res)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top