#๐ OpenCV tensor size
7 messages ยท Page 1 of 1 (latest)
@bronze pebble
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
import math
import keyboard
import mss.tools
import numpy as np
import serial
import torch
import pathlib
from torchvision import transforms
from PIL import Image
# Fixing pathlib issue for Windows
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
# Load model
model = torch.hub.load('E:/yolov5-master', 'custom', path='E:/rsix.pt', source='local', force_reload=True, autoshape=False)
# Serial communication with Arduino
arduino = serial.Serial('COM4', 115200, timeout=0)
# Screen capturing setup
with mss.mss() as sct:
dimensions = sct.monitors[1]
SQUARE_SIZE = 600
monitor = {"top": int((dimensions['height'] / 2) - (SQUARE_SIZE / 2)),
"left": int((dimensions['width'] / 2) - (SQUARE_SIZE / 2)),
"width": SQUARE_SIZE,
"height": SQUARE_SIZE}
# Transformation to resize and convert image
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((SQUARE_SIZE, SQUARE_SIZE)),
transforms.ToTensor(),
])
while True:
# Screenshot
BRGframe = np.array(sct.grab(monitor))
# Convert to RGB format
RGBframe = BRGframe[:, :, [2, 1, 0]] # BGR to RGB
# Convert to PIL Image and apply transformation
input_image = transform(RGBframe).unsqueeze(0)
# PASSING CONVERTED SCREENSHOT INTO MODEL
results = model(input_image)
print("Tensor sizes:")
for i, tensor in enumerate(results):
print(f"Tensor {i}: {tensor.size()}")
# Setting confidence threshold
model.conf = 0.6
# READING OUTPUT FROM MODEL AND DETERMINING DISTANCES TO ENEMIES FROM CENTER OF THE WINDOW
enemyNum = results.xyxy[0].shape[0]
if enemyNum == 0:
pass
else:
distances = []
closest = 1000
for i in range(enemyNum):
x1 = float(results.xyxy[0][i, 0])
x2 = float(results.xyxy[0][i, 2])
y1 = float(results.xyxy[0][i, 1])
y2 = float(results.xyxy[0][i, 3])
centerX = (x2 - x1) / 2 + x1
centerY = (y2 - y1) / 2 + y1
distance = math.sqrt(((centerX - 300) ** 2) + ((centerY - 300) ** 2))
distances.append(distance)
if distances[i] < closest:
closest = distances[i]
closestEnemy = i
x1 = float(results.xyxy[0][closestEnemy, 0])
x2 = float(results.xyxy[0][closestEnemy, 2])
y1 = float(results.xyxy[0][closestEnemy, 1])
y2 = float(results.xyxy[0][closestEnemy, 3])
Xenemycoord = (x2 - x1) / 2 + x1
Yenemycoord = (y2 - y1) / 2 + y1
difx = int(Xenemycoord - (SQUARE_SIZE / 2))
dify = int(Yenemycoord - (SQUARE_SIZE / 2))
if keyboard.is_pressed('/'):
data = str(difx) + ':' + str(dify)
arduino.write(data.encode())
print(data)
^^^^^^^^^^^^^^^^^^^^
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 76 but got size 75 for tensor number 1 in the list.
PS C:\Users\aydon\Desktop\dsfasdwadasdas>
as Autoshape wont work for my PC
I have to result in manually sizing it
@bronze pebble
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.