我正在尝试利用YOLOv8在他们的github上看到的软件,但我不知道如何将其植入jupyter实验室。
我正在尝试查看应该在 python 3.x 中实现哪些代码行才能使其正常工作。这需要某种 pip 安装吗?我怎样才能让我的代码以这种方式连接到互联网?
要在 Jupyter Lab 中使用 YOLOv8,您需要安装必要的库,包括 YOLOv8 包,然后运行代码来执行对象检测。
安装YOLOv8和依赖项:
!pip install ultralytics
!pip install opencv-python-headless
加载YOLOv8模型并进行检测:
import cv2
from ultralytics import YOLO
# Load YOLOv8 model
model = YOLO('yolov8n.pt') # You can choose a different model size such as yolov8s.pt, yolov8m.pt, etc.
# Load an image
img_path = 'path/to/your/image.jpg'
img = cv2.imread(img_path)
# Run inference
results = model(img)
# Display results
results.show()
下载图像进行测试:
import requests
url = 'https://raw.githubusercontent.com/devicons/devicon/master/icons/typescript/typescript-original.svg'
img_path = 'downloaded_image.svg'
response = requests.get(url)
with open(img_path, 'wb') as file:
file.write(response.content)
# Now you can use this image with YOLOv8
img = cv2.imread(img_path)
results = model(img)
results.show()
在 Jupyter Lab 中显示结果:
import matplotlib.pyplot as plt
# Convert the image from BGR to RGB
img_rgb = cv2.cvtColor(results.ims[0], cv2.COLOR_BGR2RGB)
# Display the image with detections
plt.imshow(img_rgb)
plt.axis('off')
plt.show()