Распознавание изображений в браузере с использованием Python - PullRequest
0 голосов
/ 21 сентября 2018

Я хочу внедрить программное обеспечение для подсчета карт в блэкджеке, используя некоторое распознавание изображений для автоматизации процесса.Но я не знаю с чего начать.Я думаю, что проблему можно разделить на следующие шаги:

1 - взять изображение из браузера в игре (в основном это Adobe Flash Game)

2 - обработать изображение, с некоторымираспознавание изображений, которые распознают все карты.

3- Обновление счетчика с использованием стратегии Hi-Lo

4- Отображение результата на экране

Как я могу это сделатьиспользуя питон?Какие библиотеки могут мне помочь?Это совершенно новое поле для меня.Я постараюсь реализовать вопрос на основе ваших предложений.

РЕДАКТИРОВАТЬ 1:

Selenium Webdriver работает нормально, до сих пор я использовал этот код для получения скриншота главной страницы, но я не могувойти в игру, потому что у меня нет денег, чтобы играть в LOL:

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.888casino.it/giochi-da-casino/')
browser.save_screenshot('screenie.png')
browser.quit()

Но в основном мне нужно заменить browser.get() чем-то, что захватывает браузер, а не чем-то, что открывает новыйстр.Затем мне нужно реализовать цикл for, который делает скриншоты каждую секунду, пока я играю в игру, и после этого я могу начать обрабатывать эти изображения.

РЕДАКТИРОВАТЬ 2:

Я попробую TensorFlow API для обработки изображений, но я не нашел ни одной модели обучения для распознавания карт.Итак, мне нужно создать совершенно новую модель, я нашел этот учебник , который помогает мне тренировать мою собственную модель распознавания объектов.Пожалуйста, если вы знаете существующую модель обучения, свяжите ее.

РЕДАКТИРОВАТЬ 3:

Используя Tensorflow, я смог создать свою собственную модель для распознавания объектов, теперь мне нужно использовать эту модель вскрипт на питоне.Сейчас я использовал этот пример сценария, который открывает изображение и рисует прямоугольники вокруг карточек.

import os
import cv2
import numpy as np
import tensorflow as tf
import sys

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

# Import utilites
from utils import label_map_util
from utils import visualization_utils as vis_util

# Name of the directory containing the object detection module we're using
MODEL_NAME = 'inference_graph'
IMAGE_NAME = 'test1.jpg'

# Grab path to current working directory
CWD_PATH = os.getcwd()

# Path to frozen detection graph .pb file, which contains the model that is used
# for object detection.
PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')

# Path to label map file
PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')

# Path to image
PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)

# Number of classes the object detector can identify
NUM_CLASSES = 13

# Load the label map.
# Label maps map indices to category names, so that when our convolution
# network predicts `5`, we know that this corresponds to `king`.
# Here we use internal utility functions, but anything that returns a
# dictionary mapping integers to appropriate string labels would be fine
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

# Load the Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

    sess = tf.Session(graph=detection_graph)

# Define input and output tensors (i.e. data) for the object detection classifier

# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Load image using OpenCV and
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
image = cv2.imread(PATH_TO_IMAGE)
image_expanded = np.expand_dims(image, axis=0)

# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: image_expanded})

# Draw the results of the detection (aka 'visulaize the results')

vis_util.visualize_boxes_and_labels_on_image_array(
    image,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8,
    min_score_thresh=0.80)

# All the results have been drawn on image. Now display the image.
cv2.imshow('Object detector', image)

# Press any key to close the image
cv2.waitKey(0)

# Clean up
cv2.destroyAllWindows()

Теперь мне нужно создать свой собственный скрипт, который распознает карточки, и для каждой карточки обновляется счетчик, который должен отображаться на экране.Это самая сложная часть, потому что я не знаю, с чего начать.У меня есть несколько проблем с этим шагом, во-первых, скрипт должен уметь отличать карты, покинувшие колоду, от новой, таким образом, он не портит счетчик каждый раз, когда делается скриншот.Во-вторых, счетчик должен быть обновлен с -1 для старших карт (десять - туз), +1 для младших карт (два-шесть) и 0 для нейтральных карт (7-8-9), и должен быть виден на экране.

