Skip to content

Instantly share code, notes, and snippets.

@HayatoTomita
Created October 7, 2017 17:56
Show Gist options
  • Save HayatoTomita/a4c4037ad27b1994d62917a573ea2745 to your computer and use it in GitHub Desktop.
Save HayatoTomita/a4c4037ad27b1994d62917a573ea2745 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# cling-jupyterでopencv顔検出"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.インクルードパスとライブラリパスを通す\n",
"` .I path` でインクルードパスを、 `.L path`でライブラリのパスを通します.\n",
"本当は1セルにまとめて実行したいところですが、1セルにまとめるとそのあとのインクルードで失敗する現象を確認しました. なので、仕方なく1セルごとに実行・・・. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".I /usr/local/Cellar/opencv/3.3.0_3/include"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".L /usr/local/Cellar/opencv/3.3.0_3/lib/libopencv_highgui.3.3.0.dylib"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".L /usr/local/Cellar/opencv/3.3.0_3/lib/libopencv_core.3.3.0.dylib"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".L /usr/local/Cellar/opencv/3.3.0_3/lib/libopencv_imgproc.3.3.0.dylib"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".L /usr/local/Cellar/opencv/3.3.0_3/lib/libopencv_objdetect.3.3.0.dylib"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## 2. あとは普通に書いて実行する\n",
"パスを通して環境が出来上がったので、ヘッダファイルインクルードして普通にC++を書く."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#include <opencv2/core.hpp>\n",
"#include <opencv2/imgproc.hpp>\n",
"#include <opencv2/highgui.hpp>\n",
"#include <opencv2/objdetect.hpp>\n",
"#include <iostream>"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(cv::Mat &) @0x1026d4118\n"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cv::Mat src_img;\n",
"cv::Mat dst_img;\n",
"src_img = cv::imread(\"/Users/xxxxxx/lenna.png\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### メモ\n",
"python-jupyter的な感覚で試行錯誤のために同じセルをガンガン実行するためのコツとして、変数の宣言は別のセルにするのが良いです. \n",
"あくまでもC++なので、変数二重で宣言すると当然エラーになります. \n",
"エラー出すと、たまにnotebookごと落ちます・・・"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cv::CascadeClassifier cascade;\n",
"std::vector<cv::Rect> faces;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(void) @0x700008997358\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cascade.load(\"/usr/local/Cellar/opencv/3.3.0_3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml\");\n",
"cascade.detectMultiScale(src_img, faces, 1.1, 3, 0, cv::Size(100,100));"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"for(auto r : faces){\n",
"cv::rectangle(src_img, cv::Point(r.x, r.y), cv::Point(r.x + r.width, r.y+r.height), cv::Scalar(0, 200), 3, CV_AA);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(void) @0x700008997358\n"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cv::namedWindow(\"lenna\", CV_WINDOW_AUTOSIZE);\n",
"cv::imshow(\"lenna\", src_img);"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(void) @0x70000ad3b358\n"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cv::destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++11",
"language": "",
"name": "cling-cpp11"
},
"language_info": {
"codemirror_mode": "c++",
"file_extension": ".c++",
"mimetype": "text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment