1. 磐创AI-开放猫官方网站首页
  2. Medium

基于多进程的TensorFlow目标检测机器人

欢迎大家学习我们的TensorFlow对象检测API教程系列的第10部分。首先,您可以在我的GitHub页面上下载代码。这将是这个CSGO aimbot视频系列的最后一个教程,因为现在,我在这个教程上花了太多的时间。尽管如此,我还是设法取得了目前最好的表现。为了进一步改进,我需要研究另一种检测方法的时间-我将在未来这样做。GitHub

在继续本教程之前,我应该提到,在将第9教程代码与CSGO TensorFlow检测代码合并之前,我更新了它。与多处理队列相比,性能是相同的(33FPS),但是我想测试不同的数据方法来在进程之间共享数据。所以我更新了第9个教程,增加了一个文件,我们在其中使用多处理管道抓取屏幕。除了多处理管道之外,它们还使用一对一通信,队列可以作为“多对多”使用。

继续本教程,我不会在此文本教程部分中进行代码解释。相反,我将整个代码分为3个部分:抓取屏幕、进行TensorFlow检测和显示屏幕。然后,这3个部分全部移到多处理进程中。

以下是最终代码:

# # Imports
import multiprocessing
from multiprocessing import Pipe
import time
import cv2
import mss
import numpy as np
import os
import sys
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import tensorflow as tf
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
import pyautogui


# title of our window
title = "FPS benchmark"
# set start time to current time
start_time = time.time()
# displays the frame rate every 2 second
display_time = 2
# Set primarry FPS to 0
fps = 0
# Load mss library as sct
sct = mss.mss()
# Set monitor size to capture to MSS
width = 800
height = 640

monitor = {"top": 80, "left": 0, "width": width, "height": height}

# ## Env setup
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

# # Model preparation
PATH_TO_FROZEN_GRAPH = 'CSGO_frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'CSGO_labelmap.pbtxt'
NUM_CLASSES = 4

# ## Load a (frozen) Tensorflow model into memory.
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_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')

def Shoot(mid_x, mid_y):
x = int(mid_x*width)
#y = int(mid_y*height)
y = int(mid_y*height+height/9)
pyautogui.moveTo(x,y)
pyautogui.click()

def grab_screen(p_input):
while True:
#Grab screen image
img = np.array(sct.grab(monitor))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Put image from pipe
p_input.send(img)

def TensorflowDetection(p_output, p_input2):
# Detection
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while True:
# Get image from pipe
image_np = p_output.recv()
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Visualization of the results of a detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=2)

# Send detection image to pipe2
p_input2.send(image_np)

array_ch = []
array_c = []
array_th = []
array_t = []
for i,b in enumerate(boxes[0]):
if classes[0][i] == 2: # ch
if scores[0][i] >= 0.5:
mid_x = (boxes[0][i][1]+boxes[0][i][3])/2
mid_y = (boxes[0][i][0]+boxes[0][i][2])/2
array_ch.append([mid_x, mid_y])
cv2.circle(image_np,(int(mid_x*width),int(mid_y*height)), 3, (0,0,255), -1)
if classes[0][i] == 1: # c
if scores[0][i] >= 0.5:
mid_x = (boxes[0][i][1]+boxes[0][i][3])/2
mid_y = boxes[0][i][0] + (boxes[0][i][2]-boxes[0][i][0])/6
array_c.append([mid_x, mid_y])
cv2.circle(image_np,(int(mid_x*width),int(mid_y*height)), 3, (50,150,255), -1)
if classes[0][i] == 4: # th
if scores[0][i] >= 0.5:
mid_x = (boxes[0][i][1]+boxes[0][i][3])/2
mid_y = (boxes[0][i][0]+boxes[0][i][2])/2
array_th.append([mid_x, mid_y])
cv2.circle(image_np,(int(mid_x*width),int(mid_y*height)), 3, (0,0,255), -1)
if classes[0][i] == 3: # t
if scores[0][i] >= 0.5:
mid_x = (boxes[0][i][1]+boxes[0][i][3])/2
mid_y = boxes[0][i][0] + (boxes[0][i][2]-boxes[0][i][0])/6
array_t.append([mid_x, mid_y])
cv2.circle(image_np,(int(mid_x*width),int(mid_y*height)), 3, (50,150,255), -1)

team = "c" # shooting target
if team == "c":
if len(array_ch) > 0:
Shoot(array_ch[0][0], array_ch[0][1])
if len(array_ch) == 0 and len(array_c) > 0:
Shoot(array_c[0][0], array_c[0][1])
if team == "t":
if len(array_th) > 0:
Shoot(array_th[0][0], array_th[0][1])
if len(array_th) == 0 and len(array_t) > 0:
Shoot(array_t[0][0], array_t[0][1])


def Show_image(p_output2):
global start_time, fps
while True:
image_np = p_output2.recv()
# Show image with detection
cv2.imshow(title, image_np)
# Bellow we calculate our FPS
fps+=1
TIME = time.time() - start_time
if (TIME) >= display_time :
print("FPS: ", fps / (TIME))
fps = 0
start_time = time.time()
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break

if __name__=="__main__":
# Pipes
p_output, p_input = Pipe()
p_output2, p_input2 = Pipe()

# creating new processes
p1 = multiprocessing.Process(target=grab_screen, args=(p_input,))
p2 = multiprocessing.Process(target=TensorflowDetection, args=(p_output,p_input2,))
p3 = multiprocessing.Process(target=Show_image, args=(p_output2,))

# starting our processes
p1.start()
p2.start()
p3.start()

作为最终的结果,我很高兴我们可以达到20多个FPS。但是,当TensorFlow接收到我们探测到敌人的图像时,瓶颈就来了。FPS下降到每秒4-5帧,我们的机器人玩这个游戏变得不可能了。因此,在未来,当我找到更快地发现我们的敌人的方法时,我可能会回到这个项目上来。有一种方法可以使用YOLO对象检测模型,它相当快速和准确,但(目前)实现起来比较困难。

不管怎么说,我想我在这个项目上花了很多时间。现在,我将转到另一个更有益的项目。在不久的将来,我计划制作教程如何破解验证码,如何使用硒来制作网络冲浪机器人或人工智能外汇交易机器人。

最初发表于https://pylessons.com/Tensorflow-object-detection-aim-bot-with-multiprocessinghttps://pylessons.com/Tensorflow-object-detection-aim-bot-with-multiprocessing

原创文章,作者:fendouai,如若转载,请注明出处:https://panchuang.net/2021/07/08/%e5%9f%ba%e4%ba%8e%e5%a4%9a%e8%bf%9b%e7%a8%8b%e7%9a%84tensorflow%e7%9b%ae%e6%a0%87%e6%a3%80%e6%b5%8b%e6%9c%ba%e5%99%a8%e4%ba%ba-2/

联系我们

400-800-8888

在线咨询:点击这里给我发消息

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息