gpt4 book ai didi

python - 游戏图像识别(识别《Flappy Bird》中的得分或游戏结束)

转载 作者:行者123 更新时间:2023-12-01 09:25:54 25 4
gpt4 key购买 nike

为了熟悉强化学习,我正在实现基本的 RL 算法来玩游戏 Flappy Bird 。我已完成所有设置,唯一遇到的问题是实现奖励功能。我希望能够处理屏幕并识别是否已得分或鸟是否已死亡。

处理屏幕是使用 mss 完成的和 opencv,它返回 stacked numpy array 。然后奖励函数需要为提供的数组分配奖励,但我不知道如何去做。

这是单个处理后的图像的样子:

This is how a processed image looks like

我实现奖励函数的想法是,如果背景停止移动,那只鸟就死了。如果小鸟位于两个管道之间的间隙中,则特工得分。关于如何在 numpy 计算中表达这一点有什么想法吗?

def _calculate_reward(self, state):
""""
calculate the reward of the state. Flappy is dead when the screen has stopped moving, so when two consecutive frames
are equal. A point is scored when an obstacle is above flappy, and before it wasn't. An object is above Flappy when
there are two white pixels in the first 50 pixels on the first row.

:param state: np.array shape = (1, height, width, 4) - > four consecutive processed frames
:return reward: int representing the reward if a point is scored or if flappy has died.
"""
if np.sum((state[0,:,:,3] - state[0,:,:,2])) == 0 and np.sum((state[0,:,:,2] - state[0,:,:,1])) == 0:
print("flappy is dead")
return -1000
elif sum(state[0,0,:50,3]) == 510 and sum(state[0,0,:50,2]) == 510 and sum(state[0,0,:50,1]) != 510 and sum(state[0,0,:50,0]) != 510:
print("point!")
return 1000
else:
return 0

最佳答案

如果你正在使用 OpenCV 试试 template matching 怎么样? ?

detecting mario coins using opencv python template matching

关于python - 游戏图像识别(识别《Flappy Bird》中的得分或游戏结束),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com