实时目标检测使用Yolo模型不起作用。

huangapple go评论136阅读模式
英文:

Real time object detection with Yolo model not working

问题

我已经训练了一个自定义的YOLO模型,用于检测棋盘上的正方形槽,对图像的准确率超过95%。

但是一旦我切换到视频检测,似乎连一个物体都无法检测到。

我使用以下代码进行实时目标检测:

  1. cap = cv2.VideoCapture('../video/1st/output.mp4')
  2. while cap.isOpened():
  3. ret, frame = cap.read()
  4. results = model(frame)
  5. final_img = np.squeeze(results.render())
  6. cv2.imshow("YOLO", final_img)
  7. if cv2.waitKey(10) & 0XFF == ord("q"):
  8. break
  9. cap.release()
  10. cv2.destroyAllWindows()

我使用以下代码加载模型:

  1. model = torch.hub.load("ultralytics/yolov5", "custom", path="yolov5/runs/train/exp36/weights/best.pt", force_reload=True)
  2. model.conf = 0.6

我甚至尝试将可用的视频拆分成JPEG图像,然后在单独的图像上运行模型,保存输出,然后将输出图像合并成一个新的视频文件。

这个方法运行得很完美,所以模型确实能够检测到一些物体。

但一旦切换到视频,似乎什么都检测不到。

英文:

I have trained a custom yolo model to detect square slots on a board, and is working with more than 95 % accuracy on images.

But as soon as I switch to video detection it seems to not detect even a single thing

I am using the following code to run real time object detection

  1. cap = cv2.VideoCapture('../video/1st/output.mp4')
  2. while cap.isOpened():
  3. ret, frame = cap.read()
  4. results = model(frame)
  5. final_img = np.squeeze(results.render())
  6. cv2.imshow("YOLO", final_img)
  7. if cv2.waitKey(10) & 0XFF == ord("q"):
  8. break
  9. cap.release()
  10. cv2.destroyAllWindows()

I load the model using this code

  1. model = torch.hub.load("ultralytics/yolov5", "custom", path="yolov5/runs/train/exp36/weights/best.pt",force_reload=True)
  2. model.conf = 0.6

I have even tried splitting the available video into jpegs and running the model on individual images, saving the output and then merging the output images into a new video file.

that works perfectly, so the model is detecting something.

but as soon as I switch to video it seems to go back to nothing.

答案1

得分: 0

是的,由于results.renders()返回的对象类型为none,您无法看到任何内容。
您可以像这样更改代码脚本

  1. cap = cv2.VideoCapture('../video/1st/output.mp4')
  2. while cap.isOpened():
  3. ret, frame = cap.read()
  4. results = model(frame)
  5. bboxes = results.xyxy[0].cpu().tolist()
  6. for bbox in bboxes:
  7. conf = f'{bbox[4]:.4f}' # 预测的置信度
  8. bbox = list(map(lambda x: int(x), bbox)) # 将浮点数转换为整数
  9. class_id = bbox[5] # 类别ID
  10. bbox = bbox[:4]
  11. cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(255, 255, 255), thickness=3)
  12. cv2.imshow("YOLO", frame)
  13. if cv2.waitKey(10) & 0XFF == ord("q"):
  14. break
  15. cap.release()
  16. cv2.destroyAllWindows()

并将帧写入视频。完整的代码应该如下所示

  1. input_video_path = #输入您的视频路径
  2. cap = cv2.VideoCapture(input_video_path)
  3. fps = int(cap.get(cv2.CAP_PROP_FPS))
  4. frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  5. frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  6. output_video_path = 'output_video.mp4' # 输出视频路径
  7. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  8. out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
  9. model = #在此加载您的模型
  10. while True:
  11. ret, frame = cap.read()
  12. if not ret:
  13. break
  14. results = model(frame)
  15. bboxes = results.xyxy[0].cpu().tolist()
  16. for bbox in bboxes:
  17. conf = f'{bbox[4]:.4f}' # 预测的置信度
  18. bbox = list(map(lambda x: int(x), bbox)) # 将浮点数转换为整数
  19. class_id = bbox[5] # 类别ID
  20. bbox = bbox[:4]
  21. cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(255, 255, 255), thickness=3)
  22. out.write(frame)
  23. if cv2.waitKey(10) & 0XFF == ord("q"):
  24. break
  25. cap.release()
  26. out.release()
  27. # 关闭所有OpenCV窗口
  28. cv2.destroyAllWindows()

参考文献:[https://docs.ultralytics.com/][1],[https://docs.ultralytics.com/yolov5/][2],[https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading/][3]

英文:

Yes, You cannot see anything due to the none type of object return in the results.renders()/
You can change the code script like this

  1. cap = cv2.VideoCapture('../video/1st/output.mp4')
  2. while cap.isOpened():
  3. ret, frame = cap.read()
  4. results = model(frame)
  5. bboxes = results.xyxy[0].cpu().tolist()
  6. for bbox in bboxes:
  7. conf = f'{bbox[4]:.4f}' #Confidance of that prediction
  8. bbox = list(map(lambda x: int(x), bbox)) #To convert float to integer
  9. class_id = bbox[5] #Class_id
  10. bbox =bbox[:4]
  11. cv2.rectangle(frame,(bbox[0],bbox[1]),(bbox[2],bbox[3]),color=(255,255,255),thickness=3)
  12. cv2.imshow("YOLO", frame)
  13. if cv2.waitKey(10) & 0XFF == ord("q"):
  14. break
  15. cap.release()
  16. cv2.destroyAllWindows()

and write the frames in the video
the full code should look like this

  1. input_video_path = #Enter your video path
  2. cap = cv2.VideoCapture(input_video_path)
  3. fps = int(cap.get(cv2.CAP_PROP_FPS))
  4. frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  5. frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  6. output_video_path = 'output_video.mp4' # Output video path
  7. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  8. out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
  9. model = #Here load your model
  10. while True:
  11. ret, frame = cap.read()
  12. if not ret:
  13. break
  14. results = model(frame)
  15. bboxes = results.xyxy[0].cpu().tolist()
  16. for bbox in bboxes:
  17. conf = f'{bbox[4]:.4f}' #Confidance of that prediction
  18. bbox = list(map(lambda x: int(x), bbox)) #To convert float to integer
  19. class_id = bbox[5] #Class_id
  20. bbox =bbox[:4]
  21. cv2.rectangle(frame,(bbox[0],bbox[1]),(bbox[2],bbox[3]),color=(255,255,255),thickness=3)
  22. out.write(frame)
  23. #cv2_imshow(frame)
  24. if cv2.waitKey(10) & 0XFF == ord("q"):
  25. break
  26. cap.release()
  27. out.release()
  28. # Close all OpenCV windows
  29. cv2.destroyAllWindows()

References:-
[https://docs.ultralytics.com/][1] <br>
[https://docs.ultralytics.com/yolov5/][2] <br>
[https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading/][3]<br>

huangapple
  • 本文由 发表于 2023年8月9日 14:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865316-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定