Skip to content

Instantly share code, notes, and snippets.

@knightli
Created November 23, 2020 13:42
Show Gist options
  • Select an option

  • Save knightli/89516dd971ccde93c0f5e06f714bb650 to your computer and use it in GitHub Desktop.

Select an option

Save knightli/89516dd971ccde93c0f5e06f714bb650 to your computer and use it in GitHub Desktop.
use opencv capture switch D3 game screen, helping us reforge item
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import winsound
import time
import cv2
# 打开默认摄像头
cap = cv2.VideoCapture(0)
FRAME_WIDTH = 1920
FRAME_HEIGHT = 1080
# FRAME_WIDTH = 1280
# FRAME_HEIGHT = 720
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, FRAME_WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT)
# 从视频流循环帧
# 判定点 [296, 1205]
# 普通 [81, 100, 110]
# 远古 [90, 166, 187]
# 太古 [21, 7, 155]
# 结果:
# 截图点1 [212,1205]
# 截图点2 [862,1547]
# 来源:
# 截图点1 [212,818]
# 截图点2 [860,1156]
y = 296
x = 1205
flag = 0
print_xy = False
prefix = time.strftime("%Y%m%d_%H%M%S", time.localtime())
count = 0
cur_key = -1
pre_key = -1
def save_rst(_frame, _type):
y_1 = 212
x_1 = 1205
y_2 = 862
x_2 = 1547
crop_img = _frame[y_1:y_2, x_1:x_2]
t = '@' if _type == 2 else ''
cv2.imwrite("D:\\d3-images\\" + prefix + "_" + str(count) + t + ".bmp", crop_img)
def save_org(_frame):
y_1 = 212
x_1 = 818
y_2 = 860
x_2 = 1156
crop_img = _frame[y_1:y_2, x_1:x_2]
cv2.imwrite("D:\\d3-images\\" + prefix + "_" + str(count) + "$.bmp", crop_img)
# 是否限制H辅助线
flag_h = False
h_x = [1205, 1547]
h_y = [212, 862]
steps = [1, 5, 10, 20, 50]
stepsIdx = 0
def step_change(_n):
steps_len = len(steps)
global stepsIdx
if _n > 0:
stepsIdx = stepsIdx + 1
if stepsIdx >= steps_len:
stepsIdx = steps_len - 1
if _n < 0:
stepsIdx = stepsIdx - 1
if stepsIdx < 0:
stepsIdx = 0
def move_step(_n, _dir):
if _dir == 'a' or _dir == 'j':
h_x[_n] = h_x[_n] - steps[stepsIdx]
if h_x[_n] < 0:
h_x[_n] = 0
print("h_x[%s]--, h_x[%s]=%s" % (_n, _n, h_x[_n]))
elif _dir == 'd' or _dir == 'l':
h_x[_n] = h_x[_n] + steps[stepsIdx]
if h_x[_n] > FRAME_WIDTH:
h_x[_n] = FRAME_WIDTH
print("h_x[%s]++, h_x[%s]=%s" % (_n, _n, h_x[_n]))
elif _dir == 'w' or _dir == 'i':
h_y[_n] = h_y[_n] - steps[stepsIdx]
if h_y[_n] < 0:
h_y[_n] = 0
print("h_y[%s]--, h_y[%s]=%s" % (_n, _n, h_y[_n]))
elif _dir == 's' or _dir == 'k':
h_y[_n] = h_y[_n] + steps[stepsIdx]
if h_y[_n] > FRAME_HEIGHT:
h_y[_n] = FRAME_HEIGHT
print("h_y[%s]++, h_y[%s]=%s" % (_n, _n, h_y[_n]))
def show_h(_frame):
for _y in range(FRAME_HEIGHT):
for _x in range(FRAME_WIDTH):
if _y == h_y[0] and _x != h_x[0]:
_frame[_y, _x] = (255, 0, 0)
if _x == h_x[0] and _y != h_y[0]:
_frame[_y, _x] = (0, 255, 0)
if _y == h_y[1] and _x != h_x[1]:
_frame[_y, _x] = (255, 0, 0)
if _x == h_x[1] and _y != h_y[1]:
_frame[_y, _x] = (0, 255, 0)
if not cap.isOpened():
raise IOError('Cannot open webcam!')
while True:
ret, frame = cap.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# print(frame[y, x])
if 1:
if print_xy:
print(frame[y, x])
if frame[y, x, 0] == 90 and frame[y, x, 1] == 166 and frame[y, x, 2] == 187:
winsound.Beep(500, 100)
if flag != 1:
flag = 1
count = count + 1
print(r"Save Image Flag1, count %s" % count)
save_rst(frame, 1)
elif frame[y, x, 0] == 21 and frame[y, x, 1] == 7 and frame[y, x, 2] == 155:
winsound.Beep(700, 150)
if flag != 2:
flag = 2
count = count + 1
print(r"Save Image Flag2, count %s" % count)
save_rst(frame, 2)
elif frame[y, x, 0] == 81 and frame[y, x, 1] == 100 and frame[y, x, 2] == 110:
if flag != -1:
flag = -1
count = count + 1
print(r"Normal no save 1, count %s" % count)
elif frame[y, x, 0] == 81 and frame[y, x, 1] == 99 and frame[y, x, 2] == 112:
if flag != -1:
flag = -1
count = count + 1
print(r"Normal no save 2, count %s" % count)
else:
flag = 0
# 辅助线展示判定点
if print_xy:
for _y in range(FRAME_HEIGHT):
for _x in range(FRAME_WIDTH):
if _y == y and _x != x:
frame[_y, _x] = (255, 0, 0)
if _x == x and _y != y:
frame[_y, _x] = (0, 255, 0)
# H动态辅助线
if flag_h:
show_h(frame)
cv2.imshow("Opencv Capture Test", frame)
# 捕获按键
cur_key = cv2.waitKey(1)
# Esc,退出
if cur_key == 27:
break
# 判断是否按下其他键
#if key > -1 and key != pre_key:
# cur_key = key
#pre_key = key
# 响应事件
if cur_key == ord('r'):
count = 0
prefix = time.strftime("%Y%m%d_%H%M%S", time.localtime())
print("reset! count=%s, prefix=%s" %(count, prefix))
elif cur_key == ord('t'):
print("Save Image Origin")
save_org(frame)
elif cur_key == ord('h'):
flag_h = not(flag_h)
elif cur_key == ord('n'):
step_change(-1)
elif cur_key == ord('m'):
step_change(1)
elif cur_key == ord('w'):
move_step(0, 'w')
elif cur_key == ord('s'):
move_step(0, 's')
elif cur_key == ord('a'):
move_step(0, 'a')
elif cur_key == ord('d'):
move_step(0, 'd')
elif cur_key == ord('i'):
move_step(1, 'i')
elif cur_key == ord('k'):
move_step(1, 'k')
elif cur_key == ord('j'):
move_step(1, 'j')
elif cur_key == ord('l'):
move_step(1, 'l')
elif cur_key == ord('p'):
print_xy = not(print_xy)
# 清理
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment