mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-03-11 23:18:22 +08:00
Add: Support gif templates
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import os
|
||||
|
||||
import cv2
|
||||
import imageio
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
import module.config.server as server
|
||||
|
||||
|
||||
@@ -12,12 +16,18 @@ class Template:
|
||||
"""
|
||||
self.server = server.server
|
||||
self.file = file[self.server] if isinstance(file, dict) else file
|
||||
self.is_gif = os.path.splitext(self.file)[1] == '.gif'
|
||||
self._image = None
|
||||
|
||||
@property
|
||||
def image(self):
|
||||
if self._image is None:
|
||||
self._image = np.array(Image.open(self.file))
|
||||
if self.is_gif:
|
||||
self._image = []
|
||||
for image in imageio.mimread(self.file):
|
||||
self._image += [image, cv2.flip(image, 1)]
|
||||
else:
|
||||
self._image = np.array(Image.open(self.file))
|
||||
|
||||
return self._image
|
||||
|
||||
@@ -34,10 +44,22 @@ class Template:
|
||||
Returns:
|
||||
bool: If matches.
|
||||
"""
|
||||
res = cv2.matchTemplate(np.array(image), self.image, cv2.TM_CCOEFF_NORMED)
|
||||
_, sim, _, _ = cv2.minMaxLoc(res)
|
||||
# print(self.file, sim)
|
||||
return sim > similarity
|
||||
if self.is_gif:
|
||||
image = np.array(image)
|
||||
for template in self.image:
|
||||
res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
|
||||
_, sim, _, _ = cv2.minMaxLoc(res)
|
||||
# print(self.file, sim)
|
||||
if sim > similarity:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
else:
|
||||
res = cv2.matchTemplate(np.array(image), self.image, cv2.TM_CCOEFF_NORMED)
|
||||
_, sim, _, _ = cv2.minMaxLoc(res)
|
||||
# print(self.file, sim)
|
||||
return sim > similarity
|
||||
|
||||
def match_result(self, image):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user