void readRTSP(const std::string&& rtsp) {
cv::VideoCapture cap;
auto cvname = std::this_thread::get_id();
std::ostringstream oss;
oss << cvname;
auto strName = oss.str();
std::cout << "thread id " << strName << "read: " << rtsp << std::endl;
cap.open(rtsp);
if (!cap.isOpened()) {
std::cout << "open video failed";
cap.release();
}
cv::Mat frame;
for (;;) {
cap >> frame;
if (frame.empty())
{
std::cout << "thread-" << strName << " frame empty! " << std::endl;;
continue;
}
std::unique_lock<std::mutex> lock{ m_mutex };
while (frame_que->size() == MAXQUESIZE)
{
std::cout << "**********队列满*********" << std::endl;
my_cond.wait(lock);
}
std::cout << "+++++++压入队列+++++++" << std::endl;
frame_que->push(frame);
my_cond.notify_one();
std::cout << "生产者唤醒消费者" << std::endl;
}
}
for (;;)
{
std::unique_lock<std::mutex>lock{ m_mutex };
while (frame_que->empty())
{
std::cout << "**********队列空*********" << std::endl;
my_cond.wait(lock);
}
while (!frame_que->empty()) {
auto frame = frame_que->front();
auto res = detector.Run(frame, conf_thres, iou_thres);
Demo(frame, res, class_names);
frame_que->pop();
my_cond.notify_one();
std::cout << "消费者唤醒生产者" << std::endl;
}
}
void readRTSP(const std::string&& rtsp) {
cv::VideoCapture cap;
auto cvname = std::this_thread::get_id();
std::ostringstream oss;
oss << cvname;
auto strName = oss.str();
std::cout << "thread id " << strName << "read: " << rtsp << std::endl;
cap.open(rtsp);
if (!cap.isOpened()) {
std::cout << "open video failed";
cap.release();
}
cv::Mat frame;
while (cap.read(frame)) {
if (frame.empty())
{
std::cout << "thread-" << strName << " frame empty! " << std::endl;;
continue;
}
if (frame_que->size() < MAXQUESIZE) {
std::cout << "+++++++压入队列+++++++" << std::endl;
frame_que->push(frame);
}
else
{
std::cout << "**********队列满*********" << std::endl;
continue;
}
}
cap.release();
cv::destroyAllWindows();
}
fun2():
m_mutex.lock();
while (!frame_que->empty()) {
auto frame = frame_que->front();
auto res = detector.Run(frame, conf_thres, iou_thres);
Demo(frame, res, class_names);
frame_que->pop();
m_mutex.unlock();
}
std::cout << "-------队列空------" << std::endl;
m_mutex.unlock();
or fun2():
for (;;)
{
std::unique_lock<std::mutex>lock{ m_mutex };
m_mutex.lock();
if (!frame_que->empty()) {
auto frame = frame_que->front();
auto res = detector.Run(frame, conf_thres, iou_thres);
Demo(frame, res, class_names);
frame_que->pop();
}
else
{
std::cout << "-------队列空------" << std::endl;
m_mutex.unlock();
continue;
}
m_mutex.unlock();
}