У меня есть такой скрипт, который использует pytesseract и pyscreenshot
Компилирую весь скрипт через pyinstaller и получаю на выходе .exe, при запуске которого в диспетчере задач вижу размножение моих main.exe.
Если я запускаю функцию job(), то происходит вышеописанная проблема, но если этот вызов убрать, то ничего "размножаться" не будет.
Помогите пожалуйста, в чем проблема и как её решить?
Код:
import pyscreenshot as ImageGrab
from PIL import Image
from pytesseract import pytesseract
import re
import os
import pyautogui
import cv2
import numpy as np
import json
import socketio
import multiprocessing
sio = socketio.Client()
connected = False
print('Около connected')
def connect_to_server():
global connected
if not connected:
@sio.on('connect')
def on_connect():
print('SIO | Connected to server')
connected = True
@sio.on('message')
def on_message(data):
print('Received message:', data)
sio.connect('https://server')
pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
screenWidth, screenHeight = pyautogui.size()
pre_x = None
pre_y = None
i = 0
def getSpeed():
x1_rel, y1_rel, x2_rel, y2_rel = 835, 65, 1084, 83
x1_abs = int(screenWidth * (x1_rel / 1920))
y1_abs = int(screenHeight * (y1_rel / 1080))
x2_abs = int(screenWidth * (x2_rel / 1920))
y2_abs = int(screenHeight * (y2_rel / 1080))
bbox = (x1_abs, y1_abs, x2_abs, y2_abs)
im = ImageGrab.grab(bbox=bbox)
im.save("speedInfo.png")
image = Image.open('speedInfo.png')
text = pytesseract.image_to_string(image, lang='eng')
image.close()
os.remove('speedInfo.png')
numbers = re.findall(r'\d+', text)
numbers = [int(num) for num in numbers]
if len(numbers) > 2 or len(numbers) == 1 or len(numbers) == 0:
print(f'\n\nRestarting... \nSpeed: {numbers}\n\n')
return getSpeed()
return numbers
def getMap():
global pre_x, pre_y
global i
bbox = (screenWidth - 307, screenHeight - 293, screenWidth, screenHeight)
im = ImageGrab.grab(bbox=bbox)
im.save("partMap.png")
full_image = cv2.imread('fullMap.png')
part_image = cv2.imread('partMap.png')
full_gray = cv2.cvtColor(full_image, cv2.COLOR_BGR2GRAY)
part_gray = cv2.cvtColor(part_image, cv2.COLOR_BGR2GRAY)
lower_yellow = np.array([0, 150, 150], dtype=np.uint8)
upper_yellow = np.array([40, 255, 255], dtype=np.uint8)
yellow_mask = cv2.inRange(part_image, lower_yellow, upper_yellow)
contours, _ = cv2.findContours(yellow_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(part_image, (x, y), (x + w, y + h), (255, 0, 255), -1)
full_gray = cv2.cvtColor(full_image, cv2.COLOR_BGR2GRAY)
part_gray = cv2.cvtColor(part_image, cv2.COLOR_BGR2GRAY)
orb = cv2.ORB_create()
keypoints1, descriptors1 = orb.detectAndCompute(full_gray, None)
keypoints2, descriptors2 = orb.detectAndCompute(part_gray, None)
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = matcher.match(descriptors1, descriptors2)
matches = sorted(matches, key=lambda x: x.distance)
src_pts = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)
h, w = full_gray.shape
transformed_part = cv2.warpPerspective(part_image, M, (w, h))
result = cv2.add(full_image, transformed_part)
cv2.imwrite("combined_image.png", result)
image = cv2.imread('combined_image.png')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_pink = np.array([140, 50, 50])
upper_pink = np.array([180, 255, 255])
pink_mask = cv2.inRange(hsv_image, lower_pink, upper_pink)
contours, _ = cv2.findContours(pink_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
max_area = 0
max_contour = None
for contour in contours:
area = cv2.contourArea(contour)
if area > max_area:
max_area = area
max_contour = contour
if max_contour is not None:
x, y, w, h = cv2.boundingRect(max_contour)
curr_x = float(f"{x}.{w}")
curr_y = float(f"{y}.{h}")
if pre_x is not None and pre_y is not None and (curr_x - pre_x > 10 or curr_y - pre_y > 10) and (i <= 1):
if i <= 1:
print('Connection lost!')
print(f'\nReconnect ({i})\n')
i += 1
return getMap()
pre_x = curr_x
pre_y = curr_y
i = 0
os.remove('combined_image.png')
os.remove('partMap.png')
return curr_x, curr_y
else:
print("Waypoint not found")
os.remove('combined_image.png')
os.remove('partMap.png')
return getMap()
def job():
connect_to_server()
print('Перед намберс')
numbers = getSpeed()
x, y = getMap()
print('Перед сеттингс')
with open('settings.json', 'r') as file:
data = json.load(file)
print(numbers)
data['speed'] = numbers[0]
data['altitude'] = numbers[1]
data['x'] = x
data['y'] = y
print(data)
data_json = json.dumps(data)
sio.emit('setFlightData', data_json)
if __name__ == '__main__':
job()