ImgToConv2dBin.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import re
  2. import time
  3. import cv2
  4. import numpy as np
  5. import random
  6. import struct
  7. preamble = 0xE1E4C312
  8. moduleId = 0x4cd2e19c
  9. jobId = 0x12345678
  10. size = 224
  11. kernelSize = 5
  12. border = int(kernelSize/2)
  13. sizeWithBorder = size+2*border
  14. pixels = sizeWithBorder**2
  15. image = cv2.imread("Test_card.png")
  16. image = cv2.resize(image, (size, size))
  17. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  18. image = cv2.copyMakeBorder(image, border, border, border, border, cv2.BORDER_REPLICATE)
  19. image = np.ndarray((228,228), dtype=float)
  20. image.fill(0)
  21. for a in range(228):
  22. for b in range(228):
  23. image[a][b] = (a)*228+(b)
  24. kernel = np.array([
  25. [1, 4, 6, 4, 1],
  26. [4, 16, 24, 16, 4],
  27. [6, 24, 36, 24, 6],
  28. [4, 16, 24, 16, 4],
  29. [1, 4, 6, 4, 1]
  30. ], dtype=float) / 256
  31. kernel.fill(0)
  32. kernel[4][4] = 1
  33. print(kernel)
  34. cs = moduleId + jobId
  35. delay = 315
  36. with open("vivado_project/vhdl-modules.sim/sim_1/behav/xsim/input.txt", "w") as inFile:
  37. inFile.write("{:d} ns {:032b}\n".format(delay, preamble))
  38. inFile.write("{:d} ns {:032b}\n".format(delay, jobId))
  39. inFile.write("{:d} ns {:032b}\n".format(delay, moduleId))
  40. for i in range(kernelSize**2):
  41. y, x = int(i / kernelSize), i % kernelSize
  42. val = struct.unpack('!i',struct.pack('!f', kernel[y][x]))[0]
  43. inFile.write("{:d} ns {:032b}\n".format(delay, val))
  44. cs += val
  45. for i in range(pixels):
  46. y, x = int(i / sizeWithBorder), i % sizeWithBorder
  47. val = struct.unpack('!i',struct.pack('!f', image[y][x]))[0]
  48. inFile.write("{:d} ns {:032b}\n".format(delay, val))
  49. cs += val
  50. csc = 2**32 - cs % (2**32)
  51. if csc == 2**32:
  52. csc = 0
  53. inFile.write("{:d} ns {:032b}\n".format(delay, int(csc)))
  54. with open("vivado_project/vhdl-modules.sim/sim_1/behav/xsim/outputTimings.txt", "w") as inFile:
  55. for i in range(4 + pixels):
  56. if i%256 == 0:
  57. inFile.write("{:d} ns\n".format(6005))
  58. else:
  59. inFile.write("{:d} ns\n".format(315))