ioImg.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import re
  2. import time
  3. import cv2
  4. import numpy as np
  5. import struct
  6. def showHex(txt, i):
  7. time, unit, val = txt.strip().split(" ")
  8. time = int(time)
  9. for x in ["s", "ms", "us", "ns", "ps", "fs"]:
  10. if unit == x:
  11. break
  12. time /= 1000.0
  13. if re.match(r'[01]{32}', val, re.M):
  14. num = int(val, 2)
  15. print("{:6d} {:8.0f} ns {} {:08X}".format(i, time * 10**9, val, num))
  16. else:
  17. print("{:6d} {:8.0f} ns {}".format(i, time * 10**9, val))
  18. size = 224
  19. kernel = 5
  20. border = int(kernel/2)
  21. sizeWithBorder = size+2*border
  22. pixels = sizeWithBorder**2
  23. imageIn = np.zeros((sizeWithBorder,sizeWithBorder,1), np.uint8)
  24. imageOut = np.zeros((sizeWithBorder,sizeWithBorder,1), np.uint8)
  25. def setPixel(img, txt, i, input):
  26. time, unit, val = txt.strip().split(" ")
  27. time = int(time)
  28. for x in ["s", "ms", "us", "ns", "ps", "fs"]:
  29. if unit == x:
  30. break
  31. time /= 1000.0
  32. if re.match(r'[01]{32}', val, re.M):
  33. num = struct.unpack('!f',struct.pack('!i', int(val, 2)))[0]
  34. if num > 255:
  35. print(str(num) + ' > 255')
  36. num = 255
  37. x = i % sizeWithBorder
  38. y = int(i / sizeWithBorder) % sizeWithBorder
  39. img[y][x] = num
  40. def main():
  41. with open("vivado_project/vhdl-modules.sim/sim_1/behav/xsim/input.txt", "r") as inFile:
  42. print("Input:")
  43. i = 0
  44. for line in inFile:
  45. if i < 3+kernel**2 or i >= 3+kernel**2 + pixels:
  46. showHex(line, i)
  47. else:
  48. setPixel(imageIn, line, i-3, True)
  49. i += 1
  50. with open("vivado_project/vhdl-modules.sim/sim_1/behav/xsim/output.txt", "r") as outFile:
  51. print("Output:")
  52. i = 0
  53. for line in outFile:
  54. if i < 3 or i >= 3 + pixels:
  55. showHex(line, i)
  56. else:
  57. setPixel(imageOut, line, i-3, False)
  58. i += 1
  59. #while True:
  60. #print("\n\n\n\n")
  61. main()
  62. cv2.imshow('send', imageIn)
  63. cv2.imshow('recv', imageOut)
  64. cv2.waitKey(0)