ioImg.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 not input:
  35. num = num/25
  36. if num > 255:
  37. num = 255
  38. x = i % sizeWithBorder
  39. y = int(i / sizeWithBorder) % sizeWithBorder
  40. img[y][x] = num
  41. def main():
  42. with open("vivado_project/vhdl-modules.sim/sim_1/behav/xsim/input.txt", "r") as inFile:
  43. #print("Input:")
  44. i = 0
  45. for line in inFile:
  46. if i < 3 or i >= 3 + pixels:
  47. showHex(line, i)
  48. else:
  49. setPixel(imageIn, line, i-3, True)
  50. i += 1
  51. with open("vivado_project/vhdl-modules.sim/sim_1/behav/xsim/output.txt", "r") as outFile:
  52. #print("Output:")
  53. i = 0
  54. for line in outFile:
  55. if i < 3 or i >= 3 + pixels:
  56. showHex(line, i)
  57. else:
  58. setPixel(imageOut, line, i-3, False)
  59. i += 1
  60. #while True:
  61. #print("\n\n\n\n")
  62. main()
  63. cv2.imshow('send', imageIn)
  64. cv2.imshow('recv', imageOut)
  65. cv2.waitKey(0)