import cv2
import numpy as np
import pyautogui
import pygame
import time
import os
import sys
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base_path, relative_path)
def cv_imread(path, flags=cv2.IMREAD_COLOR):
with open(path, 'rb') as f:
data = f.read()
data = np.frombuffer(data, np.uint8)
return cv2.imdecode(data, flags)
login_screen_path = resource_path('login_screen.png')
main_screen_path = resource_path('main_screen.png')
character_screen_path = resource_path('character_screen.png')
music_file_path = resource_path('background_music.mp3')
print("login_screen.png 존재 여부:", os.path.isfile(login_screen_path))
print("main_screen.png 존재 여부:", os.path.isfile(main_screen_path))
print("character_screen.png 존재 여부:", os.path.isfile(character_screen_path))
print("background_music.mp3 존재 여부:", os.path.isfile(music_file_path))
login_img = cv_imread(login_screen_path, cv2.IMREAD_GRAYSCALE)
main_img = cv_imread(main_screen_path, cv2.IMREAD_GRAYSCALE)
character_img = cv_imread(character_screen_path, cv2.IMREAD_GRAYSCALE)
print("login_img:", login_img is not None)
print("main_img:", main_img is not None)
print("character_img:", character_img is not None)
def screen_match(target_img, threshold=0.3):
screenshot = pyautogui.screenshot()
frame = np.array(screenshot)
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if gray_frame.shape[0] < target_img.shape[0] or gray_frame.shape[1] < target_img.shape[1]:
print("❌ 템플릿 이미지가 스크린샷보다 커서 매칭 불가!")
return False
res = cv2.matchTemplate(gray_frame, target_img, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= threshold)
return len(loc[0]) > 0
pygame.mixer.init()
music_playing = False
def is_condition_screen():
if screen_match(login_img):
return True
if screen_match(main_img):
return True
if screen_match(character_img):
return True
return False
def play_music():
global music_playing
if not music_playing:
pygame.mixer.music.load(music_file_path)
pygame.mixer.music.play(-1)
music_playing = True
print("🎵 음악 재생 시작!")
def stop_music():
global music_playing
if music_playing:
pygame.mixer.music.stop()
music_playing = False
print("🛑 음악 중지!")
try:
while True:
if is_condition_screen():
play_music()
else:
stop_music()
time.sleep(1)
except KeyboardInterrupt:
print("프로그램 종료.")
stop_music()
저는 cv2 말고 C++에서 opencv 썼는데 정지당한 적 있습니다.
인게임과 전혀 관련없는 동작이었는데 비인가 프로그램으로 정지당했어요.