์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

ํ„ฐ๋ฏธ๋„์—์„œ ์ถ”๋ก  ๊ฒฐ๊ณผ ๋ณด๊ธฐ

ํ„ฐ๋ฏธ๋„์˜ ์‹์…€ ์ด๋ฏธ์ง€ ์˜ˆ์‹œ

libsixel ์›น์‚ฌ์ดํŠธ์˜ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค.

๋™๊ธฐ ๋ถ€์—ฌ

์›๊ฒฉ ๋จธ์‹ ์— ์—ฐ๊ฒฐํ•˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋ฏธ์ง€ ๊ฒฐ๊ณผ๋ฅผ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์—†๊ฑฐ๋‚˜ GUI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ปฌ ์žฅ์น˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. VSCode ํ†ตํ•ฉ ํ„ฐ๋ฏธ๋„์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฏธ์ง€๋ฅผ ์ง์ ‘ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด๋ฅผ ๋‹ค์Œ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์งง์€ ๋ฐ๋ชจ์ž…๋‹ˆ๋‹ค. ultralytics ์™€ ํ•จ๊ป˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ.

๊ฒฝ๊ณ 

Linux ๋ฐ MacOS์™€๋งŒ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค. VSCode ํ™•์ธ ์ €์žฅ์†Œํ™•์ธ ์ด์Šˆ ์ƒํƒœ๋˜๋Š” ๋ฌธ์„œ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ„ฐ๋ฏธ๋„์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๊ธฐ ์œ„ํ•œ Windows ์ง€์›์— ๋Œ€ํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. sixel.

ํ†ตํ•ฉ ํ„ฐ๋ฏธ๋„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๊ธฐ ์œ„ํ•œ VSCode ํ˜ธํ™˜ ํ”„๋กœํ† ์ฝœ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. sixel ๊ทธ๋ฆฌ๊ณ  iTerm. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” sixel ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์„ธ์Šค

  1. ๋จผ์ € ์„ค์ •์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. terminal.integrated.enableImages ๊ทธ๋ฆฌ๊ณ  terminal.integrated.gpuAcceleration ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

    "terminal.integrated.gpuAcceleration": "auto" # "auto" is default, can also use "on"
    "terminal.integrated.enableImages": false
    

VSCode ํ™œ์„ฑํ™” ํ„ฐ๋ฏธ๋„ ์ด๋ฏธ์ง€ ์„ค์ •

  1. ์„ค์น˜ python-sixel ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. ์ด๊ฒƒ์€ ํฌํฌ ์˜ PySixel ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋” ์ด์ƒ ์œ ์ง€ ๊ด€๋ฆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

    pip install sixel
    
  2. ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ

    import io
    
    import cv2 as cv
    
    from ultralytics import YOLO
    from sixel import SixelWriter
    
  3. ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์ถ”๋ก ์„ ์‹คํ–‰ํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ํ”Œ๋กœํŒ…ํ•˜๊ณ  ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋ชจ๋“œ ํŽ˜์ด์ง€์—์„œ ์ถ”๋ก  ์ธ์ˆ˜ ๋ฐ ๊ฒฐ๊ณผ ์ž‘์—…์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด์„ธ์š”.

    from ultralytics import YOLO
    
    # Load a model
    model = YOLO("yolov8n.pt")
    
    # Run inference on an image
    results = model.predict(source="ultralytics/assets/bus.jpg")
    
    # Plot inference results
    plot = results[0].plot() #(1)!
    
    1. ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ํ”Œ๋กฏ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.
  4. ์ด์ œ OpenCV๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ numpy.ndarray ์— bytes ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ io.BytesIO ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "ํŒŒ์ผ๊ณผ ๊ฐ™์€" ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

    # Results image as bytes
    im_bytes = cv.imencode(
        ".png", #(1)!
        plot,
        )[1].tobytes() #(2)!
    
    # Image bytes as a file-like object
    mem_file = io.BytesIO(im_bytes)
    
    1. ๋‹ค๋ฅธ ์ด๋ฏธ์ง€ ํ™•์žฅ์ž๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    2. ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐœ์ฒด๋งŒ 1 ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  5. ๋งŒ๋“ค๊ธฐ SixelWriter ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์Œ .draw() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ„ฐ๋ฏธ๋„์— ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.

    # Create sixel writer object
    w = SixelWriter()
    
    # Draw the sixel image in the terminal
    w.draw(mem_file)
    

์ถ”๋ก  ๊ฒฐ๊ณผ ์˜ˆ์‹œ

ํ„ฐ๋ฏธ๋„์—์„œ ์ด๋ฏธ์ง€ ๋ณด๊ธฐ

์œ„ํ—˜

์ด ์˜ˆ์‹œ๋ฅผ ๋™์˜์ƒ ๋˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ GIF ํ”„๋ ˆ์ž„๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ž์‹ ์˜ ์ฑ…์ž„ ํ•˜์— ์‹œ๋„ํ•˜์„ธ์š”.

์ „์ฒด ์ฝ”๋“œ ์˜ˆ์ œ

import io

import cv2 as cv

from ultralytics import YOLO
from sixel import SixelWriter

# Load a model
model = YOLO("yolov8n.pt")

# Run inference on an image
results = model.predict(source="ultralytics/assets/bus.jpg")

# Plot inference results
plot = results[0].plot() #(3)!

# Results image as bytes
im_bytes = cv.imencode(
    ".png", #(1)!
    plot,
    )[1].tobytes() #(2)!

mem_file = io.BytesIO(im_bytes)
w = SixelWriter()
w.draw(mem_file)
  1. ๋‹ค๋ฅธ ์ด๋ฏธ์ง€ ํ™•์žฅ์ž๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐœ์ฒด๋งŒ 1 ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  3. ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ํ”Œ๋กฏ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

ํŒ

๋‹ค์Œ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. clear ์„ ๋ˆŒ๋Ÿฌ ํ„ฐ๋ฏธ๋„์—์„œ ์ด๋ฏธ์ง€ ๋ณด๊ธฐ๋ฅผ "์ง€์šฐ์„ธ์š”".



์ƒ์„ฑ 2024-03-09, ์—…๋ฐ์ดํŠธ 2024-03-09
์ž‘์„ฑ์ž: Burhan-Q (1)

๋Œ“๊ธ€