Tensorflow项目,用于在Python的二十一点中计数卡

时间:2018-09-25 14:19:41

标签: python opencv tensorflow

我创建了这个程序,该程序可以从屏幕截图中识别卡片,并按照Hi-Lo策略进行操作(高卡片(十到张)得分为-1,低卡片(两到六张)得分为)。 +1,中性卡(七到九)的分数为0)会更新一个计数器,该计数器将显示在屏幕上。我有一些与此计数器有关的问题。由于卡被识别,它无法正确更新。仅在识别出新卡时才需要更新。

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(data, c):
    for ch in data:
        if ch['name'] == "ace":
            c = c - 1
        if ch['name'] == "king":
            c = c - 1
        if ch['name'] == "queen":
            c = c - 1
        if ch['name'] == "jack":
            c = c - 1
        if ch['name'] == "ten":
            c = c - 1
        if ch['name'] == "six":
            c = c + 1
        if ch['name'] == "five":
            c = c + 1
        if ch['name'] == "four":
            c = c + 1
        if ch['name'] == "three":
            c = c + 1
        if ch['name'] == "two":
            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:

        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]

        print(UpdateCounter(data, c))

如果这个问题不清楚,请随时提出建议。

0 个答案:

没有答案