-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
AudioIO: add spectrogram samples for C++/python #20934
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
Conversation
50cf0fc
to
fce3b22
Compare
fce3b22
to
0cf61d2
Compare
@allnes Check please |
samples/cpp/spectrogram.cpp
Outdated
{ | ||
line(img, Point(i-1, static_cast<int>(reshape_audio[i-1])), Point(i, static_cast<int>(reshape_audio[i])), color, thickness); | ||
} | ||
resize(img, img, Size(cols, rows), INTER_AREA ); |
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.
avoid "inplace" operations. There is performance src copy penalty.
samples/cpp/spectrogram.cpp
Outdated
"{inputType i | file | file or microphone }" | ||
"{draw d | static | type of drawing: \n\t\t\tstatic - for plotting graph(s) across the entire input audio \n\t\t\tdynamic - for plotting graph(s) in a time-updating window}" | ||
"{graph g | ampl_and_spec | type of graph: amplitude graph or/and spectrogram. Please use tags below : \n\t\t\tampl - draw the amplitude graph \n\t\t\tspec - draw the spectrogram\n\t\t\tampl_and_spec - draw the amplitude graph and spectrogram on one image under each other}" | ||
"{audio a |../../../samples/data/Megamind.avi| name and path to file }" |
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.
Use cv.samples.findFile
(Python) / samples::findFile
(C++) instead of ../
samples/python/spectrogram.py
Outdated
cv.CAP_PROP_AUDIO_DATA_DEPTH, cv.CV_16S] | ||
params = np.asarray(params) | ||
|
||
cap.open(file, cv.CAP_MSMF, params) |
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.
cv.CAP_MSMF
This should be removed. We want to support other backends too.
Use CAP_ANY
.
samples/python/spectrogram.py
Outdated
parser.add_argument("-g", "--graph", dest="graph", type=str, default="ampl_and_spec", | ||
help="type of graph: amplitude graph or/and spectrogram. Please use tags below : ampl - draw the amplitude graph; spec - draw the spectrogram; ampl_and_spec - draw the amplitude graph and spectrogram on one image under each other") | ||
|
||
parser.add_argument("-a", "--audio", dest="audio", type=str, default='../data/Megamind.avi', |
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.
../
need to be removed
samples/python/spectrogram.py
Outdated
@@ -0,0 +1,827 @@ | |||
import cv2 as cv | |||
import numpy as np |
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.
Due to compatibility reasons numpy
should go first (as a dependency)
samples/cpp/spectrogram.cpp
Outdated
} | ||
else if (windowType == "Hamming") | ||
{ | ||
double pi = 2 * acos(-1.0); |
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.
CV_PI, M_PI.
samples/cpp/spectrogram.cpp
Outdated
double pi = 2 * acos(-1.0); | ||
for (int j = 1 - windLen; j < windLen; j+=2) | ||
{ | ||
WindType.push_back(j * (0.53836 - 0.46164 * (cos(pi * j / (windLen - 1))))); |
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.
Please add link at Wiki article or other documents so reviewers can validate used expressions
samples/cpp/spectrogram.cpp
Outdated
string grid = parser.get<string>("grid"); | ||
if ((grid != "on") && (grid != "off")) |
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.
get<bool>()
samples/cpp/spectrogram.cpp
Outdated
|
||
} | ||
|
||
static bool checkArgs(CommandLineParser parser) |
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.
Input parameters should be passed as 'const reference'
samples/cpp/spectrogram.cpp
Outdated
if (cap.grab()) | ||
{ | ||
for (int nCh = 0; nCh < numberOfChannels; nCh++) | ||
{ |
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.
There is one imshow for multiple channels.
samples/cpp/spectrogram.cpp
Outdated
cap.open(0, CAP_MSMF, params); | ||
if (!cap.isOpened()) | ||
{ | ||
cerr << "ERROR! Can't to open microphone" << endl; |
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.
better:
cerr << "ERROR! Can't open microphone" << endl;
(w/o the "to")
a878ba1
to
6b23c8e
Compare
samples/cpp/spectrogram.cpp
Outdated
@@ -0,0 +1,1124 @@ | |||
#include <opencv2/core.hpp> |
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.
Please rename samples: add audio_
prefix
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.
Well done! Thank you 👍
AudioIO: add spectrogram samples for C++/python
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.