{"id":379677,"date":"2024-06-25T03:26:23","date_gmt":"2024-06-25T03:26:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=379677"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=379677","title":{"rendered":"<span>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 YOLOV8. \u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f, \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f, \u0442\u0440\u0435\u043a\u0438\u043d\u0433 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044f\u043c\u0438 &#8212; \u044d\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e, \u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0434\u043b\u044f \u0432\u0430\u0441!<\/p>\n<p>\u0418\u0442\u0430\u043a, YOLO\u00a0<\/strong>(You Only Look Once) \u2014 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0430\u044f \u0441\u0435\u0442\u044c, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0448\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<ul>\n<li>\n<p>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f &#8212; \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f &#8212; \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443<\/p>\n<\/li>\n<li>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0438\u0441\u043a \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0442\u0435\u043b\u0430 &#8212; \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0437\u044b \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0435\u043a\u0438\u043d\u0433 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 &#8212; \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u0442\u043e\u0440\u0438\u044e \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e: <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u0434\u043b\u044f \u0434\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0438 \u043d\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<\/ul>\n<h2>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043a \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 YOLO<\/h2>\n<p>\u041e\u0442\u043b\u0438\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e YOLO \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0445\u043e\u0434, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0447\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0438\u043c\u0435\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0432\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430 Python.<br \/>\u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 YOLO \u043d\u0430 \u0432\u0430\u0448 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043a \u043a\u043e\u043d\u0441\u043e\u043b\u0438: <\/p>\n<p><code>pip install ultralytics<\/p>\n<p><\/code>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u043c\u043e\u0434\u0443\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a \u0440\u0430\u0431\u043e\u0442\u0435.<\/p>\n<h2>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 \u0444\u043e\u0442\u043e<\/h2>\n<p>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u043d\u0435\u0439:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c3f\/bb9\/0e2\/c3fbb90e2a3a6cfc4050b9ce2c785638.png\" alt=\"\u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\" title=\"\u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\" width=\"1229\" height=\"1218\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c3f\/bb9\/0e2\/c3fbb90e2a3a6cfc4050b9ce2c785638.png\"\/><\/p>\n<div><figcaption>\u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 YOLO \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<pre><code class=\"python\">from ultralytics import YOLO import cv2 import numpy as np import os  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO('yolov8n.pt')  # \u0421\u043f\u0438\u0441\u043e\u043a \u0446\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 colors = [     (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255),     (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0),     (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128), (72, 61, 139),     (47, 79, 79), (47, 79, 47), (0, 206, 209), (148, 0, 211), (255, 20, 147) ]  # \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f def process_image(image_path):     # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     image = cv2.imread(image_path)     results = model(image)[0]          # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     image = results.orig_img     classes_names = results.names     classes = results.boxes.cls.cpu().numpy()     boxes = results.boxes.xyxy.cpu().numpy().astype(np.int32)      # \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u043c     grouped_objects = {}      # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u043c\u043e\u043a \u0438 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     for class_id, box in zip(classes, boxes):         class_name = classes_names[int(class_id)]         color = colors[int(class_id) % len(colors)]  # \u0412\u044b\u0431\u043e\u0440 \u0446\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430         if class_name not in grouped_objects:             grouped_objects[class_name] = []         grouped_objects[class_name].append(box)          # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u043c\u043e\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438         x1, y1, x2, y2 = box         cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)         cv2.putText(image, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)      # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     new_image_path = os.path.splitext(image_path)[0] + '_yolo' + os.path.splitext(image_path)[1]     cv2.imwrite(new_image_path, image)      # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b     text_file_path = os.path.splitext(image_path)[0] + '_data.txt'     with open(text_file_path, 'w') as f:         for class_name, details in grouped_objects.items():             f.write(f\"{class_name}:\\n\")             for detail in details:                 f.write(f\"Coordinates: ({detail[0]}, {detail[1]}, {detail[2]}, {detail[3]})\\n\")      print(f\"Processed {image_path}:\")     print(f\"Saved bounding-box image to {new_image_path}\")     print(f\"Saved data to {text_file_path}\")   process_image('test.png') <\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043a\u043e\u0434\u0430 \u2014 \u0432\u044b\u0431\u043e\u0440 \u043c\u043e\u0434\u0435\u043b\u0438 (\u043e\u043d\u0430 \u0441\u043a\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u0430). \u041a\u0440\u043e\u043c\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 <code>yolov8n.pt,<\/code> \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e:<br \/><code>yolov8n.pt<\/code>\u00a0<code>yolov8s.pt<\/code>\u00a0<code>yolov8m.pt<\/code>\u00a0<code>yolov8l.pt<\/code>\u00a0<code>yolov8x.pt<\/code>  <\/p>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0431\u043e\u043b\u044c\u0448\u0435, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u043d\u043e \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0443\u0447\u0448\u0435. <\/p>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0445 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u0421\u0442\u0435\u043f\u0430\u043d\u0430 \u0416\u0434\u0430\u043d\u043e\u0432\u0430: <br \/><a href=\"https:\/\/habr.com\/ru\/articles\/822917\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/822917\/<\/a><\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <code>boxes<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u0445, \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0445 \u043a \u043a\u043b\u0430\u0441\u0441\u0443 (person, car, bus, traffic light \u0438 \u0442.\u0434.)<\/p>\n<p>\u0418\u0442\u0430\u043a, \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432\u0438\u0434\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c0b\/3a5\/5e5\/c0b3a55e5d9e359760d5ea369eec4b79.png\" width=\"1080\" height=\"526\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c0b\/3a5\/5e5\/c0b3a55e5d9e359760d5ea369eec4b79.png\"\/><\/figure>\n<p>\u041a\u0440\u043e\u043c\u0435 \u044d\u0442\u043e\u0433\u043e, \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0442\u0430\u043a\u043e\u0433\u043e \u0432\u0438\u0434\u0430:<\/p>\n<pre><code>car: Coordinates: (842, 681, 1180, 894) Coordinates: (254, 849, 524, 971) Coordinates: (49, 620, 425, 857) stop sign: Coordinates: (407, 560, 470, 626) Coordinates: (267, 494, 341, 557) traffic light: Coordinates: (334, 157, 451, 426) Coordinates: (938, 97, 1031, 312) Coordinates: (86, 481, 130, 602) person: Coordinates: (578, 711, 710, 990) Coordinates: (715, 723, 750, 798) Coordinates: (715, 852, 864, 976) Coordinates: (241, 897, 385, 1012) truck: Coordinates: (52, 620, 425, 859)<\/code><\/pre>\n<h2>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435\u043e-\u043f\u043e\u0442\u043e\u043a\u0435<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0441 \u043e\u0434\u043d\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u0438\u0434\u0435\u043e. \u0412 \u0446\u0435\u043b\u043e\u043c, \u043e\u043d\u043e \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0442.\u043a. \u0432\u0438\u0434\u0435\u043e \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439! \u041e\u0442\u043b\u0438\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u0438\u0434\u0435\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u0434\u0435\u043a\u0438 \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0430\u0442\u0430 MP4.<\/p>\n<pre><code class=\"python\">from ultralytics import YOLO import cv2 import numpy as np  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO('yolov8n.pt')  # \u0421\u043f\u0438\u0441\u043e\u043a \u0446\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 colors = [     (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255),     (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0),     (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128), (72, 61, 139),     (47, 79, 79), (47, 79, 47), (0, 206, 209), (148, 0, 211), (255, 20, 147) ]  # \u041e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0435\u043e\u0444\u0430\u0439\u043b\u0430 input_video_path = 'input.mp4' capture = cv2.VideoCapture(input_video_path)  # \u0427\u0442\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432\u0438\u0434\u0435\u043e fps = int(capture.get(cv2.CAP_PROP_FPS)) width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))  # \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 output_video_path = 'detect.mp4' fourcc = cv2.VideoWriter_fourcc(*'mp4v') writer = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))  while True:     # \u0417\u0430\u0445\u0432\u0430\u0442 \u043a\u0430\u0434\u0440\u0430     ret, frame = capture.read()     if not ret:         break      # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u0430\u0434\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0435\u043b\u0438 YOLO     results = model(frame)[0]      # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u0445     classes_names = results.names     classes = results.boxes.cls.cpu().numpy()     boxes = results.boxes.xyxy.cpu().numpy().astype(np.int32)     # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u043c\u043e\u043a \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0435\u0439 \u043d\u0430 \u043a\u0430\u0434\u0440\u0435     for class_id, box, conf in zip(classes, boxes, results.boxes.conf):         if conf>0.5:             class_name = classes_names[int(class_id)]             color = colors[int(class_id) % len(colors)]             x1, y1, x2, y2 = box             cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)             cv2.putText(frame, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)      # \u0417\u0430\u043f\u0438\u0441\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u0430\u0434\u0440\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b     writer.write(frame)  # \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u043e\u043a\u043e\u043d capture.release() writer.release() <\/code><\/pre>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/666d1f27ee339fd48b1f9509\" data-style=\"\" id=\"666d1f27ee339fd48b1f9509\" width=\"\"><\/div>\n<p>\u0414\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0432\u0435\u0431 \u043a\u0430\u043c\u0435\u0440\u044b &#8212; \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0435<br \/><code>capture = cv2.VideoCapture(input_video_path)<br \/><\/code>\u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u043c\u0435\u0440\u044b \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<code><br \/>capture = cv2.VideoCapture(0)<br \/><\/code>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430: <br \/><a href=\"https:\/\/github.com\/stepanburmistrov\/YoloV8\/blob\/main\/Detection_Camera.py\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/stepanburmistrov\/YoloV8\/blob\/main\/Detection_Camera.py<\/a><\/p>\n<h2>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f &#8212; \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u043b\u0430\u0441\u0441\u044b. \u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u043d\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0441 \u0444\u043e\u0442\u043e\/\u0432\u0438\u0434\u0435\u043e.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044e (\u043a\u0430\u0434\u0440 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0432\u0438\u0434\u0435\u043e) \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c YoloV8 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0435\u0439 \u0432\u044b\u0431\u043e\u0440 \u0435\u0441\u0442\u044c:<br \/><code>yolov8n-seg.pt<\/code>\u00a0<code>yolov8s-seg.pt<\/code>\u00a0<code>yolov8m-seg.pt<\/code>\u00a0<code>yolov8l-seg.pt<\/code>\u00a0<code>yolov8x-seg.pt<\/code>  <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/475\/b79\/786\/475b79786817c86f34504c571d07ecc7.png\" width=\"897\" height=\"607\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/475\/b79\/786\/475b79786817c86f34504c571d07ecc7.png\"\/><\/figure>\n<pre><code class=\"python\">import cv2 import numpy as np from ultralytics import YOLO import os  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO('yolov8x-seg.pt')  colors = [     (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255),     (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0),     (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128), (72, 61, 139),     (47, 79, 79), (47, 79, 47), (0, 206, 209), (148, 0, 211), (255, 20, 147) ]  def process_image(image_path):     # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u043f\u0430\u043f\u043a\u0438 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     if not os.path.exists('results'):         os.makedirs('results')          # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     image = cv2.imread(image_path)     image_orig = image.copy()     h_or, w_or = image.shape[:2]     image = cv2.resize(image, (640, 640))     results = model(image)[0]          classes_names = results.names     classes = results.boxes.cls.cpu().numpy()     masks = results.masks.data.cpu().numpy()      # \u041d\u0430\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043e\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435     for i, mask in enumerate(masks):         color = colors[int(classes[i]) % len(colors)]                  # \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043c\u0430\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0446\u0432\u0435\u0442\u043d\u043e\u0439 \u043c\u0430\u0441\u043a\u0438         mask_resized = cv2.resize(mask, (w_or, h_or))                  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0446\u0432\u0435\u0442\u043d\u043e\u0439 \u043c\u0430\u0441\u043a\u0438         color_mask = np.zeros((h_or, w_or, 3), dtype=np.uint8)         color_mask[mask_resized > 0] = color          # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b         mask_filename = os.path.join('results', f\"{classes_names[classes[i]]}_{i}.png\")         cv2.imwrite(mask_filename, color_mask)          # \u041d\u0430\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435         image_orig = cv2.addWeighted(image_orig, 1.0, color_mask, 0.5, 0)       # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     new_image_path = os.path.join('results', os.path.splitext(os.path.basename(image_path))[0] + '_segmented' + os.path.splitext(image_path)[1])     cv2.imwrite(new_image_path, image_orig)     print(f\"Segmented image saved to {new_image_path}\")  process_image('segmentation_test.png') <\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a05\/b77\/e8a\/a05b77e8a8c8a82220f0660053c0c4fe.png\" alt=\"\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e YoloV8\" title=\"\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e YoloV8\" width=\"897\" height=\"607\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a05\/b77\/e8a\/a05b77e8a8c8a82220f0660053c0c4fe.png\"\/><\/p>\n<div><figcaption>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e YoloV8<\/figcaption><\/div>\n<\/figure>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0432 \u043f\u0430\u043f\u043a\u0443 results \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043c\u0430\u0441\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b45\/848\/807\/b45848807523fa45b5dce42cf3e1a6a4.png\" width=\"1087\" height=\"597\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b45\/848\/807\/b45848807523fa45b5dce42cf3e1a6a4.png\"\/><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043d\u0430\u0434 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c \u0444\u043e\u043d\u0430 &#8212; \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u0444\u043e\u043d \u043d\u0430 \u0437\u0435\u043b\u0435\u043d\u044b\u0439. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 \u0434\u043b\u044f \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u043e\u0442\u043e \u0438\u043b\u0438 \u0432\u0438\u0434\u0435\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u043c\u044b\u0442\u044c \u043a\u0440\u0430\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0435\u0441\u044c\u043c\u0430 \u0434\u043e\u0441\u0442\u043e\u0439\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442!<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430, \u0440\u0435\u0448\u0430\u044e\u0449\u0435\u0433\u043e \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<pre><code class=\"python\">import cv2 import numpy as np from ultralytics import YOLO import os  model = YOLO('yolov8n-seg.pt')  # \u0426\u0432\u0435\u0442 \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 \"person\" person_color = (0, 255, 0)  # \u0417\u0435\u043b\u0435\u043d\u044b\u0439 \u0446\u0432\u0435\u0442  def process_image(image_path):     frame = cv2.imread(image_path)     if frame is None:         print(\"\u041e\u0448\u0438\u0431\u043a\u0430: \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\")         return      image_orig = frame.copy()     h_or, w_or = frame.shape[:2]     image = cv2.resize(frame, (640, 640))     results = model(image)[0]      classes = results.boxes.cls.cpu().numpy()     masks = results.masks.data.cpu().numpy()      # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0437\u0435\u043b\u0435\u043d\u044b\u0439 \u0444\u043e\u043d     green_background = np.zeros_like(image_orig)     green_background[:] = person_color      # \u041d\u0430\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043e\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435     for i, mask in enumerate(masks):         class_name = results.names[int(classes[i])]         if class_name == 'person':             color_mask = np.zeros((640, 640, 3), dtype=np.uint8)             resized_mask = cv2.resize(mask, (640, 640), interpolation=cv2.INTER_NEAREST)             color_mask[resized_mask > 0] = person_color              color_mask = cv2.resize(color_mask, (w_or, h_or), interpolation=cv2.INTER_NEAREST)              mask_resized = cv2.resize(mask, (w_or, h_or), interpolation=cv2.INTER_NEAREST)             green_background[mask_resized > 0] = image_orig[mask_resized > 0]      # \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u0430 '_segmented'     base_name, ext = os.path.splitext(image_path)     output_path = f\"{base_name}_removed_BG{ext}\"     cv2.imwrite(output_path, green_background)     print(f\"Processed image saved to {output_path}\")      cv2.imshow('Processed Image', green_background)  # \u041f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435     cv2.waitKey(0)     cv2.destroyAllWindows()  # \u041f\u0443\u0442\u044c \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c image_path = 'test.jpg' process_image(image_path) <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5aa\/88a\/3f6\/5aa88a3f66e1c56567b1395997ddb948.png\" alt=\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u043d\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 YOLOV8\" title=\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u043d\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 YOLOV8\" width=\"1262\" height=\"733\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5aa\/88a\/3f6\/5aa88a3f66e1c56567b1395997ddb948.png\"\/><\/p>\n<div><figcaption>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u043d\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 YOLOV8<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e \u043a\u043e\u0434\u0443 \u0435\u0441\u0442\u044c \u0432\u0430\u0436\u043d\u043e\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u0434 &#171;\u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u043d\u0438\u0435\u043c&#187; \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0440\u0443 640*640 px.<br \/>\u0412 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u0430\u0441\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0435\u0449\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u2014 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0442\u0438\u043a\u0435\u0440\u043e\u0432:<br \/><a href=\"https:\/\/github.com\/stepanburmistrov\/YoloV8\/blob\/main\/Segmentation_Image2Sticker.py\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/stepanburmistrov\/YoloV8\/blob\/main\/Segmentation_Image2Sticker.py<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fa8\/fbd\/ce3\/fa8fbdce3783e0ccb00b67e9de10d187.png\" alt=\"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0442\u0438\u043a\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 YoloV8\" title=\"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0442\u0438\u043a\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 YoloV8\" width=\"1393\" height=\"733\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fa8\/fbd\/ce3\/fa8fbdce3783e0ccb00b67e9de10d187.png\"\/><\/p>\n<div><figcaption>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0442\u0438\u043a\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 YoloV8<\/figcaption><\/div>\n<\/figure>\n<h2>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u044c\u0441\u044f \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443.<\/p>\n<p>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u0438 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0440\u0435\u0448\u0430\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u0432\u043e\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u0412\u043e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b, \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u044e \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p><strong>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438<\/strong>:<\/p>\n<ul>\n<li>\n<p><strong>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong>: \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a \u043a\u0430\u043a\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0432\u0435\u0441\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043b\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u0438\u043a\u043e\u043c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u043a\u0430\u043a &#171;\u0441\u043e\u0431\u0430\u043a\u0430&#187; \u0438\u043b\u0438 &#171;\u043a\u043e\u0448\u043a\u0430&#187;.<\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f<\/strong>: \u041d\u0430\u0445\u043e\u0434\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u044b \u0438 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0433\u0434\u0435 \u043d\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043e\u0431\u0430\u043a\u0430 \u0438 \u0433\u0434\u0435 \u043a\u043e\u0448\u043a\u0430).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u0420\u0435\u0441\u0443\u0440\u0441\u044b \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u044c<\/strong>:<\/p>\n<ul>\n<li>\n<p><strong>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong>: \u041e\u0431\u044b\u0447\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432\u0435\u0441\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u043b\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f<\/strong>: \u0411\u043e\u043b\u0435\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0433\u0440\u0430\u043d\u0438\u0446.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/strong>:<\/p>\n<ul>\n<li>\n<p><strong>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong>: \u0411\u044b\u0441\u0442\u0440\u0435\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f<\/strong>: \u041c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u0441\u0435\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/strong>:<\/p>\n<ul>\n<li>\n<p><strong>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/strong>: \u041f\u0440\u043e\u0449\u0435 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f<\/strong>: \u0411\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u0430 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0440\u0430\u0437\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u0444\u043e\u0440\u043c\u044b.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e<\/h4>\n<ol>\n<li>\n<p><strong>\u0426\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/strong>: \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0432\u0441\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u043b\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 \u043d\u0435 \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0435\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0447\u0442\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043e \u043d\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438 (\u0441\u043e\u0431\u0430\u043a\u0430 \u0438\u043b\u0438 \u043a\u043e\u0448\u043a\u0430).<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/strong>: \u0412 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 &#171;\u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u043e\u0432&#187;. <\/strong>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0431\u0443\u043a\u0432\u044b \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0438, \u0430 \u0437\u0430\u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438!<\/p>\n<\/li>\n<\/ol>\n<h4>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u044e<\/h4>\n<ol>\n<li>\n<p><strong>\u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438<\/strong>: \u0415\u0441\u043b\u0438 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0438 \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0438\u0445 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043a\u043b\u0430\u0441\u0441\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u043c\u043e\u0431\u0438\u043b\u0435\u0439 \u0438 \u043f\u0435\u0448\u0435\u0445\u043e\u0434\u043e\u0432 \u043d\u0430 \u0443\u043b\u0438\u0446\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0446\u0435\u043d<\/strong>: \u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u044b, \u0433\u0434\u0435 \u0432\u0430\u0436\u043d\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442, \u043d\u043e \u0438 \u0433\u0434\u0435 \u043e\u043d\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/strong>: \u0412 \u0437\u0430\u0434\u0430\u0447\u0430\u0445, \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u0432\u0438\u0434\u0435\u043e\u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">import cv2 import numpy as np from ultralytics import YOLO import os  model = YOLO('yolov8n-cls.pt')   def process_image(img):     # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u0430\u0434\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0435\u043b\u0438     results = model(img)[0]      # \u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438     if results.probs is not None:         # \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438         top1_idx = results.probs.top1  # \u0418\u043d\u0434\u0435\u043a\u0441 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043d\u0430\u0438\u0432\u044b\u0441\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e         top1_conf = results.probs.top1conf.item()  # \u0412\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043d\u0430\u0438\u0432\u044b\u0441\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e         class_name = results.names[top1_idx]  # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443          # \u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0438 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u043a\u0430\u0434\u0440\u0435         label = f\"{class_name}: {top1_conf:.2f}\"         cv2.putText(img, label, (50, 50),                     cv2.FONT_HERSHEY_SIMPLEX, 2,                     (255, 0, 0), 3)      return image  image = cv2.imread(\"dog.jpg\") image = process_image(image) cv2.imwrite('result.jpg', image)<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eb1\/32b\/9e6\/eb132b9e6c1ef4a29b0604dec59764ce.png\" alt=\"\u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043a\u043e\u043b\u043b\u0438, \u043d\u043e \u043f\u043e\u0445\u043e\u0436! \u041c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u043a\u0443\u0434\u0430 \u0443\u0447\u0438\u0442\u044c\u0441\u044f\" title=\"\u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043a\u043e\u043b\u043b\u0438, \u043d\u043e \u043f\u043e\u0445\u043e\u0436! \u041c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u043a\u0443\u0434\u0430 \u0443\u0447\u0438\u0442\u044c\u0441\u044f\" width=\"842\" height=\"733\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eb1\/32b\/9e6\/eb132b9e6c1ef4a29b0604dec59764ce.png\"\/><\/p>\n<div><figcaption>\u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043a\u043e\u043b\u043b\u0438, \u043d\u043e \u043f\u043e\u0445\u043e\u0436! \u041c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u043a\u0443\u0434\u0430 \u0443\u0447\u0438\u0442\u044c\u0441\u044f<\/figcaption><\/div>\n<\/figure>\n<h2>\u041f\u043e\u0438\u0441\u043a \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0442\u0435\u043b\u0430 &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0437\u044b \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430<\/h2>\n<p>\u0421\u043f\u043e\u0441\u043e\u0431\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e, \u0432\u043e\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043d\u0438\u0445:<\/p>\n<ul>\n<li>\n<p><strong>\u0422\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043f\u043e\u0440\u0442\u0441\u043c\u0435\u043d\u043e\u0432<\/strong>: \u041f\u043e\u043c\u043e\u0449\u044c \u0432 \u0430\u043d\u0430\u043b\u0438\u0437\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u0441\u043f\u043e\u0440\u0442\u0441\u043c\u0435\u043d\u043e\u0432 \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0438\u0445 \u0442\u0435\u0445\u043d\u0438\u043a\u0438 <\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0435\u0430\u0431\u0438\u043b\u0438\u0442\u0430\u0446\u0438\u044f<\/strong>: \u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u043f\u0430\u0446\u0438\u0435\u043d\u0442\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0430\u0431\u0438\u043b\u0438\u0442\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0443\u043f\u0440\u0430\u0436\u043d\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043f\u0430\u0434\u0435\u043d\u0438\u0439<\/strong>: \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043f\u0430\u0434\u0435\u043d\u0438\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u043f\u0430\u0441\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u0436\u0438\u043b\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 \u0438\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u043d\u0438\u043a\u043e\u0432 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u0410\u043d\u0438\u043c\u0430\u0446\u0438\u044f<\/strong>: \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0445 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0441\u043e\u043d\u0430\u0436\u0435\u0439 \u0432 \u0444\u0438\u043b\u044c\u043c\u0430\u0445 \u0438 \u0432\u0438\u0434\u0435\u043e\u0438\u0433\u0440\u0430\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/strong>: \u0420\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u043e\u0435 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 VR \u0438 AR \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0410\u043d\u0430\u043b\u0438\u0437 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/strong>: \u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0432 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u0445 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u043a\u043b\u0430\u0434\u043a\u0438 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u0426\u0438\u0444\u0440\u043e\u0432\u044b\u0435 \u0437\u0435\u0440\u043a\u0430\u043b\u0430<\/strong>: \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043a\u0430 \u043e\u0434\u0435\u0436\u0434\u044b, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c \u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043d\u0430\u0440\u044f\u0434\u0430\u0445 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043a\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f (\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043b\u0435\u0433\u043a\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432\u0438\u0434\u0435\u043e-\u0444\u0430\u0439\u043b\u0430 \u0438\u043b\u0438 \u043a\u0430\u043c\u0435\u0440\u044b):<\/p>\n<pre><code class=\"python\">from ultralytics import YOLO import cv2 import numpy as np import os  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8-Pose model = YOLO('yolov8n-pose.pt')  # \u0421\u043b\u043e\u0432\u0430\u0440\u044c \u0446\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 colors = {     'white': (255, 255, 255),     'red': (0, 0, 255),     'blue': (255, 0, 0) }  def draw_skeleton(image, keypoints, confs, pairs, color):     for (start, end) in pairs:         if confs[start] > 0.5 and confs[end] > 0.5:             x1, y1 = int(keypoints[start][0]), int(keypoints[start][1])             x2, y2 = int(keypoints[end][0]), int(keypoints[end][1])             if (x1, y1) != (0, 0) and (x2, y2) != (0, 0):  # \u0418\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u043e\u0447\u0435\u043a \u0432 (0, 0)                 cv2.line(image, (x1, y1), (x2, y2), color, 2)  def process_image(image_path):     # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     image = cv2.imread(image_path)     if image is None:         print(\"\u041e\u0448\u0438\u0431\u043a\u0430: \u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\")         return      # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0435\u043b\u0438     results = model(image)[0]      # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432     if hasattr(results, 'boxes') and hasattr(results.boxes, 'cls') and len(results.boxes.cls) > 0:         classes_names = results.names         classes = results.boxes.cls.cpu().numpy()         boxes = results.boxes.xyxy.cpu().numpy().astype(np.int32)          # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a         if results.keypoints:             keypoints = results.keypoints.data.cpu().numpy()             confs = results.keypoints.conf.cpu().numpy()                          for i, (class_id, box, kp, conf) in enumerate(zip(classes, boxes, keypoints, confs)):                 draw_box=False                 if draw_box:                     class_name = classes_names[int(class_id)]                     color = colors['white']                     x1, y1, x2, y2 = box                     cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)                     cv2.putText(image, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)                  # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438                 for j, (point, point_conf) in enumerate(zip(kp, conf)):                     if point_conf > 0.5:  # \u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438                         x, y = int(point[0]), int(point[1])                         if (x, y) != (0, 0):  # \u0418\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u043e\u0447\u0435\u043a \u0432 (0, 0)                             cv2.circle(image, (x, y), 5, colors['blue'], -1)                             cv2.putText(image, str(j), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors['blue'], 2)                  # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043a\u0435\u043b\u0435\u0442\u0430                 draw_skeleton(image, kp, conf, [(5, 7), (7, 9), (6, 8), (8, 10)], colors['white']) # \u0420\u0443\u043a\u0438                 draw_skeleton(image, kp, conf, [(11, 13), (13, 15), (12, 14), (14, 16)], colors['red']) # \u041d\u043e\u0433\u0438                 draw_skeleton(image, kp, conf, [(5, 11), (6, 12)], colors['blue']) # \u0422\u0435\u043b\u043e      # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     output_path = os.path.splitext(image_path)[0] + \"_pose_detected.jpg\"     cv2.imwrite(output_path, image)     print(f\"\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438: {output_path}\")      cv2.imshow('YOLOv8-Pose Detection', image)     cv2.waitKey(0)     cv2.destroyAllWindows()  # \u041f\u0443\u0442\u044c \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 image_path = 'd.jpg' process_image(image_path) <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/52a\/3e3\/844\/52a3e3844b9698920e2122dd87acfccc.png\" width=\"866\" height=\"733\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/52a\/3e3\/844\/52a3e3844b9698920e2122dd87acfccc.png\"\/><\/figure>\n<h2>\u0422\u0440\u0435\u043a\u0438\u043d\u0433 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/h2>\n<p>\u0422\u0440\u0435\u043a\u0438\u043d\u0433 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435\u043e \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043d\u043e\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0438 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0442\u0435\u0447\u0435\u043d\u0438\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0445\u043e\u0434\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u0445.<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/666dbd0ad1b5b66d8de272fa\" data-style=\"\" id=\"666dbd0ad1b5b66d8de272fa\" width=\"\"><\/div>\n<h4>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432:<\/h4>\n<ul>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0438 \u0432\u0438\u0434\u0435\u043e\u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435<\/strong>: \u041e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u043b\u044e\u0434\u0435\u0439 \u0434\u043b\u044f \u0432\u044b\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435 \u043e \u043f\u0440\u043e\u043f\u0430\u0432\u0448\u0438\u0445 \u0438\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0431\u0435\u0437 \u043f\u0440\u0438\u0441\u043c\u043e\u0442\u0440\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u0422\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442 \u0438 \u043b\u043e\u0433\u0438\u0441\u0442\u0438\u043a\u0430<\/strong>: \u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u043c \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435\u043c, \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f, \u0430\u0432\u0442\u043e\u043d\u043e\u043c\u043d\u044b\u0435 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0438 \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u043e\u0437\u043d\u0438\u0447\u043d\u0430\u044f \u0442\u043e\u0440\u0433\u043e\u0432\u043b\u044f<\/strong>: \u0410\u043d\u0430\u043b\u0438\u0437 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043f\u043e\u043a\u0443\u043f\u0430\u0442\u0435\u043b\u0435\u0439 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0442\u043e\u0432\u0430\u0440\u043e\u0432, \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043e\u043f\u044b\u0442\u0430 \u0438 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043a\u0440\u0430\u0436\u043d\u044b\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u043f\u043e\u0434\u043e\u0437\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043f\u043e\u0440\u0442 \u0438 \u0430\u043d\u0430\u043b\u0438\u0437 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/strong>: \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043f\u043e\u0440\u0442\u0438\u0432\u043d\u044b\u0445 \u043c\u0435\u0440\u043e\u043f\u0440\u0438\u044f\u0442\u0438\u0439, \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0438\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0438 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430 \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0438 \u0441\u043f\u043e\u0440\u0442\u0441\u043c\u0435\u043d\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u041c\u0435\u0434\u0438\u0446\u0438\u043d\u0430 \u0438 \u0437\u0434\u0440\u0430\u0432\u043e\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435<\/strong>: \u0420\u0435\u0430\u0431\u0438\u043b\u0438\u0442\u0430\u0446\u0438\u044f, \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u043f\u0430\u0446\u0438\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0430, \u0430\u043d\u0430\u043b\u0438\u0437 \u043f\u043e\u0445\u043e\u0434\u043a\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0432\u044b\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0439 \u0438 \u0437\u0430\u0431\u043e\u043b\u0435\u0432\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u043e\u0431\u043e\u0442\u043e\u0442\u0435\u0445\u043d\u0438\u043a\u0430 \u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u043b\u043e\u0432\u0435\u043a-\u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440<\/strong>: \u041d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u0440\u043e\u0431\u043e\u0442\u043e\u0432, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u0440\u043e\u0431\u043e\u0442\u043e\u0432 \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0440\u0435\u0434\u0435, \u0436\u0435\u0441\u0442\u043e\u0432\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0438\u0434\u0435\u043e:<\/p>\n<pre><code class=\"python\">from collections import defaultdict import cv2 import numpy as np from ultralytics import YOLO  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO(\"yolov8x.pt\")  # \u041e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0432\u0438\u0434\u0435\u043e \u0444\u0430\u0439\u043b\u0430 video_path = \"input.mp4\" cap = cv2.VideoCapture(video_path)  # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0432\u0438\u0434\u0435\u043e if not cap.isOpened():     print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f {video_path}\")     exit()   # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 FPS \u0432\u0438\u0434\u0435\u043e fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  # \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 VideoWriter \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0435\u043e fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter('out.mp4', fourcc, fps, (width, height))  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u0442\u0440\u0435\u043a\u043e\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 track_history = defaultdict(lambda: [])  # \u0426\u0438\u043a\u043b \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u0430\u0434\u0440\u0430 \u0432\u0438\u0434\u0435\u043e while cap.isOpened():     # \u0421\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u043a\u0430\u0434\u0440\u0430 \u0438\u0437 \u0432\u0438\u0434\u0435\u043e     success, frame = cap.read()      if not success:         print(\"\u041a\u043e\u043d\u0435\u0446 \u0432\u0438\u0434\u0435\u043e\")         break      # \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 YOLOv8 \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 \u043a\u0430\u0434\u0440\u0435, \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0440\u0435\u043a\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043a\u0430\u0434\u0440\u0430\u043c\u0438     results = model.track(frame, persist=True)      # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432     if results[0].boxes is not None and results[0].boxes.id is not None:         # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u0431\u043e\u043a\u0441\u043e\u0432 \u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0442\u0440\u0435\u043a\u043e\u0432         boxes = results[0].boxes.xywh.cpu()  # xywh \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u0431\u043e\u043a\u0441\u043e\u0432         track_ids = results[0].boxes.id.int().cpu().tolist()  # \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0442\u0440\u0435\u043a\u043e\u0432          # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043d\u0430 \u043a\u0430\u0434\u0440\u0435         annotated_frame = results[0].plot()          # \u041e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0430 \u0442\u0440\u0435\u043a\u043e\u0432         for box, track_id in zip(boxes, track_ids):             x, y, w, h = box  # \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u0446\u0435\u043d\u0442\u0440\u0430 \u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0431\u043e\u043a\u0441\u0430             track = track_history[track_id]             track.append((float(x), float(y)))  # \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u0446\u0435\u043d\u0442\u0440\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 \u0438\u0441\u0442\u043e\u0440\u0438\u044e             if len(track) > 30:  # \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u0434\u043e 30 \u043a\u0430\u0434\u0440\u043e\u0432                 track.pop(0)              # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0438\u043d\u0438\u0439 \u0442\u0440\u0435\u043a\u0430             points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))             cv2.polylines(annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)          # \u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u0430\u0434\u0440\u0430         cv2.imshow(\"YOLOv8 Tracking\", annotated_frame)         out.write(annotated_frame)  # \u0437\u0430\u043f\u0438\u0441\u044c \u043a\u0430\u0434\u0440\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0432\u0438\u0434\u0435\u043e     else:         # \u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u044b, \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043a\u0430\u0434\u0440         cv2.imshow(\"YOLOv8 Tracking\", frame)         out.write(frame)  # \u0437\u0430\u043f\u0438\u0441\u044c \u043a\u0430\u0434\u0440\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0432\u0438\u0434\u0435\u043e      # \u041f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u043f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043a\u043b\u0430\u0432\u0438\u0448\u0438 'Esc'     if cv2.waitKey(1) == 27:         break  # \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u0438\u0434\u0435\u043e\u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u0438 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u0432\u0441\u0435\u0445 \u043e\u043a\u043e\u043d OpenCV cap.release() out.release()  # \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0435\u043e\u0444\u0430\u0439\u043b\u0430 cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u043a\u0438\u043d\u0433 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f:<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 NumPy, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u043b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044e \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a. \u042d\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u0443\u0441\u0440\u0435\u0434\u043d\u0438\u0442\u044c \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0435\u0433\u043e \u0431\u0443\u0434\u0443\u0449\u0438\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b.<\/p>\n<p>\u0412\u043e\u0442 \u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>predict_position<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043e\u0432 \u0434\u043b\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043b\u0438\u043d\u0438\u0438, \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0442\u043e\u0447\u043a\u0438 \u043f\u0443\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0438 \u044d\u043a\u0441\u0442\u0440\u0430\u043f\u043e\u043b\u044f\u0446\u0438\u044e \u044d\u0442\u043e\u0439 \u043b\u0438\u043d\u0438\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">def predict_position(track, future_time, fps):     if len(track) &lt; 2:         return track[-1]      N = min(len(track), 25)     track = np.array(track[-N:])      times = np.arange(-N + 1, 1)      A = np.vstack([times, np.ones(len(times))]).T     k_x, b_x = np.linalg.lstsq(A, track[:, 0], rcond=None)[0]     k_y, b_y = np.linalg.lstsq(A, track[:, 1], rcond=None)[0]      future_frames = future_time * fps     future_x = k_x * future_frames + b_x     future_y = k_y * future_frames + b_y <\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0442\u043e\u0436\u0435 \u0432\u0438\u0434\u0435\u043e, \u043d\u043e \u0443\u0436\u0435 \u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f:<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/666dc04e28dfc09d9e478801\" data-style=\"\" id=\"666dc04e28dfc09d9e478801\" width=\"\"><\/div>\n<p> <a href=\"https:\/\/Tracking_Prediction_Video.py\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u0434 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0438\u0434\u0435\u043e \u0441 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c<\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430<\/h2>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u2014 \u043a\u0430\u043a \u0436\u0435 \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c YOLOV8 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0432\u0442\u043e\u043c\u043e\u0431\u0438\u043b\u0438 \u0438 \u0441\u043e\u0431\u0430\u0447\u0435\u043a, \u043d\u043e \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c.<br \/>\u041f\u043e\u0447\u0435\u043c\u0443 \u0434\u043e\u043e\u0431\u0443\u0447\u0438\u0442\u044c? \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0435 \u0432\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0441\u043b\u043e\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0442\u0438\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438! \u041d\u0430\u0447\u043d\u0435\u043c!<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u043b\u0443\u0447\u0448\u0430\u0442\u044c, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438\u0445 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f!<br \/>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043d\u0430\u0448\u0435\u0433\u043e \u0433\u0435\u0440\u043e\u044f &#8212; &#171;\u041a\u0443\u0431\u0438\u043a&#187; (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0435\u043f\u0438\u043f\u0435\u0434). \u0418\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u043c\u044b \u043d\u0430\u0443\u0447\u0438\u043c \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c!<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/132\/650\/3e1\/1326503e14520f51a4e2d3d8104457c1.png\" width=\"365\" height=\"370\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/132\/650\/3e1\/1326503e14520f51a4e2d3d8104457c1.png\"\/><\/figure>\n<p>\u2014 \u0421\u0440\u0430\u0437\u0443 \u043e\u0442\u0432\u0435\u0447\u0443 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441: &#171;\u0410 \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 OpenCV? \u0412\u0435\u0434\u044c \u0435\u0435 \u0442\u0443\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e&#187;. <br \/>\u2014 \u0414\u0430, \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0437\u043c\u0435\u0442\u0438\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442. \u0410 \u043d\u0435 \u0435\u0435, \u0442.\u043a. \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b!<\/p>\n<p>\u0421\u043d\u0438\u043c\u0430\u0435\u043c \u0432\u0438\u0434\u0435\u043e \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044f \u0442\u0430\u043a\u0436\u0435 \u043a\u0430\u0434\u0440\u044b, \u0433\u0434\u0435 \u0435\u0433\u043e \u043d\u0435\u0442, \u0442.\u0435. \u043f\u0440\u043e\u0441\u0442\u043e \u0444\u043e\u043d! <\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/666e6d422331bb9d71f15554\" data-style=\"\" id=\"666e6d422331bb9d71f15554\" width=\"\"><\/div>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/p>\n<pre><code class=\"python\">import cv2 import os import time  # \u041f\u0443\u0442\u044c \u043a \u0432\u0438\u0434\u0435\u043e\u0444\u0430\u0439\u043b\u0443 video_path = '000.mp4' # \u041f\u0430\u043f\u043a\u0430 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 output_folder = 'output_images' # \u0418\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043c\u0435\u0436\u0434\u0443 \u043a\u0430\u0434\u0440\u0430\u043c\u0438 (\u043a\u0430\u0436\u0434\u044b\u0439 n-\u0439 \u043a\u0430\u0434\u0440 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d) frame_interval = 1  # \u041c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 2, 5 \u0438 \u0442.\u0434.  os.makedirs(output_folder, exist_ok=True)  # \u041e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0432\u0438\u0434\u0435\u043e\u0444\u0430\u0439\u043b\u0430 cap = cv2.VideoCapture(video_path) if not cap.isOpened():     print(f\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0432\u0438\u0434\u0435\u043e\u0444\u0430\u0439\u043b\u0430: {video_path}\")     exit()  frame_count = 0 saved_count = 0  while True:     ret, frame = cap.read()     if not ret:         break      if frame_count % frame_interval == 0:         # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432 \u0432\u0438\u0434\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u0438         timestamp = int(time.time() * 1000)  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u044b \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438         output_path = os.path.join(output_folder, f'{timestamp}_frame_{saved_count:05d}.jpg')         cv2.imwrite(output_path, frame)         print(f\"\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043e: {output_path}\")         saved_count += 1      frame_count += 1  cap.release() print(\"\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u043e.\") <\/code><\/pre>\n<p>\u0418 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0439. \u0421\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0445 \u043d\u0443\u0436\u043d\u043e? \u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e \u0434\u0430\u0442\u044c, \u0442.\u043a. \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c\u0441\u044f.<br \/>\u0422\u043e\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0447\u0435\u043c \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u0435\u0435 \u0431\u0443\u0434\u0443\u0442 \u043a\u0430\u0434\u0440\u044b \u0441 \u043e\u043a\u0440\u0443\u0436\u0430\u044e\u0449\u0435\u0439 \u043e\u0431\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438), \u0442\u0435\u043c \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c!<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b2b\/f22\/ada\/b2bf22ada4c147b7413400cdfc282c96.png\" width=\"2592\" height=\"802\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b2b\/f22\/ada\/b2bf22ada4c147b7413400cdfc282c96.png\"\/><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0442\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0442.\u0435. \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043c\u0435\u0441\u0442\u0430, \u0433\u0434\u0435 \u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043a\u043e\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<h2>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0430<\/h2>\n<p>\u0422.\u043a. \u0434\u0430\u043d\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0432 &#171;\u0442\u0435\u043f\u043b\u0438\u0447\u043d\u044b\u0445&#187; \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0442\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c OpenCV. \u0412\u043e\u0437\u044c\u043c\u0435\u043c \u043e\u0434\u0438\u043d \u0438\u0437 \u043a\u0430\u0434\u0440\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c \u0434\u043b\u044f \u043f\u043e\u0434\u0431\u043e\u0440\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u043e\u0432 \u0432 \u0446\u0432\u0435\u0442\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 HSV, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u0443\u0431\u0438\u043a \u0438\u0437 \u0444\u043e\u043d\u0430:<\/p>\n<pre><code class=\"python\">import cv2 import numpy as np  def nothing(*arg):     pass  cv2.namedWindow( \"result\" )  cv2.namedWindow( \"settings\" )   cv2.createTrackbar('h1', 'settings', 0, 180, nothing) cv2.createTrackbar('s1', 'settings', 0, 255, nothing) cv2.createTrackbar('v1', 'settings', 0, 255, nothing) cv2.createTrackbar('h2', 'settings', 180, 180, nothing) cv2.createTrackbar('s2', 'settings', 255, 255, nothing) cv2.createTrackbar('v2', 'settings', 255, 255, nothing)  while True:     img = cv2.imread('000.jpg')     h,w,_=img.shape     img=cv2.resize(img,(w\/\/5,h\/\/5))     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV )       # \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0431\u0435\u0433\u0443\u043d\u043a\u043e\u0432     h1 = cv2.getTrackbarPos('h1', 'settings')     s1 = cv2.getTrackbarPos('s1', 'settings')     v1 = cv2.getTrackbarPos('v1', 'settings')     h2 = cv2.getTrackbarPos('h2', 'settings')     s2 = cv2.getTrackbarPos('s2', 'settings')     v2 = cv2.getTrackbarPos('v2', 'settings')     h_min = np.array((h1, s1, v1), np.uint8)     h_max = np.array((h2, s2, v2), np.uint8)     img_bin = cv2.inRange(hsv, h_min, h_max)     cv2.imshow('result', img_bin)     cv2.imshow('original', img)     ch = cv2.waitKey(5)     if ch == 27:         break cv2.destroyAllWindows() <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/abf\/b40\/f1a\/abfb40f1ad678ff418b45e0dd29fefa2.png\" width=\"854\" height=\"560\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/abf\/b40\/f1a\/abfb40f1ad678ff418b45e0dd29fefa2.png\"\/><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<br \/><code>lower_hsv = np.array([64, 54, 167])<br \/>upper_hsv = np.array([180, 255, 255])<\/code><\/p>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043a\u043e \u0432\u0441\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. <br \/>\u0412\u0410\u0416\u041d\u041e! \u042d\u0442\u043e\u0442 \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 &#8212; \u0433\u0434\u0435 \u0432 \u043a\u0430\u0434\u0440\u0435 \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u0432 \u0445\u043e\u0440\u043e\u0448\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u0414\u043b\u044f \u0440\u0443\u0447\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0438 \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u0430\u043b\u044c\u0448\u0435!<\/p>\n<pre><code class=\"python\">import cv2 import os import numpy as np  input_folder = 'output_images' output_folder = 'dataset\/train' output_images_folder = os.path.join(output_folder, 'images') output_labels_folder = os.path.join(output_folder, 'labels')  os.makedirs(output_images_folder, exist_ok=True) os.makedirs(output_labels_folder, exist_ok=True)  lower_hsv = np.array([89, 71, 120]) upper_hsv = np.array([180, 255, 255])  def find_mask(image, lower_hsv, upper_hsv):     hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)     mask = cv2.inRange(hsv_image, lower_hsv, upper_hsv)     return mask  def find_bounding_rect(mask):     contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)     if contours:         contour = max(contours, key=cv2.contourArea)         x, y, w, h = cv2.boundingRect(contour)         return x, y, x + w, y + h     else:         return None  def normalize_coordinates(x1, y1, x2, y2, img_width, img_height):     x_center = (x1 + x2) \/ 2 \/ img_width     y_center = (y1 + y2) \/ 2 \/ img_height     width = abs(x2 - x1) \/ img_width     height = abs(y2 - y1) \/ img_height     return x_center, y_center, width, height  for filename in os.listdir(input_folder):     if filename.endswith(('.jpg', '.jpeg', '.png')):         image_path = os.path.join(input_folder, filename)         image = cv2.imread(image_path)         resized_image = cv2.resize(image, (640,640))         mask = find_mask(resized_image, lower_hsv, upper_hsv)         bounding_rect = find_bounding_rect(mask)          if bounding_rect is not None:              x1, y1, x2, y2 = bounding_rect             x_center, y_center, width, height = normalize_coordinates(x1, y1, x2, y2, 640, 640)              # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f             output_image_path = os.path.join(output_images_folder, filename)             cv2.imwrite(output_image_path, resized_image)              # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435              label_filename = os.path.splitext(filename)[0] + '.txt'             label_file_path = os.path.join(output_labels_folder, label_filename)             with open(label_file_path, 'w') as f:                 f.write(f\"0 {x_center} {y_center} {width} {height}\\n\")              print(f\"Processed and saved {filename}\")  print(\"\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430.\") <\/code><\/pre>\n<p>\u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0430\u0439\u043b\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0434\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 640*640 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0432 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c. \u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f TXT \u0444\u0430\u0439\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<br \/>\u0412\u0430\u0436\u043d\u043e, \u0447\u0442\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0435 \u0432 \u043f\u0438\u043a\u0441\u0435\u043b\u044f\u0445, \u0430 \u0432 \u0432\u0438\u0434\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043e\u0442 0 \u0434\u043e 1, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0442\u043e\u0447\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0430\u0434\u0440\u0430. <\/p>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0430\u043f\u043e\u043a \u043d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>dataset\/ \u251c\u2500\u2500 train\/ \u2502   \u251c\u2500\u2500 images\/ \u2502   \u2502   \u251c\u2500\u2500 img1.jpg \u2502   \u2502   \u251c\u2500\u2500 img2.jpg \u2502   \u2502   \u251c\u2500\u2500 ... \u2502   \u251c\u2500\u2500 labels\/ \u2502   \u2502   \u251c\u2500\u2500 img1.txt \u2502   \u2502   \u251c\u2500\u2500 img2.txt \u2502   \u2502   \u251c\u2500\u2500 ... \u251c\u2500\u2500 output_images\/ \u2502   \u251c\u2500\u2500 img1.jpg \u2502   \u251c\u2500\u2500 img2.jpg \u2502   \u251c\u2500\u2500 ... \u251c\u2500\u2500 Dataset_video2images.py \u2514\u2500\u2500 Dataset_HSV_Markup.py <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0438 \u0445\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0430 \u043a\u0430\u043a \u0436\u0435 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0431\u044b\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u044b \u0434\u0430\u043d\u043d\u044b\u0435!<\/p>\n<pre><code class=\"python\">import cv2 import os  # \u041f\u0430\u043f\u043a\u0438 \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u0438 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 images_path = 'dataset\/train\/images' labels_path = 'dataset\/train\/labels'  # \u041f\u0430\u043f\u043a\u0430 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430\u043c\u0438 output_folder = 'checked_images' os.makedirs(output_folder, exist_ok=True)  # \u0426\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432 (\u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u0446\u0432\u0435\u0442\u043e\u0432, \u0435\u0441\u043b\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0431\u043e\u043b\u044c\u0448\u0435) colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]  # \u0427\u0442\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438 \u043c\u0435\u0442\u043e\u043a images = [f for f in os.listdir(images_path) if f.endswith(('.jpg', '.jpeg', '.png'))]  # \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u0438\u0437 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u043f\u0438\u043a\u0441\u0435\u043b\u0438 def denormalize_coordinates(x_center, y_center, width, height, img_width, img_height):     x_center *= img_width     y_center *= img_height     width *= img_width     height *= img_height     x1 = int(x_center - width \/ 2)     y1 = int(y_center - height \/ 2)     x2 = int(x_center + width \/ 2)     y2 = int(y_center + height \/ 2)     return x1, y1, x2, y2  # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 for image_file in images:     image_path = os.path.join(images_path, image_file)     label_file = os.path.splitext(image_file)[0] + '.txt'     label_path = os.path.join(labels_path, label_file)      # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u043c\u0435\u0442\u043e\u043a     if not os.path.exists(label_path):         print(f\"Label file not found for image: {image_file}\")         continue      # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     image = cv2.imread(image_path)     img_height, img_width = image.shape[:2]      # \u0427\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043c\u0435\u0442\u043e\u043a \u0438 \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u043e\u0432     with open(label_path, 'r') as f:         for line in f:             cls, x_center, y_center, width, height = map(float, line.strip().split())             x1, y1, x2, y2 = denormalize_coordinates(x_center, y_center, width, height, img_width, img_height)             color = colors[int(cls) % len(colors)]             cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)             cv2.putText(image, f'class {int(cls)}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)      # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430\u043c\u0438     output_path = os.path.join(output_folder, image_file)     cv2.imwrite(output_path, image)     print(f\"Saved checked image: {output_path}\")  print(\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430.\") <\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0438 \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u0432 \u043f\u0430\u043f\u043a\u0435 checked_images \u043b\u0435\u0436\u0430\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u042d\u0442\u0438 \u0440\u0430\u043c\u043a\u0438 \u0432\u043e\u043a\u0440\u0443\u0433 \u043a\u0443\u0431\u0438\u043a\u043e\u0432 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 labels, \u0447\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/42a\/a66\/019\/42aa660196ecd10160b4a971a2ed9c72.png\" width=\"1570\" height=\"607\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/42a\/a66\/019\/42aa660196ecd10160b4a971a2ed9c72.png\"\/><\/figure>\n<h2>\u0420\u0443\u0447\u043d\u0430\u044f \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0430<\/h2>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0443\u0447\u043d\u0430\u044f \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0430. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043e\u043d\u043b\u0430\u0439\u043d-\u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434. \u041f\u0438\u0448\u0435\u043c \u0441\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">import cv2 import os import yaml import shutil  # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435 \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 full_images_path = 'output_images' # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 dataset_path = 'dataset' train_images_path = os.path.join(dataset_path, 'train', 'images') train_labels_path = os.path.join(dataset_path, 'train', 'labels') valid_images_path = os.path.join(dataset_path, 'valid', 'images') valid_labels_path = os.path.join(dataset_path, 'valid', 'labels') test_images_path = os.path.join(dataset_path, 'test', 'images') test_labels_path = os.path.join(dataset_path, 'test', 'labels') ready_images_path = os.path.join(full_images_path, 'ready')  os.makedirs(train_images_path, exist_ok=True) os.makedirs(train_labels_path, exist_ok=True) os.makedirs(valid_images_path, exist_ok=True) os.makedirs(valid_labels_path, exist_ok=True) os.makedirs(test_images_path, exist_ok=True) os.makedirs(test_labels_path, exist_ok=True) os.makedirs(ready_images_path, exist_ok=True)   window_name = 'Annotation Tool' current_class = 0 colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] annotations = []  # \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f def resize_image(image, size=(640, 640)):     return cv2.resize(image, size)  # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043c\u044b\u0448\u0438 drawing = False ix, iy = -1, -1  def draw_rectangle(event, x, y, flags, param):     global ix, iy, drawing, annotations, current_class          if event == cv2.EVENT_LBUTTONDOWN:         drawing = True         ix, iy = x, y     elif event == cv2.EVENT_MOUSEMOVE:         if drawing:             image = param['original_image'].copy()             for annotation in annotations:                 cls, x1, y1, x2, y2 = annotation                 cv2.rectangle(image, (x1, y1), (x2, y2), colors[cls], 2)             cv2.rectangle(image, (ix, iy), (x, y), colors[current_class], 2)             cv2.imshow(window_name, image)     elif event == cv2.EVENT_LBUTTONUP:         drawing = False         annotations.append((current_class, ix, iy, x, y))         image = param['original_image'].copy()         for annotation in annotations:             cls, x1, y1, x2, y2 = annotation             cv2.rectangle(image, (x1, y1), (x2, y2), colors[cls], 2)         cv2.imshow(window_name, image)     elif event == cv2.EVENT_RBUTTONDOWN:  # \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0440\u0430\u043c\u043a\u0438         if annotations:             removed_annotation = annotations.pop()             image = param['original_image'].copy()  # \u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e             for annotation in annotations:                 cls, x1, y1, x2, y2 = annotation                 cv2.rectangle(image, (x1, y1), (x2, y2), colors[cls], 2)             cv2.imshow(window_name, image)         else:             image = param['original_image'].copy()             cv2.imshow(window_name, image)  # \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 data.yaml def update_data_yaml():     data_yaml_path = 'data.yaml'     data = {         'train': 'dataset\/train\/images',         'val': 'dataset\/valid\/images',         'test': 'dataset\/test\/images',         'nc': 4,         'names': ['class0', 'class1', 'class2', 'class3']     }     with open(data_yaml_path, 'w') as f:         yaml.dump(data, f, default_flow_style=None, sort_keys=False)     print(f\"Updated {data_yaml_path}\")  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 for filename in os.listdir(full_images_path):     if filename.endswith(('.jpg', '.jpeg', '.png')):         image_path = os.path.join(full_images_path, filename)         image = cv2.imread(image_path)         image = resize_image(image)         original_image = image.copy()         annotations = []          cv2.namedWindow(window_name)         cv2.setMouseCallback(window_name, draw_rectangle, param={'original_image': original_image})          while True:             image_with_annotations = original_image.copy()             for annotation in annotations:                 cls, x1, y1, x2, y2 = annotation                 cv2.rectangle(image_with_annotations, (x1, y1), (x2, y2), colors[cls], 2)             cv2.imshow(window_name, image_with_annotations)             key = cv2.waitKey(1) &amp; 0xFF              if key == ord(' '):  # \u041d\u0430\u0436\u0430\u0442\u0438\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u0430 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f                 # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f                 output_image_path = os.path.join(train_images_path, filename)                 cv2.imwrite(output_image_path, original_image)                 print(f\"Saved image to {output_image_path}\")                  # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445                 label_filename = os.path.splitext(filename)[0] + '.txt'                 label_file_path = os.path.join(train_labels_path, label_filename)                 with open(label_file_path, 'w') as f:                     for annotation in annotations:                         cls, x1, y1, x2, y2 = annotation                         x_center = (x1 + x2) \/ 2 \/ 640                         y_center = (y1 + y2) \/ 2 \/ 640                         width = abs(x2 - x1) \/ 640                         height = abs(y2 - y1) \/ 640                         f.write(f\"{cls} {x_center} {y_center} {width} {height}\\n\")                 print(f\"Saved labels to {label_file_path}\")                  # \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 data.yaml                 update_data_yaml()                  # \u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f                 ready_image_path = os.path.join(ready_images_path, filename)                 shutil.move(image_path, ready_image_path)                 print(f\"Moved image to {ready_image_path}\")                 break             elif key == 27:  # \u041d\u0430\u0436\u0430\u0442\u0438\u0435 Esc \u0434\u043b\u044f \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f                 print(\"Skipped image\")                 break             elif key in [ord(str(i)) for i in range(10)]:  # \u0412\u044b\u0431\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430                 current_class = int(chr(key))                 print(f\"Selected class: {current_class}\")          cv2.destroyAllWindows()  <\/code><\/pre>\n<p>\u041a\u0440\u0430\u0442\u043a\u0430\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f:<br \/>\u2014 \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0435 \u043e\u0434\u0438\u043d \u0438\u0437 4 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0446\u0438\u0444\u0440 0,1,2,3 \u043d\u0430 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u0435<br \/>\u2014 \u0420\u0438\u0441\u0443\u0435\u043c \u0440\u0430\u043c\u043a\u0438 \u0432\u043e\u043a\u0440\u0443\u0433 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<br \/>\u2014 \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0440\u0430\u043c\u043a\u0443 &#8212; \u043f\u0440\u0430\u0432\u0430\u044f \u043a\u043d\u043e\u043f\u043a\u0430 \u043c\u044b\u0448\u0438<br \/>\u2014 \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043a\u0430\u0434\u0440\u0443 &#8212; \u043f\u0440\u043e\u0431\u0435\u043b<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2c6\/2fc\/bf4\/2c62fcbf4fbe4285ee433559344d278a.png\" width=\"862\" height=\"695\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2c6\/2fc\/bf4\/2c62fcbf4fbe4285ee433559344d278a.png\"\/><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u044b \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u0439\u043b\u043e\u0432 \u0438 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435!<\/p>\n<h2>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0414\u043b\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 3 \u0447\u0430\u0441\u0442\u0438:<br \/>\u2014 train (\u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435) &#8212; 70%<br \/>\u2014 test (\u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f) &#8212; 20 %<br \/>\u2014 valid (\u043f\u0440\u043e\u0432\u0435\u0440\u043e\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f) &#8212; 10 %<\/p>\n<p>\u0412\u0440\u0443\u0447\u043d\u0443\u044e \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043a\u0440\u0438\u043f\u0442\u0430:<\/p>\n<pre><code class=\"python\">import os import shutil import random  # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 test_percent = 0.2  # \u041f\u0440\u043e\u0446\u0435\u043d\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f valid_percent = 0.1  # \u041f\u0440\u043e\u0446\u0435\u043d\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438  # \u041f\u0443\u0442\u044c \u043a \u043f\u0430\u043f\u043a\u0435 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 dataset_path = 'dataset' train_images_path = os.path.join(dataset_path, 'train', 'images') train_labels_path = os.path.join(dataset_path, 'train', 'labels') valid_images_path = os.path.join(dataset_path, 'valid', 'images') valid_labels_path = os.path.join(dataset_path, 'valid', 'labels') test_images_path = os.path.join(dataset_path, 'test', 'images') test_labels_path = os.path.join(dataset_path, 'test', 'labels')  os.makedirs(valid_images_path, exist_ok=True) os.makedirs(valid_labels_path, exist_ok=True) os.makedirs(test_images_path, exist_ok=True) os.makedirs(test_labels_path, exist_ok=True)  # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u043a images = [f for f in os.listdir(train_images_path) if f.endswith(('.jpg', '.jpeg', '.png'))] labels = [f for f in os.listdir(train_labels_path) if f.endswith('.txt')]  # \u0423\u0431\u0435\u0434\u0438\u043c\u0441\u044f, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438 \u043c\u0435\u0442\u043e\u043a \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 images.sort() labels.sort()  # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438 \u043c\u0435\u0442\u043e\u043a if len(images) != len(labels):     print(\"\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438 \u043c\u0435\u0442\u043e\u043a \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442.\")     exit()  # \u041f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 data = list(zip(images, labels)) random.shuffle(data) images, labels = zip(*data)  # \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 num_images = len(images) num_test = int(num_images * test_percent) num_valid = int(num_images * valid_percent) num_train = num_images - num_test - num_valid  # \u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043f\u0430\u043f\u043a\u0438 def move_files(file_list, source_image_dir, source_label_dir, dest_image_dir, dest_label_dir):     for file in file_list:         image_path = os.path.join(source_image_dir, file)         label_path = os.path.join(source_label_dir, os.path.splitext(file)[0] + '.txt')         shutil.move(image_path, os.path.join(dest_image_dir, file))         shutil.move(label_path, os.path.join(dest_label_dir, os.path.splitext(file)[0] + '.txt'))  # \u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 move_files(images[:num_test], train_images_path, train_labels_path, test_images_path, test_labels_path)  # \u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 move_files(images[num_test:num_test + num_valid], train_images_path, train_labels_path, valid_images_path, valid_labels_path)  # \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0432 \u043f\u0430\u043f\u043a\u0435 train  print(f\"\u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u043e {num_test} \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0432 \u043f\u0430\u043f\u043a\u0443 test.\") print(f\"\u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u043e {num_valid} \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0432 \u043f\u0430\u043f\u043a\u0443 valid.\") print(f\"\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c {num_train} \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0432 \u043f\u0430\u043f\u043a\u0435 train.\") <\/code><\/pre>\n<p>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u0430\u0436\u043d\u044b\u0439 \u044d\u0442\u0430\u043f &#8212; \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 data.yaml, \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u0438 \u043f\u0430\u043f\u043a\u0430\u0445, \u0444\u0430\u0439\u043b\u0430\u0445 \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0444\u0430\u0439\u043b\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>train: dataset\/train\/images  val: dataset\/valid\/images  test: dataset\/test\/images  nc: 4  names: [class0, class1, class2, class3]<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e, \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>dataset\/ \u251c\u2500\u2500 train\/ \u2502   \u251c\u2500\u2500 images\/ \u2502   \u2502   \u251c\u2500\u2500 img1.jpg \u2502   \u2502   \u251c\u2500\u2500 img2.jpg \u2502   \u2502   \u251c\u2500\u2500 ... \u2502   \u251c\u2500\u2500 labels\/ \u2502   \u2502   \u251c\u2500\u2500 img1.txt \u2502   \u2502   \u251c\u2500\u2500 img2.txt \u2502   \u2502   \u251c\u2500\u2500 ... \u251c\u2500\u2500 test\/ \u2502   \u251c\u2500\u2500 images\/ \u2502   \u2502   \u251c\u2500\u2500 img3.jpg \u2502   \u2502   \u251c\u2500\u2500 img4.jpg \u2502   \u2502   \u251c\u2500\u2500 ... \u2502   \u251c\u2500\u2500 labels\/ \u2502   \u2502   \u251c\u2500\u2500 img3.txt \u2502   \u2502   \u251c\u2500\u2500 img4.txt \u2502   \u2502   \u251c\u2500\u2500 ... \u251c\u2500\u2500 valid\/ \u2502   \u251c\u2500\u2500 images\/ \u2502   \u2502   \u251c\u2500\u2500 img5.jpg \u2502   \u2502   \u251c\u2500\u2500 img6.jpg \u2502   \u2502   \u251c\u2500\u2500 ... \u2502   \u251c\u2500\u2500 labels\/ \u2502   \u2502   \u251c\u2500\u2500 img5.txt \u2502   \u2502   \u251c\u2500\u2500 img6.txt \u2502   \u2502   \u251c\u2500\u2500 ... \u251c\u2500\u2500 output_images\/ \u2502   \u251c\u2500\u2500 img1.jpg \u2502   \u251c\u2500\u2500 img2.jpg \u2502   \u251c\u2500\u2500 ... \u251c\u2500\u2500 data.yaml \u251c\u2500\u2500 Dataset_video2images.py \u2514\u2500\u2500 Dataset_HSV_Markup.py<\/code><\/pre>\n<h2>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412\u0441\u0435\u0433\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0438 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435:<br \/><code>epochs = 500<\/code> \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043f\u043e\u0445 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0438\u043d\u0434\u0438\u0432\u0438\u0434\u0443\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u0440\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 YOLO \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0435\u0441\u043b\u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043f\u043e\u0445.<\/p>\n<p>batch = 64 &#8212; \u0440\u0430\u0437\u043c\u0435\u0440 &#171;\u043f\u0430\u043a\u0435\u0442\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439&#187; \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u0437\u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c. \u0418\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>imgsz = 640 &#8212; \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 <\/p>\n<pre><code class=\"python\">import os from ultralytics import YOLO  current_dir = os.path.dirname(os.path.abspath(__file__))  data_path = os.path.join(current_dir, 'data.yaml') model = YOLO(os.path.join(current_dir, 'yolov8n.pt'))   epochs = 500 batch = 64 imgsz = 640 if __name__ == '__main__':     results = model.train(data=data_path,                       epochs=epochs,                        batch=batch,                        imgsz=imgsz,                        name='red',                       device='cuda')  <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u0436\u0434\u0435\u043c!<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/be0\/bfd\/579\/be0bfd579d10377703301982d069fa99.png\" width=\"1183\" height=\"318\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/be0\/bfd\/579\/be0bfd579d10377703301982d069fa99.png\"\/><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 (\u0432\u0440\u0435\u043c\u044f \u0441\u0438\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432) \u0432 \u043f\u0430\u043f\u043a\u0435 runs \u0431\u0443\u0434\u0443\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u043a\u0438 \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0444\u0430\u0439\u043b\u044b \u043c\u043e\u0434\u0435\u043b\u0438 <code>best.pt<\/code> \u0438 <code>last.pt<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/45a\/c43\/1f0\/45ac431f0f550da3a69211b6b79aa532.png\" width=\"1427\" height=\"565\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/45a\/c43\/1f0\/45ac431f0f550da3a69211b6b79aa532.png\"\/><\/figure>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 <code>best.pt<\/code> \u0438 \u0423\u0420\u0410, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/821971\/\"> https:\/\/habr.com\/ru\/articles\/821971\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044f\u043c\u0438 &#8212; \u044d\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e, \u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0434\u043b\u044f \u0432\u0430\u0441!<\/p>\n<p>\u0418\u0442\u0430\u043a, YOLO\u00a0<\/strong>(You Only Look Once) \u2014 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0430\u044f \u0441\u0435\u0442\u044c, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0448\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<ul>\n<li>\n<p>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f &#8212; \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f &#8212; \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443<\/p>\n<\/li>\n<li>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0438\u0441\u043a \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0442\u0435\u043b\u0430 &#8212; \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0437\u044b \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0435\u043a\u0438\u043d\u0433 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 &#8212; \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u0442\u043e\u0440\u0438\u044e \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e: <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 \u0434\u043b\u044f \u0434\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0438 \u043d\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<\/li>\n<\/ul>\n<h2>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043a \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 YOLO<\/h2>\n<p>\u041e\u0442\u043b\u0438\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e YOLO \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0445\u043e\u0434, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0447\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0438\u043c\u0435\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0432\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430 Python.<br \/>\u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 YOLO \u043d\u0430 \u0432\u0430\u0448 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043a \u043a\u043e\u043d\u0441\u043e\u043b\u0438: <\/p>\n<p><code>pip install ultralytics<\/p>\n<p><\/code>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u043c\u043e\u0434\u0443\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a \u0440\u0430\u0431\u043e\u0442\u0435.<\/p>\n<h2>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 \u0444\u043e\u0442\u043e<\/h2>\n<p>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u043d\u0435\u0439:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 YOLO \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<pre><code class=\"python\">from ultralytics import YOLO import cv2 import numpy as np import os  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO('yolov8n.pt')  # \u0421\u043f\u0438\u0441\u043e\u043a \u0446\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 colors = [     (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255),     (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0),     (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128), (72, 61, 139),     (47, 79, 79), (47, 79, 47), (0, 206, 209), (148, 0, 211), (255, 20, 147) ]  # \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f def process_image(image_path):     # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     image = cv2.imread(image_path)     results = model(image)[0]          # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     image = results.orig_img     classes_names = results.names     classes = results.boxes.cls.cpu().numpy()     boxes = results.boxes.xyxy.cpu().numpy().astype(np.int32)      # \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u043c     grouped_objects = {}      # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u043c\u043e\u043a \u0438 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     for class_id, box in zip(classes, boxes):         class_name = classes_names[int(class_id)]         color = colors[int(class_id) % len(colors)]  # \u0412\u044b\u0431\u043e\u0440 \u0446\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430         if class_name not in grouped_objects:             grouped_objects[class_name] = []         grouped_objects[class_name].append(box)          # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u043c\u043e\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438         x1, y1, x2, y2 = box         cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)         cv2.putText(image, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)      # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     new_image_path = os.path.splitext(image_path)[0] + '_yolo' + os.path.splitext(image_path)[1]     cv2.imwrite(new_image_path, image)      # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b     text_file_path = os.path.splitext(image_path)[0] + '_data.txt'     with open(text_file_path, 'w') as f:         for class_name, details in grouped_objects.items():             f.write(f\"{class_name}:\\n\")             for detail in details:                 f.write(f\"Coordinates: ({detail[0]}, {detail[1]}, {detail[2]}, {detail[3]})\\n\")      print(f\"Processed {image_path}:\")     print(f\"Saved bounding-box image to {new_image_path}\")     print(f\"Saved data to {text_file_path}\")   process_image('test.png') <\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043a\u043e\u0434\u0430 \u2014 \u0432\u044b\u0431\u043e\u0440 \u043c\u043e\u0434\u0435\u043b\u0438 (\u043e\u043d\u0430 \u0441\u043a\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u0430). \u041a\u0440\u043e\u043c\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 <code>yolov8n.pt,<\/code> \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e:<br \/><code>yolov8n.pt<\/code>\u00a0<code>yolov8s.pt<\/code>\u00a0<code>yolov8m.pt<\/code>\u00a0<code>yolov8l.pt<\/code>\u00a0<code>yolov8x.pt<\/code>  <\/p>\n<p>\u041a\u0430\u0436\u0434\u0430\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0431\u043e\u043b\u044c\u0448\u0435, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u043d\u043e \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0443\u0447\u0448\u0435. <\/p>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0445 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u0421\u0442\u0435\u043f\u0430\u043d\u0430 \u0416\u0434\u0430\u043d\u043e\u0432\u0430: <br \/><a href=\"https:\/\/habr.com\/ru\/articles\/822917\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/822917\/<\/a><\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <code>boxes<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u0445, \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0445 \u043a \u043a\u043b\u0430\u0441\u0441\u0443 (person, car, bus, traffic light \u0438 \u0442.\u0434.)<\/p>\n<p>\u0418\u0442\u0430\u043a, \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0432\u0438\u0434\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a\u0440\u043e\u043c\u0435 \u044d\u0442\u043e\u0433\u043e, \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0442\u0430\u043a\u043e\u0433\u043e \u0432\u0438\u0434\u0430:<\/p>\n<pre><code>car: Coordinates: (842, 681, 1180, 894) Coordinates: (254, 849, 524, 971) Coordinates: (49, 620, 425, 857) stop sign: Coordinates: (407, 560, 470, 626) Coordinates: (267, 494, 341, 557) traffic light: Coordinates: (334, 157, 451, 426) Coordinates: (938, 97, 1031, 312) Coordinates: (86, 481, 130, 602) person: Coordinates: (578, 711, 710, 990) Coordinates: (715, 723, 750, 798) Coordinates: (715, 852, 864, 976) Coordinates: (241, 897, 385, 1012) truck: Coordinates: (52, 620, 425, 859)<\/code><\/pre>\n<h2>\u0414\u0435\u0442\u0435\u043a\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435\u043e-\u043f\u043e\u0442\u043e\u043a\u0435<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0441 \u043e\u0434\u043d\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u0438\u0434\u0435\u043e. \u0412 \u0446\u0435\u043b\u043e\u043c, \u043e\u043d\u043e \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0442.\u043a. \u0432\u0438\u0434\u0435\u043e \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439! \u041e\u0442\u043b\u0438\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u0438\u0434\u0435\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u0434\u0435\u043a\u0438 \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0430\u0442\u0430 MP4.<\/p>\n<pre><code class=\"python\">from ultralytics import YOLO import cv2 import numpy as np  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO('yolov8n.pt')  # \u0421\u043f\u0438\u0441\u043e\u043a \u0446\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 colors = [     (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255),     (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0),     (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128), (72, 61, 139),     (47, 79, 79), (47, 79, 47), (0, 206, 209), (148, 0, 211), (255, 20, 147) ]  # \u041e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0435\u043e\u0444\u0430\u0439\u043b\u0430 input_video_path = 'input.mp4' capture = cv2.VideoCapture(input_video_path)  # \u0427\u0442\u0435\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432\u0438\u0434\u0435\u043e fps = int(capture.get(cv2.CAP_PROP_FPS)) width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))  # \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 output_video_path = 'detect.mp4' fourcc = cv2.VideoWriter_fourcc(*'mp4v') writer = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))  while True:     # \u0417\u0430\u0445\u0432\u0430\u0442 \u043a\u0430\u0434\u0440\u0430     ret, frame = capture.read()     if not ret:         break      # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043a\u0430\u0434\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0435\u043b\u0438 YOLO     results = model(frame)[0]      # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u0445     classes_names = results.names     classes = results.boxes.cls.cpu().numpy()     boxes = results.boxes.xyxy.cpu().numpy().astype(np.int32)     # \u0420\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u043c\u043e\u043a \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0435\u0439 \u043d\u0430 \u043a\u0430\u0434\u0440\u0435     for class_id, box, conf in zip(classes, boxes, results.boxes.conf):         if conf>0.5:             class_name = classes_names[int(class_id)]             color = colors[int(class_id) % len(colors)]             x1, y1, x2, y2 = box             cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)             cv2.putText(frame, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)      # \u0417\u0430\u043f\u0438\u0441\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u0430\u0434\u0440\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b     writer.write(frame)  # \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u043e\u043a\u043e\u043d capture.release() writer.release() <\/code><\/pre>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/666d1f27ee339fd48b1f9509\" data-style=\"\" id=\"666d1f27ee339fd48b1f9509\" width=\"\"><\/div>\n<p>\u0414\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0432\u0435\u0431 \u043a\u0430\u043c\u0435\u0440\u044b &#8212; \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0435<br \/><code>capture = cv2.VideoCapture(input_video_path)<br \/><\/code>\u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u043c\u0435\u0440\u044b \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<code><br \/>capture = cv2.VideoCapture(0)<br \/><\/code>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430: <br \/><a href=\"https:\/\/github.com\/stepanburmistrov\/YoloV8\/blob\/main\/Detection_Camera.py\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/stepanburmistrov\/YoloV8\/blob\/main\/Detection_Camera.py<\/a><\/p>\n<h2>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f &#8212; \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u043b\u0430\u0441\u0441\u044b. \u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u043d\u0430 \u0432\u043e\u043a\u0440\u0443\u0433 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0441 \u0444\u043e\u0442\u043e\/\u0432\u0438\u0434\u0435\u043e.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044e (\u043a\u0430\u0434\u0440 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0432\u0438\u0434\u0435\u043e) \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c YoloV8 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0434\u0435\u0442\u0435\u043a\u0446\u0438\u0435\u0439 \u0432\u044b\u0431\u043e\u0440 \u0435\u0441\u0442\u044c:<br \/><code>yolov8n-seg.pt<\/code>\u00a0<code>yolov8s-seg.pt<\/code>\u00a0<code>yolov8m-seg.pt<\/code>\u00a0<code>yolov8l-seg.pt<\/code>\u00a0<code>yolov8x-seg.pt<\/code>  <\/p>\n<figure class=\"full-width\"><\/figure>\n<pre><code class=\"python\">import cv2 import numpy as np from ultralytics import YOLO import os  # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 YOLOv8 model = YOLO('yolov8x-seg.pt')  colors = [     (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255),     (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0),     (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128), (72, 61, 139),     (47, 79, 79), (47, 79, 47), (0, 206, 209), (148, 0, 211), (255, 20, 147) ]  def process_image(image_path):     # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u043f\u0430\u043f\u043a\u0438 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     if not os.path.exists('results'):         os.makedirs('results')          # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     image = cv2.imread(image_path)     image_orig = image.copy()     h_or, w_or = image.shape[:2]     image = cv2.resize(image, (640, 640))     results = model(image)[0]          classes_names = results.names     classes = results.boxes.cls.cpu().numpy()     masks = results.masks.data.cpu().numpy()      # \u041d\u0430\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043e\u043a \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435     for i, mask in enumerate(masks):         color = colors[int(classes[i]) % len(colors)]                  # \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043c\u0430\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0446\u0432\u0435\u0442\u043d\u043e\u0439 \u043c\u0430\u0441\u043a\u0438         mask_resized = cv2.resize(mask, (w_or, h_or))                  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0446\u0432\u0435\u0442\u043d\u043e\u0439 \u043c\u0430\u0441\u043a\u0438         color_mask = np.zeros((h_or, w_or, 3), dtype=np.uint8)         color_mask[mask_resized > 0] = color          # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b         mask_filename = os.path.join('results', f\"{classes_names[classes[i]]}_{i}.png\")         cv2.imwrite(mask_filename, color_mask)          # \u041d\u0430\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435         image_orig = cv2.addWeighted(image_orig, 1.0, color_mask, 0.5, 0)       # \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f     new_image_path = os.path.join('results', os.path.splitext(os.path.basename(image_path))[0] + '_segmented' + os.path.splitext(image_path)[1])     cv2.imwrite(new_image_path, image_orig)     print(f\"Segmented image saved to {new_image_path}\")  process_image('segmentation_test.png') <\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e YoloV8<\/figcaption><\/div>\n<\/figure>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0432 \u043f\u0430\u043f\u043a\u0443 results \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043c\u0430\u0441\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-379677","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/379677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=379677"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/379677\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=379677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=379677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=379677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}