只计算Tensorflow Python中的新对象

时间:2018-09-26 11:47:38

标签: python opencv tensorflow

该程序的目的是从屏幕快照中检测卡,并打印屏幕上检测到的卡数:

jwt_token = jwt.encode(payload, settings.SECRET_KEY)

问题是,当我在屏幕上显示一张卡片时,它会不断向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 UpdateLabels(labels): 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 return labels 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_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') 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} 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) (scores, classes, num) = sess.run( [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.99] UpdateLabels(labels) print(labels) 添加同一张卡片,而我只需要跟踪可以识别的新卡片。例如,当我显示ace时,程序的输出将为:

Output of the program

它一直将同一张卡加到labels,它应该继续显示“ 1”王牌 只要识别出一张新卡,例如一张两张,就应该显示“ 1” ace和“ 1”两个,如果识别出另一张一张ace,则应该显示“ 2” ace和“ 1”两个。我该怎么做?

0 个答案:

没有答案