Exemplo n.º 1
0
 def __init__(self, helper):
     super().__init__()
     self.helper: BaseAutomator = helper
     self.logger = logging.getLogger(type(self).__name__)
     self.richlogger = richlog.get_logger(type(self).__name__)
     self._sync_handler = None
     self.on_attach()
Exemplo n.º 2
0
def tell_item(itemimg, with_quantity=True, learn_unrecognized=False):
    logger = get_logger(__name__)
    logger.logimage(itemimg)
    from . import itemdb
    # l, t, r, b = scaledwh(80, 146, 90, 28)
    # print(l/itemimg.width, t/itemimg.height, r/itemimg.width, b/itemimg.height)
    # numimg = itemimg.crop(scaledwh(80, 146, 90, 28)).convert('L')
    low_confidence = False
    quantity = None
    if with_quantity:
        numimg = imgops.scalecrop(itemimg, 0.39, 0.71, 0.82, 0.84).convert('L')
        numimg = imgops.crop_blackedge2(numimg, 120)

        if numimg is not None:
            numimg = imgops.clear_background(numimg, 120)
            logger.logimage(numimg)
            numtext, score = itemdb.num_recognizer.recognize2(
                numimg, subset='0123456789万')
            logger.logtext('quantity: %s, minscore: %f' % (numtext, score))
            if score < 0.2:
                low_confidence = True
            quantity = int(numtext) if numtext.isdigit() else None

    # scale = 48/itemimg.height
    img4reco = np.array(
        itemimg.resize((48, 48), Image.BILINEAR).convert('RGB'))
    img4reco[itemdb.itemmask] = 0

    scores = []
    for name, templ in itemdb.itemmats.items():
        scores.append((name, imgops.compare_mse(img4reco, templ)))

    scores.sort(key=lambda x: x[1])
    itemname, score = scores[0]
    # maxmatch = max(scores, key=lambda x: x[1])
    logger.logtext(repr(scores[:5]))
    diffs = np.diff([a[1] for a in scores])
    item_type = None
    if score < 800 and np.any(diffs > 600):
        logger.logtext('matched %s with mse %f' % (itemname, score))
        name = itemname
    else:
        prob, item_id, name, item_type = get_item_id(
            common.convert_to_cv(itemimg))
        logger.logtext(f'dnn matched {name} with prob {prob}')
        if prob < 0.8 or item_id == 'other':
            logger.logtext('no match')
            low_confidence = True
            name = None

    if name is None and learn_unrecognized:
        name = itemdb.add_item(itemimg)

    return RecognizedItem(name, quantity, low_confidence, item_type)
Exemplo n.º 3
0
def tell_item(itemimg, with_quantity=True):
    logger = get_logger(__name__)
    logger.logimage(itemimg)
    cached = load_data()
    # l, t, r, b = scaledwh(80, 146, 90, 28)
    # print(l/itemimg.width, t/itemimg.height, r/itemimg.width, b/itemimg.height)
    # numimg = itemimg.crop(scaledwh(80, 146, 90, 28)).convert('L')
    low_confidence = False
    quantity = None
    if with_quantity:
        numimg = imgops.scalecrop(itemimg, 0.39, 0.71, 0.82, 0.84).convert('L')
        numimg = imgops.crop_blackedge2(numimg, 120)

        if numimg is not None:
            numimg = imgops.clear_background(numimg, 120)
            logger.logimage(numimg)
            numtext, score = cached.num_recognizer.recognize2(
                numimg, subset='0123456789万')
            logger.logtext('quantity: %s, minscore: %f' % (numtext, score))
            if score < 0.2:
                low_confidence = True
            quantity = int(numtext) if numtext.isdigit() else None

    # scale = 48/itemimg.height
    img4reco = np.array(
        itemimg.resize((48, 48), Image.BILINEAR).convert('RGB'))
    img4reco[cached.itemmask] = 0

    scores = []
    for name, templ in cached.itemmats.items():
        scores.append((name, imgops.compare_mse(img4reco, templ)))

    scores.sort(key=lambda x: x[1])
    itemname, score = scores[0]
    # maxmatch = max(scores, key=lambda x: x[1])
    logger.logtext(repr(scores[:5]))
    diffs = np.diff([a[1] for a in scores])
    if score < 800 and np.any(diffs > 600):
        logger.logtext('matched %s with mse %f' % (itemname, score))
        name = itemname
    else:
        logger.logtext('no match')
        low_confidence = True
        name = None

    return RecognizedItem(name, quantity, low_confidence)
Exemplo n.º 4
0
from fractions import Fraction

import numpy as np
from PIL import Image

from util.richlog import get_logger
from . import imgops
from . import resources
from . import util

logger = get_logger(__name__)


def check_main(img):
    vw, vh = util.get_vwvh(img.size)
    gear1 = img.crop(
        (3.148 * vh, 2.037 * vh, 9.907 * vh, 8.796 * vh)).convert('L')
    gear2 = resources.load_image_cached('main/gear.png', 'L')
    gear1, gear2 = imgops.uniform_size(gear1, gear2)
    result = imgops.compare_ccoeff(gear1, gear2)
    # result = np.corrcoef(np.asarray(gear1).flat, np.asarray(gear2).flat)[0, 1]
    logger.logimage(gear1)
    logger.logtext('ccoeff=%f' % result)
    return result > 0.9


def get_ballte_corners(img):
    """
    :returns: [0][1]
              [3][2]
    """