Skip to content

Commit

Permalink
ergoCubEmotions: avoid reading videos at each update of the module (#214
Browse files Browse the repository at this point in the history
)
  • Loading branch information
martinaxgloria authored Jan 23, 2024
1 parent eaacbf1 commit 1533382
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
43 changes: 19 additions & 24 deletions app/ergoCubEmotions/conf/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@
num_expressions 4
num_transitions 12

# [expression_0]
# name neutral
# type image
# file expressions/images/exp_img_0.png

# [expression_1]
# name happy
# type image
# file expressions/images/exp_img_1.png

# [expression_2]
# name alert
# type image
# file expressions/images/exp_img_2.png

# [expression_3]
# name shy
# type image
# file expressions/images/exp_img_3.png

# [expression_4]
# name wink
# type video
# file expressions/videos/exp_vid_0.mp4
#[expression_0]
#name neutral
#type image
#file expressions/images/exp_img_0.png
#
#[expression_1]
#name happy
#type image
#file expressions/images/exp_img_1.png
#
#[expression_2]
#name alert
#type image
#file expressions/images/exp_img_2.png
#
#[expression_3]
#name shy
#type image
#file expressions/images/exp_img_3.png

[expression_0]
name neutral
Expand Down
33 changes: 31 additions & 2 deletions src/modules/ergoCubEmotions/ergoCubEmotions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ bool ErgoCubEmotions::configure(ResourceFinder& rf)

std::pair<std::string, std::string> par = std::make_pair(type, filePath);
imgMap[name] = par;

if(!(std::count(videoFileNames.begin(), videoFileNames.end(), filePath)))
{
videoFileNames.push_back(filePath);
VideoCapture cap(filePath);
videoCaptures.push_back(cap);
}
}

for(int j = 0; j < nTransitions; j++)
Expand Down Expand Up @@ -88,6 +95,11 @@ bool ErgoCubEmotions::configure(ResourceFinder& rf)
std::string filePath = rf.findFile(file);
std::pair<std::string, std::string> par = std::make_pair(source, destination);
transitionMap[par] = filePath;

if(!(std::count(videoFileNames.begin(), videoFileNames.end(), filePath)))
{
videoFileNames.push_back(filePath);
}
}

isTransition = true;
Expand Down Expand Up @@ -159,7 +171,16 @@ bool ErgoCubEmotions::updateModule()
{
showTransition(current_local, command_local);
}
VideoCapture cap(info.second);

VideoCapture cap;
for (size_t i = 0; i < videoFileNames.size(); i++)
{
if(info.second == videoFileNames[i])
{
cap = videoCaptures.at(i);
}
}

Mat frame;

while(cap.isOpened())
Expand All @@ -172,6 +193,7 @@ bool ErgoCubEmotions::updateModule()
imshow("emotion", frame);
pollKey();
}
cap.release();
}

return true;
Expand Down Expand Up @@ -205,7 +227,14 @@ void ErgoCubEmotions::showTransition(const std::string& current, const std::stri
{
if(k->first.first == current && k->first.second == desired)
{
VideoCapture capTrans(k->second);
VideoCapture capTrans;
for (size_t i = 0; i < videoFileNames.size(); i++)
{
if(k->second == videoFileNames[i])
{
capTrans.open(videoFileNames[i]);
}
}
Mat frameTrans;

while(capTrans.isOpened())
Expand Down
2 changes: 2 additions & 0 deletions src/modules/ergoCubEmotions/ergoCubEmotions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ErgoCubEmotions : public yarp::os::RFModule, public ergoCubEmotions_IDL {
std::vector<std::string> avlEmotions;
cv::Mat img;
std::mutex mutex;
std::vector<cv::VideoCapture> videoCaptures;
std::vector<std::string> videoFileNames;
};

#endif

0 comments on commit 1533382

Please sign in to comment.