# yolo-prg1 YOLO物件辨識 #!pip install ultralytics import cv2 from ultralytics import YOLO from google.colab.patches import cv2_imshow # Import the cv2_imshow function from google.colab.patches # 加载YOLOv8模型 model = YOLO('yolov8n.pt') # 或使用其他预训练模型 # 读取图片 image_path = '/content/table.png' image = cv2.imread(image_path) # 运行YOLOv8检测 results = model(image) # 在图片上绘制检测结果 for result in results: boxes = result.boxes.xyxy.cpu().numpy().astype(int) for box in boxes: cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) # 显示结果 cv2_imshow(image) # Use cv2_imshow instead of cv2.imshow cv2.waitKey(0) cv2.destroyAllWindows() # 保存结果(可选) cv2.imwrite('output_image.jpg', image) print('检测完成,结果已保存。')