-
Notifications
You must be signed in to change notification settings - Fork 15
/
utilities.py
109 lines (72 loc) · 2.71 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import cv2
import numpy as np
from time import time, sleep
from gamecapture import GameCapture
from detection import Detection
from vision import Vision
class Utilities:
def __init__(self):
pass
@staticmethod
def check_fps(input_video):
cap = cv2.VideoCapture(input_video)
print(f'FPS: {cap.get(cv2.CAP_PROP_FPS)}')
cap.release()
# Multi thread
# TODO: Refactor
@staticmethod
def fps_test(w, h, windowname = ''):
# C: 162, 0.08039550722381215
# D: 13, 0.774630069732666
cap = GameCapture(w, h, windowname, method)
cap.start()
sleep(5)
cap.stop()
print(f'FPS: {int(cap.frame_number / 3)}')
return int(cap.frame_number / 3)
# Single thread
def fps_test2(sw, sh, windowname = '', method = 'WIN32GUI'):
capture = GameCapture(sw, sh, windowname)
detector = Detection()
vision = Vision()
start = time()
for _ in range(32):
frame = capture.capture_frame()
boxes = detector.detect(frame)
target = vision.get_priority_target(boxes)
frame = vision.draw_bounding_boxes(frame, boxes)
frame = vision.draw_crosshair(frame, target)
return int(32 / (time() - start))
# TODO: refactor
@staticmethod
def process_video(filename):
cap = cv2.VideoCapture(f'data/{filename}')
out = cv2.VideoWriter(f'output/{filename}', cv2.VideoWriter_fourcc(*'mp4v'), int(cap.get(cv2.CAP_PROP_FPS)), (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
detector = Detection()
vision = Vision()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
pred = detector.detect(frame)
target = vision.get_priority_target(pred)
frame = vision.draw_bounding_boxes(frame, pred)
frame = vision.draw_crosshair(frame, target)
out.write(frame)
out.release()
cap.release()
# TODO: refactor
@staticmethod
def alter_fps(input_video, output_video, scale):
cap = cv2.VideoCapture(input_video)
out = cv2.VideoWriter(f'{output_video}', cv2.VideoWriter_fourcc(*'mp4v'), int(cap.get(cv2.CAP_PROP_FPS) / scale), (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
counter = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if counter % scale == 0:
out.write(frame)
counter += 1
out.release()
cap.release()