Apply new Ruff actions to Python codeblocks (#13783)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2024-06-18 20:51:37 +02:00 committed by GitHub
parent c0305f4e15
commit 6bddeda34b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 95 additions and 99 deletions

View file

@ -18,7 +18,7 @@ When connecting to a remote machine, normally visualizing image results is not p
!!! warning
Only compatible with Linux and MacOS. Check the VSCode [repository](https://github.com/microsoft/vscode), check [Issue status](https://github.com/microsoft/vscode/issues/198622), or [documentation](https://code.visualstudio.com/docs) for updates about Windows support to view images in terminal with `sixel`.
Only compatible with Linux and MacOS. Check the [VSCode repository](https://github.com/microsoft/vscode), check [Issue status](https://github.com/microsoft/vscode/issues/198622), or [documentation](https://code.visualstudio.com/docs) for updates about Windows support to view images in terminal with `sixel`.
The VSCode compatible protocols for viewing images using the integrated terminal are [`sixel`](https://en.wikipedia.org/wiki/Sixel) and [`iTerm`](https://iterm2.com/documentation-images.html). This guide will demonstrate use of the `sixel` protocol.
@ -31,28 +31,17 @@ The VSCode compatible protocols for viewing images using the integrated terminal
"terminal.integrated.enableImages": false
```
<p align="center">
<img width="800" src="https://github.com/ultralytics/ultralytics/assets/62214284/d158ab1c-893c-4397-a5de-2f9f74f81175" alt="VSCode enable terminal images setting">
</p>
<p align="center">
<img width="800" src="https://github.com/ultralytics/ultralytics/assets/62214284/d158ab1c-893c-4397-a5de-2f9f74f81175" alt="VSCode enable terminal images setting">
</p>
1. Install the `python-sixel` library in your virtual environment. This is a [fork](https://github.com/lubosz/python-sixel?tab=readme-ov-file) of the `PySixel` library, which is no longer maintained.
2. Install the `python-sixel` library in your virtual environment. This is a [fork](https://github.com/lubosz/python-sixel?tab=readme-ov-file) of the `PySixel` library, which is no longer maintained.
```bash
pip install sixel
```
1. Import the relevant libraries
```py
import io
import cv2 as cv
from sixel import SixelWriter
from ultralytics import YOLO
```
1. Load a model and execute inference, then plot the results and store in a variable. See more about inference arguments and working with results on the [predict mode](../modes/predict.md) page.
3. Load a model and execute inference, then plot the results and store in a variable. See more about inference arguments and working with results on the [predict mode](../modes/predict.md) page.
```{ .py .annotate }
from ultralytics import YOLO
@ -69,11 +58,15 @@ The VSCode compatible protocols for viewing images using the integrated terminal
1. See [plot method parameters](../modes/predict.md#plot-method-parameters) to see possible arguments to use.
1. Now, use OpenCV to convert the `numpy.ndarray` to `bytes` data. Then use `io.BytesIO` to make a "file-like" object.
4. Now, use OpenCV to convert the `numpy.ndarray` to `bytes` data. Then use `io.BytesIO` to make a "file-like" object.
```{ .py .annotate }
import io
import cv2
# Results image as bytes
im_bytes = cv.imencode(
im_bytes = cv2.imencode(
".png", # (1)!
plot,
)[1].tobytes() # (2)!
@ -85,9 +78,11 @@ The VSCode compatible protocols for viewing images using the integrated terminal
1. It's possible to use other image extensions as well.
2. Only the object at index `1` that is returned is needed.
1. Create a `SixelWriter` instance, and then use the `.draw()` method to draw the image in the terminal.
5. Create a `SixelWriter` instance, and then use the `.draw()` method to draw the image in the terminal.
```python
from sixel import SixelWriter
```py
# Create sixel writer object
w = SixelWriter()
@ -110,7 +105,7 @@ The VSCode compatible protocols for viewing images using the integrated terminal
```{ .py .annotate }
import io
import cv2 as cv
import cv2
from sixel import SixelWriter
from ultralytics import YOLO
@ -125,7 +120,7 @@ results = model.predict(source="ultralytics/assets/bus.jpg")
plot = results[0].plot() # (3)!
# Results image as bytes
im_bytes = cv.imencode(
im_bytes = cv2.imencode(
".png", # (1)!
plot,
)[1].tobytes() # (2)!