РЕДАКТИРОВАТЬ 4: Я собрал первую версию программного обеспечения, но есть некоторые проблемы, счетчик не обновляется правильно.Вот код:

import pyscreenshot as ImageGrab
from win32api import GetSystemMetrics
import os
import cv2
import numpy as np
import tensorflow as tf
import sys

import warnings
import h5py

def UpdateCounter(labels, c):
    for i in labels:
        if labels['ace'] > 0:
            c = c - 1
        if labels['king'] > 0:
            c = c - 1
        if labels['queen'] > 0:
            c = c - 1
        if labels['jack'] > 0:
            c = c - 1
        if labels['ten'] > 0:
            c = c - 1
        if labels['six'] > 0:
            c = c + 1
        if labels['five'] > 0:
            c = c + 1
        if labels['four'] > 0:
            c = c + 1
        if labels['three'] > 0:
            c = c + 1
        if labels['two'] > 0:
            c = c + 1
        return c

if __name__ == '__main__':


    sys.path.append("..")
    from utils import label_map_util
    from utils import visualization_utils as vis_util

    MODEL_NAME = 'inference_graph'
    IMAGE_NAME = 'test1.jpg'
    CWD_PATH = os.getcwd()
    PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')

    PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')
    PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    NUM_CLASSES = 13

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)

    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)

    category_index = label_map_util.create_category_index(categories)

    detection_graph = tf.Graph()

    with detection_graph.as_default():

        od_graph_def = tf.GraphDef()

        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
            sess = tf.Session(graph=detection_graph)

    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    c = 0


    while True:
        labels = {"ace" : 0, "king": 0, "queen": 0, "jack": 0, "ten": 0, "nine": 0, "eight": 0,"seven": 0, "six": 0, "five": 0, "four":0, "three": 0, "two": 0}

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore",category=FutureWarning)
            screenshot=ImageGrab.grab(bbox=(42,42, GetSystemMetrics(0),GetSystemMetrics(1)))
            screenshot.save(IMAGE_NAME)


        image = cv2.imread(PATH_TO_IMAGE)
        image_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: image_expanded})

        data = [category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.9]



        for ch in data:
                        if ch['name'] == "ace":
                            labels["ace"] += 1
                        elif ch['name'] == "king":
                            labels["king"] += 1
                        elif ch['name'] == "queen":
                            labels["queen"] += 1
                        elif ch['name'] == "jack":
                            labels["jack"] += 1
                        elif ch['name'] == "ten":
                            labels["ten"] += 1
                        elif ch['name'] == "nine":
                            labels["nine"] += 1
                        elif ch['name'] == "eight":
                            labels["eight"] += 1
                        elif ch['name'] == "seven":
                            labels["seven"] += 1
                        elif ch['name'] == "six":
                            labels["six"] += 1
                        elif ch['name'] == "five":
                            labels["five"] += 1
                        elif ch['name'] == "four":
                            labels["four"] += 1
                        elif ch['name'] == "three":
                            labels["three"] += 1
                        elif ch['name'] == "two":
                            labels["two"] += 1

        print(UpdateCounter(labels, c))

Пожалуйста, как я могу это исправить?Мне нужно показывать счетчик только тогда, когда новые карты распознаются, и мне нужно исправить также плохой матч, который получает программа.

1 Ответ

0 голосов
/ 22 сентября 2018

Я полагаю, что вы можете достичь этого, используя селен, как вы упомянули.

Это будет примерно так:

from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('https://www.888casino.it/giochi-da-casino/')

while True:
    browser.save_screenshot('screenie.png')
    #do the image processing...
    time.sleep(1)

browser.quit()

Для самой обработки изображений у вас будетВопрос определения нужных вам на изображении карточек, в вашем случае, для дальнейшей обработки каждого из них в отдельности.Таким образом, у вас есть два шага в этом отношении.

Для вас может пригодиться API обнаружения объектов с тензорным потоком: https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API

Удачи!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...