|
| 1 | +import typing |
| 2 | +import enum |
| 3 | +import collections |
| 4 | + |
| 5 | +from Util import * |
| 6 | +from Strategies.AbstractStrategy import * |
| 7 | +from AbstractFlagIndex import * |
| 8 | +from Rectangle import * |
| 9 | +from IR import * |
| 10 | + |
| 11 | +class MadodoraStrategy(AbstractFramewiseStrategy): |
| 12 | + class FlagIndex(AbstractFlagIndex): |
| 13 | + Dialog = enum.auto() |
| 14 | + DialogFuture = enum.auto() |
| 15 | + DialogBg = enum.auto() |
| 16 | + DialogText = enum.auto() |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def getDefaultFlagsImpl(cls) -> typing.List[typing.Any]: |
| 20 | + return [False] * cls.getNum() |
| 21 | + |
| 22 | + def __init__(self, config: dict, contentRect: AbstractRectangle) -> None: |
| 23 | + AbstractStrategy.__init__(self, contentRect) |
| 24 | + self.rectangles: collections.OrderedDict[str, AbstractRectangle] = collections.OrderedDict() |
| 25 | + for k, v in config.items(): |
| 26 | + self.rectangles[k] = RatioRectangle(contentRect, *v) |
| 27 | + |
| 28 | + self.dialogRect = self.rectangles["dialogRect"] |
| 29 | + |
| 30 | + self.cvPasses = [self.cvPassDialog] |
| 31 | + |
| 32 | + self.fpirPasses = collections.OrderedDict() |
| 33 | + self.fpirPasses["fpirPassDialogFuture"] = FPIRPassShift(MadodoraStrategy.FlagIndex.DialogFuture, MadodoraStrategy.FlagIndex.Dialog, -30, False) |
| 34 | + self.fpirPasses["fpirPassDialogRefine"] = FPIRPassFramewiseFunctional( |
| 35 | + lambda framePoint: framePoint.setFlag( |
| 36 | + MadodoraStrategy.FlagIndex.Dialog, |
| 37 | + framePoint.getFlag(MadodoraStrategy.FlagIndex.Dialog) |
| 38 | + or framePoint.getFlag(MadodoraStrategy.FlagIndex.DialogFuture) and framePoint.getFlag(MadodoraStrategy.FlagIndex.DialogBg) |
| 39 | + ) |
| 40 | + ) |
| 41 | + self.fpirPasses["fpirPassRemoveNoiseDialogTrue"] = FPIRPassBooleanRemoveNoise(MadodoraStrategy.FlagIndex.Dialog, True, 10) |
| 42 | + |
| 43 | + self.fpirToIirPasses = collections.OrderedDict() |
| 44 | + self.fpirToIirPasses["fpirPassBuildIntervals"] = FPIRPassBooleanBuildIntervals( |
| 45 | + MadodoraStrategy.FlagIndex.Dialog, |
| 46 | + ) |
| 47 | + |
| 48 | + self.iirPasses = collections.OrderedDict() |
| 49 | + self.iirPasses["iirPassFillGapDialog"] = IIRPassFillGap(MadodoraStrategy.FlagIndex.Dialog, 300, 0.0) |
| 50 | + |
| 51 | + self.logfile = open("madodora.log", "w") |
| 52 | + |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def getFlagIndexType(cls) -> typing.Type[AbstractFlagIndex]: |
| 56 | + return cls.FlagIndex |
| 57 | + |
| 58 | + def getRectangles(self) -> collections.OrderedDict[str, AbstractRectangle]: |
| 59 | + return self.rectangles |
| 60 | + |
| 61 | + def getCvPasses(self) -> typing.List[typing.Callable[[cv.Mat, FramePoint], bool]]: |
| 62 | + return self.cvPasses |
| 63 | + |
| 64 | + def getFpirPasses(self) -> collections.OrderedDict[str, FPIRPass]: |
| 65 | + return self.fpirPasses |
| 66 | + |
| 67 | + def getFpirToIirPasses(self) -> collections.OrderedDict[str, FPIRPassBuildIntervals]: |
| 68 | + return self.fpirToIirPasses |
| 69 | + |
| 70 | + def getIirPasses(self) -> collections.OrderedDict[str, IIRPass]: |
| 71 | + return self.iirPasses |
| 72 | + |
| 73 | + def cvPassDialog(self, frame: cv.Mat, framePoint: FramePoint) -> bool: |
| 74 | + roiDialog = self.dialogRect.cutRoiToUmat(frame) |
| 75 | + roiDialogGray = cv.cvtColor(roiDialog, cv.COLOR_BGR2GRAY) |
| 76 | + _, roiDialogTextBin = cv.threshold(roiDialogGray, 192, 255, cv.THRESH_BINARY) |
| 77 | + _, roiDialogBgBin = cv.threshold(roiDialogGray, 70, 255, cv.THRESH_BINARY) |
| 78 | + meanDialogTextBin: float = cv.mean(roiDialogTextBin)[0] |
| 79 | + meanDialogBgBin: float = cv.mean(roiDialogBgBin)[0] |
| 80 | + hasDialogBg: bool = meanDialogBgBin < 20 and meanDialogBgBin > 0.1 |
| 81 | + hasDialogText: bool = meanDialogTextBin < 10 and meanDialogTextBin > 0.1 |
| 82 | + |
| 83 | + isValidDialog = hasDialogBg and hasDialogText |
| 84 | + |
| 85 | + framePoint.setFlag(MadodoraStrategy.FlagIndex.Dialog, isValidDialog) |
| 86 | + framePoint.setFlag(MadodoraStrategy.FlagIndex.DialogBg, hasDialogBg) |
| 87 | + framePoint.setFlag(MadodoraStrategy.FlagIndex.DialogText, hasDialogText) |
| 88 | + |
| 89 | + return isValidDialog |
0 commit comments