Advertisement
yclee126

donttap reader

Feb 14th, 2021 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # https://youtu.be/vNIuTMhAp_Q
  4.  
  5. import ctypes
  6. ctypes.windll.user32.SetProcessDPIAware()
  7.  
  8. import cv2
  9. import numpy as np
  10.  
  11.  
  12. # osu window
  13. or_w, or_h = 512, 384
  14. hist = np.zeros((or_h, or_w, 1))
  15. coord_hist = {}
  16.  
  17. cap = cv2.VideoCapture('frenzy.mp4')
  18. f = open('osu.txt', 'w')
  19. buffer = ''
  20. frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
  21.  
  22.  
  23. # main loop
  24. init = True
  25. win = 'img'
  26. cv2.namedWindow(win)
  27. while True:
  28. cur_pos = cap.get(cv2.CAP_PROP_POS_FRAMES)
  29. cur_time = cap.get(cv2.CAP_PROP_POS_MSEC)
  30. print('%d/%d' % (cur_pos, frames), end='\r')
  31. ret, img = cap.read()
  32. if cur_pos < 228:
  33. continue
  34. if not ret or cv2.getWindowProperty(win, 0) < 0 or cur_pos > 2100:
  35. break
  36.  
  37. img = cv2.resize(img, None, fx=0.8, fy=0.8) # to make things easier
  38. img_h, img_w, _ = img.shape
  39.  
  40.  
  41. # ROI cutout
  42. x0, y0 = 321, 96
  43. x1, y1 = 321 + 382, 96 + 385
  44. roi_w, roi_h = x1 - x0, y1 - y0
  45. img = img[y0:y1, x0:x1]
  46. img_orig = img.copy()
  47.  
  48.  
  49. # image thresholding
  50. th_vals = [52, 43, 40] # B, G, R
  51. img = cv2.GaussianBlur(img, (21, 21), 4)
  52.  
  53. img_s = cv2.split(img)
  54. for i, ch in enumerate(img_s):
  55. ret, ch = cv2.threshold(ch, th_vals[i] - 2, 0, cv2.THRESH_TOZERO)
  56. ret, ch = cv2.threshold(ch, th_vals[i] + 2, 0, cv2.THRESH_TOZERO_INV)
  57. ret, ch = cv2.threshold(ch, th_vals[i] - 2, 255, cv2.THRESH_BINARY)
  58. img_s[i] = ch
  59. img = cv2.merge(img_s)
  60. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  61.  
  62. grid = cv2.resize(gray, (4, 4), interpolation=cv2.INTER_AREA)
  63. ret, grid = cv2.threshold(grid, 128, 255, cv2.THRESH_BINARY)
  64. grid_copy = grid.copy()
  65. grid = grid.astype('int') / 255
  66.  
  67.  
  68. # Error check
  69. grid_active = grid[grid > 0]
  70. if grid_active.shape[0] != 3:
  71. print('ERROR ON FRAME %d' % cur_pos)
  72. exit()
  73.  
  74.  
  75. # calc osu coord and generate osu text
  76. if init:
  77. init_time = cur_time
  78. prev_grid = grid.copy()
  79. init = False
  80. continue
  81. else:
  82. diff_time = cur_time - init_time
  83. diff = grid - prev_grid
  84. row, col = np.where(diff < 0)
  85.  
  86. if len(row) > 0:
  87. xo, yo = 98, 26
  88. wo, ho = 416 - xo, 345 - yo
  89.  
  90. osu_xcoord = int(round(xo + col[0] * wo / 4 + wo / 8))
  91. osu_ycoord = int(round(yo + row[0] * ho / 4 + ho / 8))
  92. text = '%d,%d,%d,5,0,0:0:0:0:\n' % (osu_xcoord, osu_ycoord, diff_time)
  93. buffer += text
  94.  
  95. osu_coord = (osu_xcoord, osu_ycoord)
  96. if osu_coord in coord_hist:
  97. coord_hist[osu_coord] += 1
  98. else:
  99. coord_hist[osu_coord] = 1
  100.  
  101. prev_grid = grid.copy()
  102.  
  103.  
  104. # show current image
  105. gray = cv2.resize(grid_copy, (roi_w, roi_h), interpolation=cv2.INTER_NEAREST)
  106. cv2.imshow('grid', gray)
  107. cv2.imshow(win, img_orig)
  108. cv2.waitKey(1)
  109.  
  110.  
  111. # clean up and save file
  112. cv2.destroyAllWindows()
  113. cap.release()
  114. f.write(buffer)
  115. f.close()
  116.  
  117.  
  118. # view written osu coord hist.
  119. def putTextCenter(img, text, pos, font, size, color, thickness, line=cv2.LINE_4):
  120. textsize = cv2.getTextSize(text, font, size, thickness)[0]
  121. textX = round(pos[0] - textsize[0] / 2)
  122. textY = round(pos[1] + textsize[1] / 2)
  123. cv2.putText(img, text, (textX, textY), font, size, color, thickness, line)
  124.  
  125. for key in coord_hist.keys():
  126. coord_count = coord_hist[key]
  127. putTextCenter(hist, '%d'%coord_count, key, cv2.FONT_HERSHEY_SIMPLEX, 0.4, 255, 1)
  128. cv2.imshow('hist', hist)
  129. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement