-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mihaelv gen3 emotion detection WIP #1061
Open
mihapiha
wants to merge
4
commits into
luxonis:v3_develop
Choose a base branch
from
mihapiha:mihaelv_gen3_emotion
base: v3_develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Includes common necessary includes for development using depthai library | ||
#include "depthai/depthai.hpp" | ||
|
||
class Face2ImageManipConfig : public dai::NodeCRTP<dai::node::ThreadedHostNode, Face2ImageManipConfig> { | ||
public: | ||
Input inputDetections = dai::Node::Input{*this, {}}; | ||
Output outputManipulators = dai::Node::Output{*this, {}}; | ||
|
||
void run() override { | ||
while(isRunning()) { | ||
std::shared_ptr<dai::ImgDetections> inDet; | ||
inDet = inputDetections.get<dai::ImgDetections>(); | ||
|
||
if(!inDet) | ||
{ | ||
continue; | ||
} | ||
|
||
for(auto& detection : inDet->detections) { | ||
std::shared_ptr<dai::ImageManipConfig> manipulator = std::make_shared<dai::ImageManipConfig>(); | ||
manipulator->setCropRect(detection.xmin, | ||
detection.ymin, | ||
detection.xmax, | ||
detection.ymax); | ||
manipulator->setResize(64,64); | ||
outputManipulators.send(manipulator); | ||
} | ||
|
||
} | ||
} | ||
}; | ||
|
||
void displayFrame(cv::Mat& frame, std::vector<dai::ImgDetection>& detections, std::vector<dai::ImgDetection>& detectionsEmo) | ||
{ | ||
auto color = cv::Scalar(255, 0, 0); | ||
for(int i = 0; i < detections.size(); i++) { | ||
auto& detection = detections[i]; | ||
int x1 = detection.xmin * frame.cols; | ||
int y1 = detection.ymin * frame.rows; | ||
int x2 = detection.xmax * frame.cols; | ||
int y2 = detection.ymax * frame.rows; | ||
|
||
std::stringstream confStr; | ||
if(i < detectionsEmo.size()) { | ||
auto& detEmo = detectionsEmo[i]; | ||
confStr << "label: " << detEmo.label << " " <<std::fixed << std::setprecision(2) << detEmo.confidence * 100; | ||
} | ||
else | ||
{ | ||
confStr << "no emotion"; | ||
} | ||
cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color); | ||
cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX); | ||
} | ||
cv::imshow("video", frame); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
std::string nnPath; | ||
std::string nnEmoPath; | ||
if(argc > 2) { | ||
nnPath = std::string(argv[1]); | ||
nnEmoPath = std::string(argv[2]); | ||
} else { | ||
std::cout << "call with arguments: {detection blob} {emotion blob}" << std::endl; | ||
return 1; | ||
} | ||
|
||
|
||
// Create pipeline | ||
auto device = std::make_shared<dai::Device>(dai::OpenVINO::VERSION_UNIVERSAL, dai::UsbSpeed::HIGH); | ||
dai::Pipeline pipeline(device); | ||
// Define source and output | ||
auto camRgb = pipeline.create<dai::node::ColorCamera>(); | ||
auto nn = pipeline.create<dai::node::NeuralNetwork>(); | ||
auto nnEmo = pipeline.create<dai::node::NeuralNetwork>(); | ||
auto det = pipeline.create<dai::node::DetectionParser>(); | ||
auto detEmo = pipeline.create<dai::node::DetectionParser>(); | ||
auto manipConf = pipeline.create<Face2ImageManipConfig>(); | ||
auto manip = pipeline.create<dai::node::ImageManip>(); | ||
|
||
// Camera props | ||
camRgb->setBoardSocket(dai::CameraBoardSocket::CAM_A); | ||
camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P); | ||
camRgb->setPreviewSize(300, 300); | ||
camRgb->setInterleaved(false); | ||
|
||
// Face detection NN props | ||
nn->setNumInferenceThreads(2); | ||
nn->input.setBlocking(false); | ||
dai::OpenVINO::Blob blob1(nnPath); | ||
nn->setBlob(blob1); | ||
|
||
// Face detection NN parser props | ||
det->setBlob(blob1); | ||
det->setNNFamily(DetectionNetworkType::MOBILENET); | ||
det->setConfidenceThreshold(0.5); | ||
|
||
// Emotion detection NN props | ||
nnEmo->setNumInferenceThreads(2); | ||
nnEmo->input.setBlocking(false); | ||
dai::OpenVINO::Blob blob2(nnEmoPath); | ||
nnEmo->setBlob(blob2); | ||
|
||
// Emotion detection NN parser props | ||
detEmo->setBlob(blob2); | ||
detEmo->setNNFamily(DetectionNetworkType::MOBILENET); | ||
detEmo->setConfidenceThreshold(0.5); | ||
detEmo->setNumClasses(5); | ||
|
||
// ImageManip props | ||
manip->initialConfig.setResize(64,64); | ||
|
||
// Linking | ||
/* | ||
rgb -> nn -> det -> manipConf -> manip -> nnEmo -> detEmo | ||
---------------------------> | ||
*/ | ||
camRgb->preview.link(nn->input); | ||
nn->out.link(det->input); | ||
det->out.link(manipConf->inputDetections); | ||
manipConf->outputManipulators.link(manip->inputConfig); | ||
camRgb->preview.link(manip->inputImage); | ||
manip->out.link(nnEmo->input); | ||
nnEmo->out.link(detEmo->input); | ||
|
||
auto outPassthrough = nn->passthrough.createOutputQueue(); | ||
auto outDet = det->out.createOutputQueue(); | ||
auto outDetEmo = detEmo->out.createOutputQueue(); | ||
|
||
pipeline.start(); | ||
while(pipeline.isRunning()) { | ||
std::shared_ptr<dai::ImgFrame> inRgb; | ||
std::shared_ptr<dai::ImgDetections> inDet; | ||
std::shared_ptr<dai::ImgDetections> inDetEmo; | ||
|
||
inRgb = outPassthrough->get<dai::ImgFrame>(); | ||
inDet = outDet->get<dai::ImgDetections>(); | ||
inDetEmo = outDetEmo->get<dai::ImgDetections>(); | ||
|
||
cv::Mat frame; | ||
std::vector<dai::ImgDetection> detections; | ||
std::vector<dai::ImgDetection> detectionsEmo; | ||
|
||
if(inRgb) { | ||
frame = inRgb->getCvFrame(); | ||
} | ||
|
||
if(inDet) { | ||
detections = inDet->detections; | ||
} | ||
|
||
if(inDetEmo) | ||
{ | ||
detectionsEmo = inDetEmo->detections; | ||
} | ||
|
||
if(!frame.empty()) { | ||
displayFrame(frame, detections, detectionsEmo); | ||
} | ||
|
||
int key = cv::waitKey(1); | ||
if(key == 'q' || key == 'Q') { | ||
return 0; | ||
} | ||
} | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this right? I think the nnEmo likely needs to be parsed on host.