Я пытаюсь делать прогнозы с помощью веб-камеры и отображать их в своем веб-приложении. Я хочу, чтобы рамка веб-камеры имела определенный размер, но когда я меняю ее размер, модель перестает работать правильно, а ограничивающие рамки выглядят очень плохо.
вот функция, которую я использую
def RunYOLOWebcam(path_x):
# Start webcam
cap = cv2.VideoCapture(path_x)
desired_width = 540
desired_height = 300
# Model
model = YOLO("best.pt")
# Object classes
classNames = [""] * 26 # Create an array with 26 empty strings
for i in range(26):
classNames[i] = chr(65 + i) # Fill the array with uppercase letters (A-Z)
while True:
success, img = cap.read()
if not success:
break
# Perform YOLO detection on the original image
results = model(img, stream=True)
# Save bounding box coordinates
bounding_boxes = []
for r in results:
boxes = r.boxes
for box in boxes:
# Scale bounding box coordinates to match original image size
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1 * img.shape[1] / desired_width), int(y1 * img.shape[0] / desired_height), \
int(x2 * img.shape[1] / desired_width), int(y2 * img.shape[0] / desired_height)
bounding_boxes.append((x1, y1, x2, y2))
# Resize the image to the desired resolution
img_resized = cv2.resize(img, (desired_width, desired_height))
# Resize the bounding boxes to match the resized image
resized_bounding_boxes = []
for box in bounding_boxes:
x1, y1, x2, y2 = box
x1_resized, y1_resized, x2_resized, y2_resized = int(x1 * desired_width / img.shape[1]), \
int(y1 * desired_height / img.shape[0]), \
int(x2 * desired_width / img.shape[1]), \
int(y2 * desired_height / img.shape[0])
resized_bounding_boxes.append((x1_resized, y1_resized, x2_resized, y2_resized))
# Draw bounding boxes on the resized image
for box in resized_bounding_boxes:
x1, y1, x2, y2 = box
cv2.rectangle(img_resized, (x1, y1), (x2, y2), (255, 0, 255), 3)
yield img_resized
cv2.destroyAllWindows()`
I tried to resize the boxes according to the new resized frame but it's still not working.
Похоже, вы немного запутались в имеющихся у вас координатах. Вам не нужно масштабировать координаты xyxy yolov8 box до исходного размера изображения, они уже масштабированы до него.
Для удобства я использовал box.xyxyn вместо box.xyxy: он возвращает поля в формате xyxy, нормализованные по исходному размеру изображения (x1 и x2, разделенные на исходную ширину изображения, y1 и y2, разделенные на исходную высоту изображения). Нормализованные координаты легко масштабируются под разные размеры изображения: вам просто нужно умножить их на нужную ширину и высоту изображения соответственно.
for r in results:
boxes = r.boxes
for box in boxes:
x1, y1, x2, y2 = box.xyxyn[0]
bounding_boxes.append((x1, y1, x2, y2))
# Resize the image to the desired resolution
img_resized = cv2.resize(img, (desired_width, desired_height))
# Resize the bounding boxes to match the resized image
resized_bounding_boxes = []
for box in bounding_boxes:
x1, y1, x2, y2 = box
x1_resized, y1_resized, x2_resized, y2_resized = int(x1 * desired_width), \
int(y1 * desired_height), \
int(x2 * desired_width), \
int(y2 * desired_height)
resized_bounding_boxes.append((x1_resized, y1_resized, x2_resized, y2_resized))
Доступные форматы координат коробок yolov8 перечислены здесь.