Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # https://youtu.be/vNIuTMhAp_Q
- import ctypes
- ctypes.windll.user32.SetProcessDPIAware()
- import cv2
- import numpy as np
- # osu window
- or_w, or_h = 512, 384
- hist = np.zeros((or_h, or_w, 1))
- coord_hist = {}
- cap = cv2.VideoCapture('frenzy.mp4')
- f = open('osu.txt', 'w')
- buffer = ''
- frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
- # main loop
- init = True
- win = 'img'
- cv2.namedWindow(win)
- while True:
- cur_pos = cap.get(cv2.CAP_PROP_POS_FRAMES)
- cur_time = cap.get(cv2.CAP_PROP_POS_MSEC)
- print('%d/%d' % (cur_pos, frames), end='\r')
- ret, img = cap.read()
- if cur_pos < 228:
- continue
- if not ret or cv2.getWindowProperty(win, 0) < 0 or cur_pos > 2100:
- break
- img = cv2.resize(img, None, fx=0.8, fy=0.8) # to make things easier
- img_h, img_w, _ = img.shape
- # ROI cutout
- x0, y0 = 321, 96
- x1, y1 = 321 + 382, 96 + 385
- roi_w, roi_h = x1 - x0, y1 - y0
- img = img[y0:y1, x0:x1]
- img_orig = img.copy()
- # image thresholding
- th_vals = [52, 43, 40] # B, G, R
- img = cv2.GaussianBlur(img, (21, 21), 4)
- img_s = cv2.split(img)
- for i, ch in enumerate(img_s):
- ret, ch = cv2.threshold(ch, th_vals[i] - 2, 0, cv2.THRESH_TOZERO)
- ret, ch = cv2.threshold(ch, th_vals[i] + 2, 0, cv2.THRESH_TOZERO_INV)
- ret, ch = cv2.threshold(ch, th_vals[i] - 2, 255, cv2.THRESH_BINARY)
- img_s[i] = ch
- img = cv2.merge(img_s)
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- grid = cv2.resize(gray, (4, 4), interpolation=cv2.INTER_AREA)
- ret, grid = cv2.threshold(grid, 128, 255, cv2.THRESH_BINARY)
- grid_copy = grid.copy()
- grid = grid.astype('int') / 255
- # Error check
- grid_active = grid[grid > 0]
- if grid_active.shape[0] != 3:
- print('ERROR ON FRAME %d' % cur_pos)
- exit()
- # calc osu coord and generate osu text
- if init:
- init_time = cur_time
- prev_grid = grid.copy()
- init = False
- continue
- else:
- diff_time = cur_time - init_time
- diff = grid - prev_grid
- row, col = np.where(diff < 0)
- if len(row) > 0:
- xo, yo = 98, 26
- wo, ho = 416 - xo, 345 - yo
- osu_xcoord = int(round(xo + col[0] * wo / 4 + wo / 8))
- osu_ycoord = int(round(yo + row[0] * ho / 4 + ho / 8))
- text = '%d,%d,%d,5,0,0:0:0:0:\n' % (osu_xcoord, osu_ycoord, diff_time)
- buffer += text
- osu_coord = (osu_xcoord, osu_ycoord)
- if osu_coord in coord_hist:
- coord_hist[osu_coord] += 1
- else:
- coord_hist[osu_coord] = 1
- prev_grid = grid.copy()
- # show current image
- gray = cv2.resize(grid_copy, (roi_w, roi_h), interpolation=cv2.INTER_NEAREST)
- cv2.imshow('grid', gray)
- cv2.imshow(win, img_orig)
- cv2.waitKey(1)
- # clean up and save file
- cv2.destroyAllWindows()
- cap.release()
- f.write(buffer)
- f.close()
- # view written osu coord hist.
- def putTextCenter(img, text, pos, font, size, color, thickness, line=cv2.LINE_4):
- textsize = cv2.getTextSize(text, font, size, thickness)[0]
- textX = round(pos[0] - textsize[0] / 2)
- textY = round(pos[1] + textsize[1] / 2)
- cv2.putText(img, text, (textX, textY), font, size, color, thickness, line)
- for key in coord_hist.keys():
- coord_count = coord_hist[key]
- putTextCenter(hist, '%d'%coord_count, key, cv2.FONT_HERSHEY_SIMPLEX, 0.4, 255, 1)
- cv2.imshow('hist', hist)
- cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement