-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opencv: support cross-compilation and add sample function (#44)
* opencv: support cross-compilation and add sample function * nits: clean-up * func: pass --cmdline * opencv: add example doing k-NN inference * gha: re-generate wasm if we add new function * gha: few nits * cpp: bump for latest s3 * gha: make cache depend on functions too
- Loading branch information
1 parent
5cdd796
commit 268a78d
Showing
16 changed files
with
345 additions
and
5 deletions.
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
Submodule cpp
updated
3 files
+3 −3 | func/demo/calloc.cpp | |
+13 −1 | libfaasm/faasm/host_interface.h | |
+2 −0 | libfaasm/libfaasm.imports |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,7 @@ | ||
set(FAASM_USER opencv) | ||
|
||
faasm_example_func(check check.cpp) | ||
target_link_libraries(opencv_check opencv_core opencv_imgcodecs opencv_imgproc) | ||
|
||
faasm_example_func(pca pca.cpp) | ||
target_link_libraries(opencv_pca opencv_core opencv_imgcodecs opencv_imgproc opencv_ml z) |
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,46 @@ | ||
// Even when threading is completely disabled, OpenCV assumes the C++ library | ||
// has been built with threading support, and typedefs (no-ops) these two | ||
// symbols. To prevent an undefined symbol error, we define them here. | ||
namespace std { | ||
class recursive_mutex { | ||
public: | ||
void lock() {} | ||
bool try_lock() { return true; } | ||
void unlock() {} | ||
}; | ||
|
||
template <typename T> | ||
class lock_guard { | ||
public: | ||
explicit lock_guard(T&) {} | ||
}; | ||
} | ||
|
||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/imgcodecs.hpp> | ||
#include <opencv2/imgproc.hpp> | ||
#include <iostream> | ||
|
||
int main(int argc, char** argv) { | ||
if (argc != 3) { | ||
std::cerr << "Usage: <image_name_in.bmp> <image_name_out.bmp>" << std::endl; | ||
return 1; | ||
} | ||
|
||
// Load an image | ||
cv::Mat img = cv::imread(argv[1], cv::IMREAD_COLOR); | ||
if (img.empty()) { | ||
std::cerr << "Error: Could not load image!" << std::endl; | ||
return -1; | ||
} | ||
|
||
// Convert the image to grayscale | ||
cv::Mat gray; | ||
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY); | ||
|
||
// Save the grayscale image | ||
cv::imwrite(argv[2], gray); | ||
std::cout << "Image has been converted to grayscale and saved as " << argv[2] << std::endl; | ||
|
||
return 0; | ||
} |
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,88 @@ | ||
// Even when threading is completely disabled, OpenCV assumes the C++ library | ||
// has been built with threading support, and typedefs (no-ops) these two | ||
// symbols. To prevent an undefined symbol error, we define them here. | ||
namespace std { | ||
class recursive_mutex { | ||
public: | ||
void lock() {} | ||
bool try_lock() { return true; } | ||
void unlock() {} | ||
}; | ||
|
||
template <typename T> | ||
class lock_guard { | ||
public: | ||
explicit lock_guard(T&) {} | ||
}; | ||
} | ||
|
||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/imgcodecs.hpp> | ||
#include <opencv2/imgproc.hpp> | ||
#include <opencv2/ml.hpp> | ||
#include <iostream> | ||
#include <filesystem> | ||
|
||
void loadImages(const std::string& folder, std::vector<cv::Mat>& data, std::vector<int>& labels) { | ||
int label = 0; | ||
for (const auto& entry : std::filesystem::directory_iterator(folder)) { | ||
if (entry.is_regular_file()) { | ||
|
||
std::cout << " - loading image: " << entry.path().string() << " (" << label << ")" << std::endl; | ||
cv::Mat img = cv::imread(entry.path().string(), cv::IMREAD_GRAYSCALE); | ||
if (!img.empty()) { | ||
cv::resize(img, img, cv::Size(64, 64)); | ||
data.push_back(img.reshape(1, 1)); | ||
labels.push_back(label); | ||
} | ||
} | ||
|
||
label++; | ||
} | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (argc != 2) { | ||
std::cerr << "Usage: <image_path>" << std::endl; | ||
return 1; | ||
} | ||
|
||
// Load images | ||
std::cout << "Beginning to load images..." << std::endl; | ||
std::vector<cv::Mat> images; | ||
std::vector<int> labels; | ||
loadImages(argv[1], images, labels); | ||
std::cout << "Images loaded!" << std::endl; | ||
|
||
// Convert data to a single matrix | ||
std::cout << "Converting data..." << std::endl; | ||
cv::Mat data; | ||
cv::vconcat(images, data); | ||
data.convertTo(data, CV_32F); | ||
std::cout << "Data converted!" << std::endl; | ||
|
||
// Perform PCA with 10 principal components | ||
std::cout << "Performing PCA analysis..." << std::endl; | ||
cv::PCA pca(data, cv::Mat(), cv::PCA::DATA_AS_ROW, 10); | ||
cv::Mat pcaResult; | ||
pca.project(data, pcaResult); | ||
std::cout << "PCA on images succeded!" << std::endl; | ||
|
||
// Prepare labels for training | ||
cv::Mat labelsMat(labels); | ||
labelsMat.convertTo(labelsMat, CV_32S); | ||
|
||
// Train k-NN classifier with PCA result | ||
cv::Ptr<cv::ml::KNearest> knn = cv::ml::KNearest::create(); | ||
knn->setDefaultK(3); | ||
knn->train(pcaResult, cv::ml::ROW_SAMPLE, labelsMat); | ||
std::cout << "Training k-NN classifier succeeded!" << std::endl; | ||
|
||
// Perform a prediction on the first sample as an example | ||
cv::Mat sample = pcaResult.row(0); | ||
cv::Mat response; | ||
knn->findNearest(sample, knn->getDefaultK(), response); | ||
std::cout << "Predicted label: " << response.at<float>(0, 0) << std::endl; | ||
|
||
return 0; | ||
} |
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
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
Oops, something went wrong.