predict_12.16.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # from PIL import Image
  2. # from ultralytics import YOLO
  3. # import time
  4. # # 加载预训练的YOLOv8n模型
  5. # model = YOLO('/home/nvidia/newdisk/hkpc/ultralytics-main/best.pt')
  6. # time.sleep(5)
  7. # # 在'bus.jpg'上运行推理
  8. # start_time = time.time()
  9. # model.predict('data/1.jpg', save=False, imgsz=320, conf=0.5)
  10. # # results = model('data/1.jpg') # 结果列表
  11. # # # 展示结果
  12. # # for r in results:
  13. # # print(r.boxes.data)
  14. # # print(r.names)
  15. # #print(r.probs) # 打印包含检测到的类别概率的Probs对象
  16. # # im_array = r.plot() # 绘制包含预测结果的BGR numpy数组
  17. # # im = Image.fromarray(im_array[..., ::-1]) # RGB PIL图像
  18. # # im.show() # 显示图像
  19. # # im.save('results.jpg') # 保存图像
  20. # # 记录结束时间
  21. # end_time = time.time()
  22. # # 计算代码运行时间
  23. # elapsed_time = end_time - start_time
  24. # print(f"代码运行时间: {elapsed_time} 秒")
  25. import cv2
  26. from ultralytics import YOLO
  27. import time
  28. # Load the YOLOv8 model
  29. model = YOLO('/home/nvidia/newdisk/hkpc/ultralytics-main/best.pt')
  30. # Loop through the video frames
  31. while 1:
  32. start_time = time.time()
  33. # Run YOLOv8 inference on the frame
  34. results = model('data/1.jpg')
  35. # Visualize the results on the frame
  36. # annotated_frame = results[0].plot()
  37. # # Display the annotated frame
  38. # cv2.imshow("YOLOv8 Inference", annotated_frame)
  39. # # Break the loop if 'q' is pressed
  40. # if cv2.waitKey(1) & 0xFF == ord("q"):
  41. # break
  42. end_time = time.time()
  43. elapsed_time = end_time - start_time
  44. print(f"代码运行时间: {elapsed_time} 秒")
  45. time.sleep(0.5)
  46. # cv2.destroyAllWindows()