cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
test_median_blur_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/median_blur/languages/python/build"
31 #sys.path.append(trieres_lib)
32 
33 #import _trieres_median_blur_numpi
34 from trieres import *
35 
36 # size of image to be processed on fpga (the bitstream should be already fixed to this)
37 height = width = 256
38 
39 num_frame = 1
40 
41 # path
42 image_in_filename = os.environ['cFpRootDir'] + "ROLE/vision/hls/harris/test/512x512.png"
43 image_out_filename = image_in_filename + "_fpga_points_out_frame_" + str(num_frame) + ".png"
44 
45 ROI = False
46 
47 
48 def crop_square(img, size, interpolation=cv2.INTER_AREA):
49  h, w = img.shape[:2]
50  print("h="+str(h))
51  print("w="+str(w))
52  min_size = np.amin([h,w])
53  print("min_size="+str(min_size))
54 
55  # Centralize and crop
56  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)]
57  y1=10
58  y2=y1+100
59  x1=10
60  x2=x1+100
61  roi = crop_img[y1:y2, x1:x2]
62  resized = cv2.resize(roi , (size, size), interpolation=interpolation)
63 
64  return resized
65 
66 
67 def patch_square(crop_img, img, interpolation=cv2.INTER_AREA):
68  h, w = img.shape[:2]
69  print("h="+str(h))
70  print("w="+str(w))
71  min_size = np.amin([h,w])
72  print("min_size="+str(min_size))
73 
74  # Centralize and patch
75  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
76 
77  return img
78 
79 
80 
81 
82 
83 
84 for i in range(1):
85  # Reading an image in unchanged mode
86  image = cv2.imread(image_in_filename, cv2.IMREAD_UNCHANGED)
87 
88  # Converting to grayscale
89  image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
90 
91  # Adjusting the image file if needed
92  if ((image.shape[0] != height) or (image.shape[1] != width)):
93  if ROI:
94  print("WARNING: An image of size [", height , " x ", width, "] will be cropped from input image of size [", image.shape[0] , " x ", image.shape[1] , "]")
95  image_big = image
96  image = crop_square(image_big, width, interpolation = cv2.INTER_AREA)
97  else:
98  print("WARNING: The image was resized from [", image.shape[0] , " x ", image.shape[1] , "] to [", height , " x ", width, "]")
99  dim = (width, height)
100  image = cv2.resize(image, dim, interpolation = cv2.INTER_LINEAR)
101 
102  # Flattening the image from 2D to 1D
103  image = image.flatten()
104 
105  total_size = height * width
106 
107  input_array = image
108 
109 #for i in range(100):
110 
111  #output_array = _trieres_median_blur_numpi.median_blur(input_array, total_size, "10.12.200.73", "2718")
112  output_array = trieres.vision.median_blur(input_array, total_size, "127.0.0.1", 2718)
113 
114  # Convert 1D array to a 2D numpy array of 2 rows and 3 columns
115  output_array_2d = np.reshape(output_array, (height, width))
116 
117  #if ROI:
118  # output_array_2d = patch_square(output_array_2d, image_big, interpolation = cv2.INTER_AREA)
119 
120 cv2.imwrite(image_out_filename, output_array_2d)
121 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)