Im using the following code to access the 5 camera streams as source. Is it the recommended way? Can I make any improvement?
import os
import cv2
import threading
from queue import Queue
from ultralytics import YOLO
from datetime import datetime
class VideoCapture:
def __init__(self, url):
self.cap = cv2.VideoCapture(url)
self.q = Queue(maxsize=10) # buffer size
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
def _reader(self):
while True:
success, frame = self.cap.read()
if not success:
break
if not self.q.empty():
self.q.get_nowait()
except Queue.Empty:
self.q.put(frame)
def read(self):
return self.q.get()
model = YOLO('yolov8n.pt')
urls = []
cams = [VideoCapture(url) for url in urls]
while True:
for i, cam in enumerate(cams):
frame = cam.read()
if frame is None:
continue
results = model(frame)
annotated_frame = results[0].plot()
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
output_path = os.path.join("C:\\Users\\rosha\\Downloads\\Compressed\\ultralytics-main\\detectedobjects", f"cam_{i}_output_{timestamp}.jpg")
cv2.imwrite(output_path, annotated_frame)
cv2.imshow(f"YOLOv8 Inference - Camera {i+1}", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
finally:
for cam in cams:
cam.cap.release()
cv2.destroyAllWindows()
Additional
Please suggest corrections and improvements
👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.
For additional resources and information, please see the links below:
Docs: https://docs.ultralytics.com
HUB: https://hub.ultralytics.com
Community: https://community.ultralytics.com
Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!
Thank you for your contributions to YOLO 🚀 and Vision AI ⭐
Hello @rizkynat,
Thank you for bringing this issue to our attention. The error message Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:173
you're seeing is typically associated with multithreading issues in the FFmpeg library, particularly when multiple threads are trying to access the same resources concurrently.
In the specific case of your script that attempts to read from multiple camera streams using the VideoCapture
class, it's possible that the simultaneous access and queuing of frames might be causing some conflicts in resource usage.
To solve this issue, you might consider restructuring your code to manage access to these camera frames more effectively, maybe by using different synchronization mechanisms or adjusting the queuing logic to better handle concurrency. Please experiment with these ideas and see if they solve your issue.
We are always here to assist the YOLOv8 community - do not hesitate to provide any feedback about your progress, as it might also aid others in the community with similar issues.
Warm regards,
Glenn Jocher and the Ultralytics team.