cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
test_median_blur.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 import logging
30 
31 trieres_lib=os.environ['cFpRootDir'] + "HOST/"
32 sys.path.append(trieres_lib)
33 
34 from trieres import vision
35 
36 config_file=os.environ['cFpRootDir'] + "HOST/vision/median_blur/languages/cplusplus/include/config.h"
37 
38 with open(config_file) as cfg:
39  for line in cfg:
40  if "#define FRAME_WIDTH" in line:
41  width = int(line.split()[2])
42  elif "#define FRAME_HEIGHT" in line:
43  height = int(line.split()[2])
44 try:
45  print("Found in " + config_file + ": width = "+str(width) + ", height = "+str(height))
46  total_size = height * width
47 except:
48  print("Coudln't find FRAME_WIDTH or FRAME_HEIGHT in "+ config_file + ". Aborting...")
49  exit(0)
50 
51 num_frame = 1
52 
53 # path
54 image_in_filename = os.environ['cFpRootDir'] + "ROLE/vision/hls/harris/test/512x512.png"
55 image_out_filename = image_in_filename + "_fpga_points_out_frame_" + str(num_frame) + ".png"
56 
57 ROI = False
58 
59 
60 def crop_square(img, size, interpolation=cv2.INTER_AREA):
61  h, w = img.shape[:2]
62  print("h="+str(h))
63  print("w="+str(w))
64  min_size = np.amin([h,w])
65  print("min_size="+str(min_size))
66 
67  # Centralize and crop
68  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)]
69  y1=10
70  y2=y1+100
71  x1=10
72  x2=x1+100
73  roi = crop_img[y1:y2, x1:x2]
74  resized = cv2.resize(roi , (size, size), interpolation=interpolation)
75 
76  return resized
77 
78 
79 def patch_square(crop_img, img, interpolation=cv2.INTER_AREA):
80  h, w = img.shape[:2]
81  print("h="+str(h))
82  print("w="+str(w))
83  min_size = np.amin([h,w])
84  print("min_size="+str(min_size))
85 
86  # Centralize and patch
87  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
88 
89  return img
90 
91 
92 
93 
94 for i in range(1):
95  # Reading an image in unchanged mode
96  image = cv2.imread(image_in_filename, cv2.IMREAD_UNCHANGED)
97 
98  # Converting to grayscale
99  image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
100 
101  # Adjusting the image file if needed
102  if ((image.shape[0] != height) or (image.shape[1] != width)):
103  if ROI:
104  print("WARNING: An image of size [", height , " x ", width, "] will be cropped from input image of size [", image.shape[0] , " x ", image.shape[1] , "]")
105  image_big = image
106  image = crop_square(image_big, width, interpolation = cv2.INTER_AREA)
107  else:
108  print("WARNING: The image was resized from [", image.shape[0] , " x ", image.shape[1] , "] to [", height , " x ", width, "]")
109  dim = (width, height)
110  image = cv2.resize(image, dim, interpolation = cv2.INTER_LINEAR)
111 
112  # Flattening the image from 2D to 1D
113  image = image.flatten()
114 
115  total_size = height * width
116 
117  input_array = image
118 
119 #for i in range(100):
120 
121  output_array = vision.median_blur(input_array, total_size, "10.12.200.66", 2718, logging.ERROR)
122 
123  # Convert 1D array to a 2D numpy array of 2 rows and 3 columns
124  output_array_2d = np.reshape(output_array, (height, width))
125 
126  #if ROI:
127  # output_array_2d = patch_square(output_array_2d, image_big, interpolation = cv2.INTER_AREA)
128 
129 cv2.imwrite(image_out_filename, output_array_2d)
130 print("INFO: the output file is saved at : " + image_out_filename)
def patch_square(crop_img, img, interpolation=cv2.INTER_AREA)
def crop_square(img, size, interpolation=cv2.INTER_AREA)
def median_blur(input_array, total_size, fpga_ip, fpga_port, debug_level)
Definition: vision.py:42