cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
test_warp_transform_numpi.py
Go to the documentation of this file.
1 # *****************************************************************************
2 # * cloudFPGA
3 # * Copyright 2016 -- 2022 IBM Corporation
4 # * Licensed under the Apache License, Version 2.0 (the "License");
5 # * you may not use this file except in compliance with the License.
6 # * You may obtain a copy of the License at
7 # *
8 # * http://www.apache.org/licenses/LICENSE-2.0
9 # *
10 # * Unless required by applicable law or agreed to in writing, software
11 # * distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
15 # *----------------------------------------------------------------------------
16 
17 
24 
25 import sys
26 import os
27 import numpy as np
28 import cv2
29 
30 trieres_lib=os.environ['cFpRootDir'] + "HOST/vision/warp_transform/languages/python/build"
31 sys.path.append(trieres_lib)
32 
33 import _trieres_warp_transform_numpi
34 
35 # size of image to be processed on fpga (the bitstream should be already fixed to this)
36 height = width = 256
37 
38 num_frame = 1
39 
40 # path
41 image_in_filename = os.environ['cFpRootDir'] + "ROLE/vision/hls/warp_transform/test/512x512.png"
42 image_out_filename = image_in_filename + "_fpga_points_out_frame_" + str(num_frame) + ".png"
43 
44 ROI = False
45 
46 
47 def crop_square(img, size, interpolation=cv2.INTER_AREA):
48  h, w = img.shape[:2]
49  print("h="+str(h))
50  print("w="+str(w))
51  min_size = np.amin([h,w])
52  print("min_size="+str(min_size))
53 
54  # Centralize and crop
55  crop_img = img[int(h/2-min_size/2):int(h/2+min_size/2), int(w/2-min_size/2):int(w/2+min_size/2)]
56  y1=10
57  y2=y1+100
58  x1=10
59  x2=x1+100
60  roi = crop_img[y1:y2, x1:x2]
61  resized = cv2.resize(roi , (size, size), interpolation=interpolation)
62 
63  return resized
64 
65 
66 def patch_square(crop_img, img, interpolation=cv2.INTER_AREA):
67  h, w = img.shape[:2]
68  print("h="+str(h))
69  print("w="+str(w))
70  min_size = np.amin([h,w])
71  print("min_size="+str(min_size))
72 
73  # Centralize and patch
74  img[int(h/2-min_size/2):int(h/2+min_size/2), int(w/2-min_size/2):int(w/2+min_size/2)] = crop_img
75 
76  return img
77 
78 
79 
80 
81 
82 
83 for i in range(1):
84  # Reading an image in unchanged mode
85  image = cv2.imread(image_in_filename, cv2.IMREAD_UNCHANGED)
86 
87  # Converting to grayscale
88  image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
89 
90  # Adjusting the image file if needed
91  if ((image.shape[0] != height) or (image.shape[1] != width)):
92  if ROI:
93  print("WARNING: An image of size [", height , " x ", width, "] will be cropped from input image of size [", image.shape[0] , " x ", image.shape[1] , "]")
94  image_big = image
95  image = crop_square(image_big, width, interpolation = cv2.INTER_AREA)
96  else:
97  print("WARNING: The image was resized from [", image.shape[0] , " x ", image.shape[1] , "] to [", height , " x ", width, "]")
98  dim = (width, height)
99  image = cv2.resize(image, dim, interpolation = cv2.INTER_LINEAR)
100 
101  # Flattening the image from 2D to 1D
102  image = image.flatten()
103 
104  total_size = height * width
105 
106  input_array = image
107 
108 #for i in range(100):
109 
110  output_array = _trieres_warp_transform_numpi.warp_transform(input_array, total_size, "10.12.200.122", "2718")
111 
112  # Convert 1D array to a 2D numpy array of 2 rows and 3 columns
113  output_array_2d = np.reshape(output_array, (height, width))
114 
115  #if ROI:
116  # output_array_2d = patch_square(output_array_2d, image_big, interpolation = cv2.INTER_AREA)
117 
118 cv2.imwrite(image_out_filename, output_array_2d)
119 print("INFO: the output file is saved at : " + image_out_filename)
def crop_square(img, size, interpolation=cv2.INTER_AREA)
def patch_square(crop_img, img, interpolation=cv2.INTER_AREA)