cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
vision.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 socket
30 import logging
31 
32 # Setting SO_RCVBUF
33 # Sets or gets the maximum socket receive buffer in bytes. The kernel doubles
34 # this value (to allow space for bookkeeping overhead) when it is set using
35 # setsockopt(2), and this doubled value is returned by getsockopt(2).
36 # The default value is set by the /proc/sys/net/core/rmem_default file, and
37 # the maximum allowed value is set by the /proc/sys/net/core/rmem_max file.
38 # The minimum (doubled) value for this option is 256.
39 recvBufSize = 0x1000000
40 real_buffer_size = 0
41 
42 def median_blur(input_array, total_size, fpga_ip, fpga_port, debug_level):
43  logging.basicConfig(level=debug_level)
44  bytesToSend = input_array.tostring()
45  # Create a UDP socket at client side
46  UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
47  previous_buffer_size = UDPClientSocket.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF)
48  UDPClientSocket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF, recvBufSize)
49  real_buffer_size = UDPClientSocket.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF)
50  if(real_buffer_size/2 != recvBufSize):
51  logging.warning("set SO_RCVBUF failed! got only: " +str(real_buffer_size/2) + "; trying to continue...")
52  BUFF_SIZE = 1024#65536
53  serverAddressPort = (fpga_ip, fpga_port)
54  cnt = 0;
55  while True:
56  logging.debug("Sending bytes: " + str(cnt*BUFF_SIZE) + " : " + str((cnt+1)*BUFF_SIZE-1))
57  UDPClientSocket.sendto(bytesToSend[cnt*BUFF_SIZE:(cnt+1)*BUFF_SIZE], serverAddressPort)
58  if ((cnt+1)*BUFF_SIZE >= total_size):
59  logging.debug("INFO: Reached size to sent")
60  break;
61  else:
62  cnt = cnt + 1
63  cnt = 0;
64  output_array = np.zeros((total_size,))
65  while True:
66  logging.debug("Receiving bytes: " + str(cnt*BUFF_SIZE) + " : " + str((cnt+1)*BUFF_SIZE-1))
67  msgFromServer = UDPClientSocket.recvfrom(BUFF_SIZE)
68  y = np.frombuffer(msgFromServer[0], dtype=input_array.dtype)
69  logging.debug(output_array[cnt*BUFF_SIZE:(cnt+1)*BUFF_SIZE-1].size)
70  output_array[cnt*BUFF_SIZE:(cnt+1)*BUFF_SIZE] = y
71  if ((cnt+1)*BUFF_SIZE >= total_size):
72  logging.debug("Reached size to receive")
73  break;
74  else:
75  cnt = cnt + 1
76  return output_array
77 
78 if __name__ == '__main__':
79  main(args)
int main()
Definition: tb_fmc.cpp:380
def median_blur(input_array, total_size, fpga_ip, fpga_port, debug_level)
Definition: vision.py:42