0%

安装

安装Node.js

1
brew install node
1
2
node -v         
v18.11.0

安装Hexo[1]

1
npm install -g hexo-cli
1
2
npm info hexo-cli version
4.3.0

Blog文件夹下初始化Hexo:

1
2
3
cd Blog
hexo init
npm install

升级Hexo[2]

进入带有package.json文件的Hexo文件夹

1
npm update

安装/升级NexT主题[3] [4]

Blog文件夹下:

1
npm install hexo-theme-next@latest

修改Blog/_config.yml文件:

1
theme: next

测试:

1
2
hexo clean
hexo s --debug

浏览器打开http://localhost:4000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
hexo -v

INFO Validating config
INFO ==================================
███╗ ██╗███████╗██╗ ██╗████████╗
████╗ ██║██╔════╝╚██╗██╔╝╚══██╔══╝
██╔██╗ ██║█████╗ ╚███╔╝ ██║
██║╚██╗██║██╔══╝ ██╔██╗ ██║
██║ ╚████║███████╗██╔╝ ██╗ ██║
╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝ ╚═╝
========================================
NexT version 8.13.1
Documentation: https://theme-next.js.org
========================================
hexo: 6.3.0
hexo-cli: 4.3.0
os: darwin 21.6.0 12.6

node: 18.11.0
v8: 10.2.154.15-node.12
uv: 1.44.2
zlib: 1.2.11
brotli: 1.0.9
ares: 1.18.1
modules: 108
nghttp2: 1.50.0
napi: 8
llhttp: 6.0.10
openssl: 1.1.1q
cldr: 41.0
icu: 71.1
tz: 2022a
unicode: 14.0

配置

在Hexo中有两份主要的配置文件,其名称都是_config.yml。 其中,一份位于站点根目录下,主要包含Hexo本身的配置;另一份位于主题目录下/node_modules/hexo-theme-next/_config.yml,这份配置由主题作者提供,主要用于配置主题相关的选项。为了描述方便,在以下说明中,将前者称为站点配置文件, 后者称为主题配置文件

主题配置文件

主题配置文件文件/node_modules/hexo-theme-next/_config.yml中修改:

Schemes

1
scheme: Pisces

language

1
language: en
1
2
3
4
5
6
7
8
9
10
11
12
13
# Usage: `Key: /link/ || icon`
# Key is the name of menu item. If the translation for this item is available, the translated text will be loaded, otherwise the Key name will be used. Key is case-sensitive.
# Value before `||` delimiter is the target link, value after `||` delimiter is the name of Font Awesome icon.
# External url should start with http:// or https://
menu:
home: / || fa fa-home
about: /about/ || fa fa-user
tags: /tags/ || fa fa-tags
categories: /categories/ || fa fa-th
archives: /archives/ || fa fa-archive
#schedule: /schedule/ || fa fa-calendar
#sitemap: /sitemap.xml || fa fa-sitemap
#commonweal: /404/ || fa fa-heartbeat

手动创建页面tags:

1
hexo new page "tags"

修改Blog/source/tags/index.md

1
2
3
4
5
6
---
title: tags
date: 2016-01-28 18:44:44
type: "tags"
comments: false
---
1
hexo new page "categories"

修改Blog/source/categories/index.md

1
2
3
4
5
6
---
title: categories
date: 2016-01-28 19:18:13
type: "categories"
comments: false
---

手动创建页面categories:

1
hexo new page "categories"

修改Blog/source/categories/index.md

1
2
3
4
5
6
---
title: categories
date: 2016-01-28 19:18:13
type: "categories"
comments: false
---

手动创建页面about:

1
hexo new page "about"

修改Blog/source/about/index.md

1
2
3
4
5
---
title: about
date: 2016-01-28 19:43:39
comments: false
---
1
2
3
sidebar:
position: left
display: always

头像

放置在source/images/目录下,配置为:avatar: /images/avatar.png

1
2
3
4
5
6
# Sidebar Avatar
avatar:
# Replace the default image and set the url here.
url: /images/avatar.png
# If true, the avatar will be displayed in circle.
rounded: true

Google Analytics

1
2
3
4
5
6
7
# Google Analytics
# See: https://analytics.google.com
google_analytics:
tracking_id: UA-27799979-1
# By default, NexT will load an external gtag.js script on your site.
# If you only need the pageview feature, set the following option to true to get a better performance.
only_pageview: false

社交链接

1
2
3
4
5
social:
GitHub: https://github.com/HanjieLuo || fab fa-github
Linkedin: https://www.linkedin.com/in/hanjie-luo-89602197 || fa-brands fa-linkedin-in
YouTube: https://www.youtube.com/luohanjie || fab fa-youtube
Bilibili: https://space.bilibili.com/319752 || fa-brands fa-bilibili

disqus

1
2
3
4
disqus:
enable: true
shortname: luohanjie
count: true

代码高亮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
codeblock:
# Code Highlight theme
# All available themes: https://theme-next.js.org/highlight/
theme:
light: stackoverflow-dark
dark: stackoverflow-dark
prism:
light: prism
dark: prism-dark
# Add copy button on codeblock
copy_button:
enable: false
# Available values: default | flat | mac
style:

访客统计、访问次数统计、文章阅读次数统计

1
2
3
4
5
6
7
8
9
10
# Show Views / Visitors of the website / page with busuanzi.
# For more information: http://ibruce.info/2015/04/04/busuanzi/
busuanzi_count:
enable: true
total_visitors: true
total_visitors_icon: fa fa-user
total_views: true
total_views_icon: fa fa-eye
post_views: true
post_views_icon: far fa-eye

本地显示的数字是随机的,部署到服务器上面就好了。

站点建立时间

1
2
3
footer:
# Specify the year when the site was setup. If not defined, current year will be used.
since: 2016
1
npm install hexo-generator-searchdb

Blog\_config.yml添加:

1
2
3
4
5
search:
path: search.xml
field: post
content: true
format: html

修改/node_modules/hexo-theme-next/_config.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Local search
# Dependencies: https://github.com/next-theme/hexo-generator-searchdb
local_search:
enable: true
# If auto, trigger search by changing input.
# If manual, trigger search by pressing enter key or search button.
trigger: auto
# Show top n results per article, show all results by setting to -1
top_n_per_article: 1
# Unescape html strings to the readable one.
unescape: false
# Preload the search data when the page loads.
preload: false

mathjax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Math Formulas Render Support
# Warning: Please install / uninstall the relevant renderer according to the documentation.
# See: https://theme-next.js.org/docs/third-party-services/math-equations
# Server-side plugin: https://github.com/next-theme/hexo-filter-mathjax
math:
# Default (false) will load mathjax / katex script on demand.
# That is it only render those page which has `mathjax: true` in front-matter.
# If you set it to true, it will load mathjax / katex script EVERY PAGE.
every_page: true

mathjax:
enable: true
# Available values: none | ams | all
tags: none

katex:
enable: false
# See: https://github.com/KaTeX/KaTeX/tree/master/contrib/copy-tex
copy_tex: false

CDN

1
2
3
4
5
vendors:
internal: local
plugins: cdnjs
# Custom CDN URL
custom_cdn_url: https://lib.baomitu.com/${cdnjs_name}/${version}/${cdnjs_file}

Favicon

准备一张260x260以上大的图片:

favicon

用软件转换成svg格式,改名为logo.svg。在realfavicongenerator上生成favicon_package包,包括favicon-16x16-next.png(由favicon-16x16.png改名),favicon-32x32-next.png(由favicon-32x32.png改名),apple-touch-icon-next.png(由apple-touch-icon.png改名)。将以上4张图片复制到Blog/node_modules/hexo-theme-next/source/images/进行替换。

站点配置文件

站点配置文件文件_config.yml中修改:

网站

1
2
3
4
5
6
7
title: Hanjie's Blog
subtitle: 一只有理想的羊驼
description: ''
keywords:
author: Hanjie Luo
language: en
timezone: ''

网址

1
2
3
4
5
6
7
8
# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: http://luohanjie.com
permalink: :year-:month-:day/:urlname.html
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
trailing_html: true # Set to false to remove trailing '.html' from permalinks

文章的Formatter:

1
2
3
4
5
6
7
title: Mac上安装和配置Hexo博客
urlname: install-and-create-a-blog-with-hexo-on-mac
categories: [Tech, Web]
date: 2022-10-25 16:08:00
tags: [Web, Mac, Hexo, Next]
published: true
---

文章

1
2
3
4
5
6
7
8
# Writing
new_post_name: :title.md # File name of new posts
filename_case: 1
highlight:
enable: true
line_number: false
auto_detect: true
tab_replace:

分页

1
2
3
4
index_generator:
path: ''
per_page: 3
order_by: -date

hexo-renderer-markdown-it

支持footnote

可以通过将markdown渲染器替换为hexo-renderer-markdown-it,使得支持footnote功能[5]。必须要先卸载原先的渲染器,然后安装:

1
2
npm un hexo-renderer-marked --save
npm i hexo-renderer-markdown-it --save

然后在网站\_config.yml文件中添加:

1
2
3
markdown:
plugins:
- markdown-it-footnote

然后发现每一条footnote间都有一行间隔。想要去掉的话,可以在/Blogs//Users/luohanjie/Workspace/Web/Blog/node_modules/hexo-theme-next/source/css/_common/components/post/post-footer.styl中添加:

1
2
3
.footnote-item p {
margin-bottom: 0
}

hexo-renderer-markdown-it还支持很多其他的功能,具体可以到官网了解。

html

The html setting defines whether or not HTML content inside the document should be escaped or passed to the final result.

1
2
3
markdown:
render:
html: true # Doesn't escape HTML content

支持插入pdf

1
npm install --save hexo-pdf

主题配置文件文件/node_modules/hexo-theme-next/_config.yml中修改:

1
2
3
4
pdf:
enable: true
# Default height
height: 500px

使用:

1
2
3
4
5
6
7
8
# Normal PDF
{% pdf http://7xov2f.com1.z0.glb.clouddn.com/bash_freshman.pdf %}

#Google drive
{% pdf https://drive.google.com/file/d/0B6qSwdwPxPRdTEliX0dhQ2JfUEU/preview %}

#Slideshare
{% pdf http://www.slideshare.net/slideshow/embed_code/key/8Jl0hUt2OKUOOE %}

自定义字体

修改/node_modules/hexo-theme-next/_config.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
font:
enable: false

# Uri of fonts host, e.g. https://fonts.googleapis.com (Default).
host: https://cdn.baomitu.com

# Font options:
# `external: true` will load this font family from `host` above.
# `family: Times New Roman`. Without any quotes.
# `size: x.x`. Use `em` as unit. Default: 1 (16px)

# Global font settings used for all elements inside <body>.
global:
external: true
family: Noto Serif SC
size:

# Font settings for site title (.site-title).
title:
external: true
family: Noto Serif SC
size:

# Font settings for headlines (<h1> to <h6>).
headings:
external: true
family: Noto Serif SC
size:

# Font settings for posts (.post-body).
posts:
external: true
family: Noto Serif SC

# Font settings for <code> and code blocks.
codes:
external: true
family: Source Code Pro

编辑主题的Blog/node_modules/hexo-theme-next/source/css/_variables/base.styl文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Font size
$font-size-base = (hexo-config('font.enable') and hexo-config('font.global.size') is a 'unit') ? unit(hexo-config('font.global.size'), em) : 1em;
$font-size-smallest = .55em;
$font-size-smaller = .6125em;
$font-size-small = .675em;
$font-size-medium = 0.8em;
$font-size-large = 0.925em;
$font-size-larger = 1.15em;
$font-size-largest = 1.7em;


// Headings font size
$font-size-headings-step = .125em;
$font-size-headings-base = (hexo-config('font.enable') and hexo-config('font.headings.size') is a 'unit') ? unit(hexo-config('font.headings.size'), em) : 1.425em;

sitemap

1
npm install hexo-generator-sitemap --save

服务器部署[6]

使用Git Hook自动部署到vps上。

创建用户

服务器上,创建git用户并且赋予权限:

1
2
3
adduser git
chmod 740 /etc/sudoers
nano /etc/sudoers
1
2
3
# User privilege specification
root ALL=(ALL:ALL) ALL
git ALL=(ALL:ALL) ALL
1
chmod 440 /etc/sudoers

建立密钥

本地Mac上,查看密钥(没有的话需要创建一个):

1
ls -al ~/.ssh

创建钥匙密钥:

1
2
3
4
cd ~/.ssh 
ssh-keygen -t rsa -C "你的邮箱" // 执行这个命令会提示输入用于保存的密钥名和口令之类的,都不填

touch ~/.ssh/config
1
2
3
4
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
1
ssh-add -K ~/.ssh/id_rsa

复制公匙:

1
pbcopy < ~/.ssh/id_rsa.pub

服务器上:

1
2
3
4
cd /home/git                   //切换到git用户目录
mkdir .ssh //创建.ssh目录
cd .ssh //进入.ssh目录
nano authorized_keys //将本地的公钥复制到authorized_keys文件里
1
2
3
chmod 700 /home/git
chmod 700 /home/git/.ssh #只有拥有者有读、写、执行权限
chmod 600 /home/git/.ssh/authorized_keys #只有拥有者有读写权限
1
nano /etc/ssh/sshd_config
1
2
3
4
// 取消这些注释
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
1
sudo /etc/init.d/ssh restart

关闭git用户的shell权限,设置后git用户可以通过ssh正常使用git服务,但无法登录shell:

1
nano /etc/passwd
1
git:x:1001:1001:,,,:/home/git:/bin/bash 改成 git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

Git仓库

服务器上:

1
2
3
4
5
6
apt-get install git

cd /home/git //切换到git用户目录
mkdir blog.git //创建仓库目录,以blog.git为例
cd blog.git //进入仓库目录
git init --bare //使用--bare参数初始化为裸仓库,这样创建的仓库不包含工作区

修改/home/git/blog.git目录的用户组权限为git:git

1
2
3
sudo chown git:git -R /home/git/.ssh
sudo chown git:git -R /home/git/.ssh/authorized_keys
sudo chown git:git -R /home/git/blog.git

本地Mac上测试:

1
2
3
ssh -v git@ip

git clone ssh://git@ip:port/home/git/blog.git

Git Hooks自动部署

在本地编辑Markdown文章,然后使用Git推送到VPS的Git仓库。Git Hooks实际上就是当Git仓库收到最新的push时,将Git仓库接受到的内容复制到VPS上的网站目录内。相当于完成了手动将public文件夹复制到VPS的网站根目录里的操作。

建立网站根目录:

1
2
3
cd /home/git
mkdir blog
sudo chown git:git -R /home/git/blog

创建post-receive文件:

1
2
cd /home/git/blog.git/hooks     //切换到hooks目录下
nano post-receive //创建post-receive文件并编辑
1
2
3
4
5
6
7
8
#!/bin/bash
GIT_REPO=/home/git/blog.git
TMP_GIT_CLONE=/tmp/blog
PUBLIC_WWW=/home/git/blog
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
1
chmod +x post-receive

部署Nginx

服务器上:

1
2
3
sudo apt-get install nginx

sudo vim /etc/nginx/conf.d/blog.conf

写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
listen 80;
server_name example.com; # 自己的域名

root /home/git/blog; # 刚才说的路径
access_log /var/log/nginx/blog_access.log;
error_log /var/log/nginx/blog_error.log;

# 这里是针对静态资源文件做个缓存
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 1d;
add_header Pragma public;
add_header Cache-Control "public";
}

# 如果使用了hexo-pdf插件,并且将文件放到服务器上,则还需添加
location ~* \.(pdf)$ {
add_header "Access-Control-Allow-Origin" "http://nagland.github.io";
expires 1d;
}

# 这里就是把请求转给我们的静态文件了
location / {
root /home/git/blog;
if (-f $request_filename) {
rewrite ^/(.*)$ /$1 break;
}
}
}

由于nginx的运行用户没有权限访问网站所在的目录,检查:

1
/etc/nginx/nginx.conf

可以将user的值改为git

重启:

1
service nginx restart

Hexo远程部署

本地Blog文件夹下:

1
npm install hexo-deployer-git --save

站点配置文件文件_config.yml中修改:

1
2
3
4
deploy:
type: git
repo: git@ip:blog.git
branch: master
1
git clone ssh://git@ip:port/home/git/blog.git
1
2
3
hexo clean
hexo generate
hexo deploy

  1. https://hexo.io/zh-cn/docs/ ↩︎

  2. https://dandyxu.me/Hexo/How-to-update-Hexo-and-Hexo-theme-properly/ ↩︎

  3. http://theme-next.iissnan.com/getting-started.html ↩︎

  4. https://israynotarray.com/hexo/20201101/60919/ ↩︎

  5. https://github.com/hexojs/hexo-renderer-markdown-it ↩︎

  6. https://www.eula.club/搭建Hexo静态博客并使用Git部署到VPS.html ↩︎

Unity: 2021.2.10f1c1 silicon
OS: macOS Monterey 12.1
Compiler: Clang 13.0.0 arm64-apple-darwin21.1.0

一个简单的Demo

新建一个c++文件unity_api.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#if defined(__CYGWIN32__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#else
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#endif

extern "C" {
UNITY_CPP_INTEROP_DLL_BRIDGE float Add(const float a,
const float b) {
return a + b;
}
}

CMakeLists.txt:

1
2
3
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O3 -D__ARM_NEON__ -DENABLE_NEON -Wno-unused-result -march=armv8-a+fp+simd+crypto -DCMAKE_APPLE_SILICON_PROCESSOR=arm64")

add_library(unity_api SHARED unity_api.cpp)

编译生成libunity_api.dylib,并且放到UnityProject/Assets/Plugins/macOS文件夹下,并且在Unity中对库进行设置:

unity_inspector

编写Unity的C#脚本PluginImport.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class PluginImport : MonoBehaviour
{
[DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)]
public static extern float Add(float a, float b);

// Start is called before the first frame update
void Start()
{
Debug.Log(Add(1, 2));
}

}

并且将脚本PluginImport.cs挂靠到Unity的Main Camera下,并且运行,可以在Console下看到结果:

unity_simple_test_output

使用OpenCV库

修改c++文件unity_api.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <opencv2/opencv.hpp>

#if defined(__CYGWIN32__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#else
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#endif



extern "C" {
UNITY_CPP_INTEROP_DLL_BRIDGE float Add(const float a,
const float b) {
return a + b;
}

UNITY_CPP_INTEROP_DLL_BRIDGE bool ToGray(const char *color_img_file,
const char *gray_img_file) {

cv::Mat img = cv::imread(std::string(color_img_file));
if (img.empty()) {
return false;
}

cv::Mat img_gray;
cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);
cv::imwrite(std::string(gray_img_file), img_gray);
return true;
}
}

CMakeLists.txt:

1
2
3
4
5
6
7
8
9
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O3 -D__ARM_NEON__ -DENABLE_NEON -Wno-unused-result -march=armv8-a+fp+simd+crypto -DCMAKE_APPLE_SILICON_PROCESSOR=arm64")

find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})

add_library(unity_api SHARED unity_api.cpp)
target_link_libraries(unity_api ${OpenCV_LIBS})

编译生成libunity_api.dylib,并且放到UnityProject/Assets/Plugins/macOS文件夹下。修改Unity的C#脚本PluginImport.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class PluginImport : MonoBehaviour
{
[DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)]
public static extern float Add(float a, float b);

[DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)]
public static extern bool ToGray(string color_img_file, string gray_img_file);

// Start is called before the first frame update
void Start()
{
Debug.Log(ToGray("/Users/luohanjie/Downloads/lenna_color.png", "/Users/luohanjie/Downloads/lenna_gray.png"));
}

}

并且将脚本PluginImport.cs挂靠到Unity的Main Camera下,并且运行,可以得到的灰度图lenna_gray.png

lenna_gray

通过Unity调用网络摄像头

在Unity的Main Camera下添加一个Raw Image,命名为Background并且设置Tagbackground
Canvas节点中中将Render Mode设置为Screen Space - Camera,并且将Main Camera节点拖拽到Render Camera上。

background1

Background节点(Raw Image)中,将LeftTopPos ZRightBottom都设置为0。为了使得显示的图片比例正确,添加Aspect Ratio Fitter并且Aspect Mode设置为Fit In Parent

background2

Main Camera下添加脚本WebCamera.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;


public class WebCamera : MonoBehaviour
{
public string cam_name = "FaceTime HD Camera";
public int cam_width = 1280;
public int cam_heidht = 720;
public int cam_fps = 30;
public AspectRatioFitter aspect_fitter;
private WebCamTexture web_cam_tex = null;
private bool is_init = false;

// Start is called before the first frame update
void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
for (int i = 0; i < devices.Length; i++)
{
if (devices[i].name == cam_name)
{
is_init = true;
Debug.Log("Connect camera to " + cam_name);
break;
}
}

if (is_init == false) {
Debug.Log("Can not find " + cam_name);
return;
}

web_cam_tex = new WebCamTexture(cam_name, cam_width, cam_heidht, cam_fps);

if (web_cam_tex == null) {
is_init = false;
Debug.Log ("Can not new WebCamTexture");
return;
}

GameObject.FindWithTag("background").GetComponent<RawImage>().texture = web_cam_tex;

web_cam_tex.Play();
}

void OnStart (Texture preview) {
aspect_fitter.aspectRatio = preview.width / (float)preview.height;
}

// Update is called once per frame
void Update()
{

}
}

然后将Background节点拖拽到Main Camera节点中,Web Camera (Script)项目下的Aspect_fitter项目上:

background1

点击运行可以看到结果:

webcam_unity

将c++ OpenCV的Mat传输到Unity并且显示

c++文件unity_api.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#if defined(__CYGWIN32__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#else
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#endif

#include <opencv2/opencv.hpp>

cv::Mat img_;

extern "C" {
UNITY_CPP_INTEROP_DLL_BRIDGE bool ReadImage(const char *color_img_file,
unsigned char*& rgb24_pixels,
int& width,
int& height,
int& size) {

img_ = cv::imread(std::string(color_img_file));
if (img_.empty()) {
return false;
}

// Unity中图片坐标系原点在图片的坐下角,与OpenCV中的定义不一样。
// 为了显示正确,需要flip操作。
cv::flip(img_, img_, 0);
cv::cvtColor(img_, img_, cv::COLOR_BGR2RGB);

rgb24_pixels = img_.data;
width = img_.cols;
height = img_.cols;
size = img_.cols * img_.rows * img_.channels();
return true;
}
}

Unity的C#脚本PluginImport.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;

public class PluginImport : MonoBehaviour
{
[DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)]
public static extern bool ReadImage(string color_img_file, ref IntPtr data, ref int width, ref int height, ref int size);

// Start is called before the first frame update
void Start()
{
IntPtr data = IntPtr.Zero;
int width = 0;
int height = 0;
int size = 0;

bool flag = ReadImage("/Users/luohanjie/Downloads/lenna_color.png", ref data, ref width, ref height, ref size);

if (!flag || size <= 0) return;

byte[] buffer = new byte[size];

Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false, false);

System.Runtime.InteropServices.Marshal.Copy(data, buffer, 0, size);
tex.LoadRawTextureData(buffer);
tex.Apply();

RawImage background = GameObject.FindWithTag("background").GetComponent<RawImage>();
background.texture = tex;
}
}

在Unity的Main Camera下添加一个Raw Image,命名为Background并且设置Tagbackground

将脚本PluginImport.cs挂靠到Unity的Main Camera下,并且运行:

mat2unity

从c++读取Unity物体的Pose

c++文件unity_api.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#if defined(__CYGWIN32__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
#define UNITY_CPP_INTEROP_DLL_BRIDGE __declspec(dllexport) __stdcall
#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__)
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#else
#define UNITY_CPP_INTEROP_DLL_BRIDGE
#endif

extern "C" {
UNITY_CPP_INTEROP_DLL_BRIDGE void GetPose(float *pose_out) {
// 假定我们有一个右手系的pose = [x, y, z, qw, qx, qy, qz],表示物体在摄像头坐标系下(opencv定义)的位姿。
std::array<float, 7> pose;

// 从OpenCV的右手系转到Unity中使用的左手系
pose[1] = -pose[1]; //y
pose[4] = -pose[4]; //qx
pose[6] = -pose[6]; //qz

memcpy(pose_out, pose.data(), 7 * sizeof(float));
}
}

Unity的C#脚本PluginImport.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;

public class PluginImport : MonoBehaviour
{
[DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetPose([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] float[] pose);

// Start is called before the first frame update
void Start()
{
float[] obj_pose = new float[7]; // x, y, z, qw, qx, qy, qz
GetPose(obj_pose);

//方法1,根据读取的Pose设置Unity中物体的姿态
GameObject obj = GameObject.FindWithTag("obj_tag_name");
obj.transform.position = new Vector3((float)obj_pose[0], (float)obj_pose[1], (float)obj_pose[2]);
obj.transform.rotation = new Quaternion((float)obj_pose[4], (float)obj_pose[5], (float)obj_pose[6], (float)obj_pose[3]);

//方法2,根据读取的Pose设置Unity中摄像头的姿态
Camera cam_obj = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
cam_obj.transform.position = new Vector3((float)obj_pose[0], (float)obj_pose[1], (float)obj_pose[2]);
cam_obj.transform.rotation = new Quaternion((float)obj_pose[4], (float)obj_pose[5], (float)obj_pose[6], (float)obj_pose[3]);
}
}

Hardware: MacBook Pro with Apple M1 Max
OS: macOS Monterey 12.0.1

Install Xcode and Homebrew[1]

1
2
3
4
sudo xcodebuild -license
# accept the Xcode license

sudo xcode-select --install

Install Homebrew[2]:

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you would want to add the command brew after the installation is complete:

1
2
3
echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> $HOME/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
source $HOME/.zprofile
1
2
3
4
5
brew update

brew -v
Homebrew 3.3.7
Homebrew/homebrew-core (git revision e1567b5f249; last commit 2021-12-13)

Install Miniforge

Virtual environments are a great way to separate different dependencies if you need different versions for different projects or if you don’t want to change your system path and its dependencies. One of the most popular packages to accomplish this is Anaconda, but anaconda is bloated and doesn’t always have the right packages for m1. Instead, we use miniforge, a community package that provides a minimalistic Anaconda install and uses the conda-forge branch to install packages. Conda-forge provides community packages for Anaconda which has a much larger library of arm64 compatible packages.[3]

1
2
brew install miniforge
conda init zsh

Checking:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
conda info

active environment : None
shell level : 0
user config file : /Users/luohanjie/.condarc
populated config files : /opt/homebrew/Caskroom/miniforge/base/.condarc
/Users/luohanjie/.condarc
conda version : 4.11.0
conda-build version : not installed
python version : 3.9.7.final.0
virtual packages : __osx=12.0.1=0
__unix=0=0
__archspec=1=arm64
base environment : /opt/homebrew/Caskroom/miniforge/base (writable)
conda av data dir : /opt/homebrew/Caskroom/miniforge/base/etc/conda
conda av metadata url : None
channel URLs : https://conda.anaconda.org/conda-forge/osx-arm64
https://conda.anaconda.org/conda-forge/noarch
package cache : /opt/homebrew/Caskroom/miniforge/base/pkgs
/Users/luohanjie/.conda/pkgs
envs directories : /opt/homebrew/Caskroom/miniforge/base/envs
/Users/luohanjie/.conda/envs
platform : osx-arm64
user-agent : conda/4.11.0 requests/2.26.0 CPython/3.9.7 Darwin/21.1.0 OSX/12.0.1
UID:GID : 501:20
netrc file : None
offline mode : False

conda常用命令[4]

  1. conda --version #查看conda版本,验证是否安装
  2. conda update conda #更新至最新版本,也会更新其它相关包
  3. conda update --all #更新所有包
  4. conda update package_name #更新指定的包
  5. conda create -n env_name package_name #创建名为env_name的新环境,并在该环境下安装名为package_name 的包,可以指定新环境的版本号,例如:conda create -n python2 python=python2.7 numpy pandas,创建了python2环境,python版本为2.7,同时还安装了numpy pandas包
  6. source activate env_name #切换至env_name环境
  7. source deactivate #退出环境
  8. conda info -e #显示所有已经创建的环境
  9. conda create --name new_env_name --clone old_env_name #复制old_env_name为new_env_name
  10. conda remove --name env_name –all #删除环境
  11. conda list #查看所有已经安装的包
  12. conda install package_name #在当前环境中安装包
  13. conda install --name env_name package_name #在指定环境中安装包
  14. conda remove – name env_name package #删除指定环境中的包
  15. conda remove package #删除当前环境中的包
  16. conda create -n tensorflow_env tensorflow
  17. conda activate tensorflow_env #conda 安装tensorflow的CPU版本
  18. conda create -n tensorflow_gpuenv tensorflow-gpu
  19. conda activate tensorflow_gpuenv #conda安装tensorflow的GPU版本
  20. conda env remove -n env_name #采用第10条的方法删除环境失败时,可采用这种方法

Disable the automatic activation of the base environment:

1
conda config --set auto_activate_base false

Next we can set up a new virtual environment called cv with python 3.9.

1
2
3
4
5
conda create --name cv python=3.9
conda activate cv

python --version
Python 3.9.7

Install Numpty and TensorFlow[5]

1
2
3
4
conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos -i https://mirrors.aliyun.com/pypi/simple/
python -m pip install tensorflow-metal -i https://mirrors.aliyun.com/pypi/simple/
conda install -y pandas scikit-learn jupyterlab

Check numpy version:

1
python -c "import numpy as np;print(np.__version__);np.show_config()"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1.19.5
blas_info:
libraries = ['cblas', 'blas', 'cblas', 'blas']
library_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib']
include_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/include']
language = c
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
libraries = ['cblas', 'blas', 'cblas', 'blas']
library_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib']
include_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/include']
language = c
lapack_info:
libraries = ['lapack', 'blas', 'lapack', 'blas']
library_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib']
language = f77
lapack_opt_info:
libraries = ['lapack', 'blas', 'lapack', 'blas', 'cblas', 'blas', 'cblas', 'blas']
library_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib']
language = c
define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
include_dirs = ['/opt/homebrew/Caskroom/miniforge/base/envs/cv/include']

NumPy dropped support for using the Accelerate BLAS and LAPACK at version 1.20.0. According to the release notes for NumPy 1.21.1, these bugs have been resolved and building NumPy from source using the Accelerate framework on MacOS >= 11.3 is now possible again. How to build NumPy from source linked to Apple Accelerate framework

Testing the environment[6]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# What version of Python do you have?
import sys

import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf

print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")
1
2
3
4
5
6
7
8
Tensor Flow Version: 2.7.0
Keras Version: 2.7.0

Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:24:02)
[Clang 11.1.0 ]
Pandas 1.3.4
Scikit-Learn 1.0.1
GPU is available

Install FFmpeg

1
2
3
brew install ffmpeg@4

echo 'export PATH="/opt/homebrew/opt/ffmpeg/bin:$PATH"' >> ~/.zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ffmpeg -version

ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 13.0.0 (clang-1300.0.29.3)
configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/4.4.1_3 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-avresample --enable-videotoolbox
libavutil 56. 70.100 / 56. 70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58. 76.100 / 58. 76.100
libavdevice 58. 13.100 / 58. 13.100
libavfilter 7.110.100 / 7.110.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 9.100 / 5. 9.100
libswresample 3. 9.100 / 3. 9.100
libpostproc 55. 9.100 / 55. 9.100
1
2
3
4
5
6
7
8
9
10
11
12
ffmpeg@4 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have ffmpeg@4 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/ffmpeg@4/bin:$PATH"' >> ~/.zshrc

For compilers to find ffmpeg@4 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/ffmpeg@4/lib"
export CPPFLAGS="-I/opt/homebrew/opt/ffmpeg@4/include"

For pkg-config to find ffmpeg@4 you may need to set:
export PKG_CONFIG_PATH="/opt/homebrew/opt/ffmpeg@4/lib/pkgconfig"

Install QT5

1
2
brew install qt@5
echo 'export PATH="/opt/homebrew/opt/qt@5/bin:$PATH"' >> ~/.zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
We agreed to the Qt open source license for you.
If this is unacceptable you should uninstall.

This version of Qt on Apple Silicon does not include QtWebEngine.

qt@5 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have qt@5 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/qt@5/bin:$PATH"' >> ~/.zshrc

For compilers to find qt@5 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/qt@5/lib"
export CPPFLAGS="-I/opt/homebrew/opt/qt@5/include"

For pkg-config to find qt@5 you may need to set:
export PKG_CONFIG_PATH="/opt/homebrew/opt/qt@5/lib/pkgconfig"

Install Gstreamer

1
brew install gstreamer gst-plugins-base

Install VTK

1
2
conda activate cv
conda install vtk

Build OpenCV

1
brew install wget cmake gcc

Download OpenCV source files:

1
2
3
4
5
6
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.4.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.4.zip
unzip opencv.zip
unzip opencv_contrib.zip
cd opencv-4.5.4
mkdir build && cd build

Check PYTHON3_EXECUTABLE location[7]:

1
2
3
4
5
6
conda info -e

# conda environments:
#
base /opt/homebrew/Caskroom/miniforge/base
cv * /opt/homebrew/Caskroom/miniforge/base/envs/cv
1
2
which python3
/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3

so we get:

1
2
3
4
5
-D PYTHON3_LIBRARY=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/libpython3.9.dylib \
-D PYTHON3_INCLUDE_DIR=/opt/homebrew/Caskroom/miniforge/base/envs/cv/include/python3.9 \
-D PYTHON3_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 \
-D PYTHON_DEFAULT_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 \
-D PYTHON3_PACKAGES_PATH=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages \

cmake:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cmake -D CMAKE_SYSTEM_PROCESSOR=arm64 \
-D CMAKE_OSX_ARCHITECTURES=arm64 \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/Users/luohanjie/Softwares/opencv_contrib-4.5.4/modules \
-D PYTHON3_LIBRARY=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/libpython3.9.dylib \
-D PYTHON3_INCLUDE_DIR=/opt/homebrew/Caskroom/miniforge/base/envs/cv/include/python3.9 \
-D PYTHON3_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 \
-D PYTHON_DEFAULT_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 \
-D PYTHON3_PACKAGES_PATH=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=OFF \
-D WITH_OPENJPEG=ON \
-D WITH_IPP=OFF \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_OPENGL=ON \
-D WITH_VTK=ON \
-D VTK_DIR=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/cmake/vtk-9.1 \
-D WITH_QT=ON \
-D Qt5_DIR=$(brew --prefix qt5)/lib/cmake/Qt5 \
-D BUILD_opencv_java=OFF ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
-- 
-- General configuration for OpenCV 4.5.4 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /Users/luohanjie/Softwares/opencv_contrib-4.5.4/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2022-08-30T02:00:42Z
-- Host: Darwin 21.6.0 arm64
-- CMake: 3.23.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: NEON FP16
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ (ver 13.1.6.13160021)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,-dead_strip
-- Linker flags (Debug): -Wl,-dead_strip
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/OpenGL.framework
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: alphamat aruco barcode bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: java world
-- Disabled by dependency: -
-- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf julia matlab ovis python2
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: YES
--
-- GUI: QT5
-- QT: YES (ver 5.15.5 )
-- QT OpenGL support: YES (Qt5::OpenGL 5.15.5)
-- Cocoa: YES
-- OpenGL support: YES (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/OpenGL.framework /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/OpenGL.framework)
-- VTK support: YES (ver 9.1.0)
--
-- Media I/O:
-- ZLib: zlib (ver 1.2.11)
-- JPEG: build-libjpeg-turbo (ver 2.1.0-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: libpng (ver 1.6.37)
-- TIFF: libtiff (ver 42 / 4.2.0)
-- JPEG 2000: build (ver 2.4.0)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: YES
-- avcodec: YES (58.134.100)
-- avformat: YES (58.76.100)
-- avutil: YES (56.70.100)
-- swscale: YES (5.9.100)
-- avresample: YES (4.0.0)
-- GStreamer: YES (1.20.3)
-- AVFoundation: YES
--
-- Parallel framework: GCD
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Lapack: YES (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Accelerate.framework -lm -ldl)
-- Eigen: YES (ver 3.4.0)
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: NO
-- Link libraries: -framework OpenCL
--
-- Python 3:
-- Interpreter: /opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 (ver 3.9.7)
-- Libraries: /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/libpython3.9.dylib (ver 3.9.7)
-- numpy: /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages/numpy/core/include (ver 1.19.5)
-- install path: /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages/cv2/python-3.9
--
-- Python (for build): /opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3
--
-- Java:
-- ant: /opt/homebrew/bin/ant (ver 1.10.12)
-- JNI: /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/include /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/include/darwin /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/include
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/luohanjie/Softwares/opencv-4.5.4/build_mac
1
2
make -j10
sudo make install

如果由于FFMPEG版本问题发生错误(OpenCV需要FFMPEG4版本),则通过下面去解决:

1
2
brew unlink ffmpeg
brew link ffmpeg@4

Sym-link OpenCV 4 on macOS to virtual environment site-packages:

1
2
3
4
mdfind cv2.cpython
/Users/luohanjie/Softwares/opencv-4.5.4/build/lib/python3/cv2.cpython-39-darwin.so
/usr/local/lib/python3.9/site-packages/cv2/python-3.9/cv2.cpython-39-darwin.so
...
1
2
3
4
5
conda info -e
# conda environments:
#
base /opt/homebrew/Caskroom/miniforge/base
cv * /opt/homebrew/Caskroom/miniforge/base/envs/cv
1
2
cd /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages
ln -s /Users/luohanjie/Softwares/opencv-4.5.4/build/lib/python3/cv2.cpython-39-darwin.so cv2.so

And test:

1
2
3
4
conda activate cv
python -c "import cv2;print(cv2.__version__);"

'4.5.4'

OpenGL[8]

1
brew install glew glfw

Oh My Zsh[9] [10]

Check if using zsh:

1
2
echo $SHELL
/bin/zsh

Install Oh My Zsh:

1
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Install the Powerlevel10k Theme:

1
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Set ZSH_THEME="powerlevel10k/powerlevel10k" in ~/.zshrc.

Install Nerd Fonts:

1
2
brew tap homebrew/cask-fonts
brew install --cask font-hack-nerd-font

Open Terminal → Preferences → Profiles → Text, click Change under Font and select Hack family.

Restart Zsh. Type p10k configure if the configuration wizard doesn’t start automatically.

Change settings for vscode

Open Command Pallete (Short Cut: F1), type settings and select Preferences: Open Settings (JSON):

1
"terminal.integrated.fontFamily": "Hack Nerd Font"

Install matplotlib

1
2
conda activate cv
conda install matplotlib

c#

1
2
brew install --cask dotnet-sdk
brew install --cask mono-mdk

Build Android OpenCV

1
brew install openjdk@11 ant

For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
echo ‘export PATH=“/opt/homebrew/opt/openjdk@11/bin:$PATH”’ >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
export CPPFLAGS=“-I/opt/homebrew/opt/openjdk@11/include”

1
2
3
echo 'export PATH="/opt/homebrew/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc
echo 'export JAVA_HOME=/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home' >> ~/.zshrc
source ~/.zshrc
1
2
3
4
5
java -version

openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment Homebrew (build 11.0.15+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.15+0, mixed mode)

Android Gradle 插件版本所需的 Gradle 版本:https://developer.android.google.cn/studio/releases/gradle-plugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cd $OPENCV_SRC
mkdir build
cd build
export ANDROID_HOME=/Users/luohanjie/Library/Android/sdk
cmake -D CMAKE_TOOLCHAIN_FILE="/Users/luohanjie/Library/Android/sdk/ndk/24.0.8215888/build/cmake/android.toolchain.cmake" \
-D ANDROID_NDK="/Users/luohanjie/Library/Android/sdk/ndk/24.0.8215888" \
-D ANDROID_SDK="/Users/luohanjie/Library/Android/sdk" \
-D ANDROID_SDK_TOOLS="/Users/luohanjie/Library/Android/sdk/build-tools/33.0.0" \
-D ANDROID_ABI="arm64-v8a" \
-D ANDROID_STL=c++_shared \
-D ANDROID_NATIVE_API_LEVEL=android-29 \
-D ANDROID_PLATFORM=27 \
-D CMAKE_BUILD_TYPE=Release \
-D ANDROID_ARM_NEON=TRUE \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_ANDROID_EXAMPLES=OFF \
-D BUILD_ANDROID_PROJECTS=ON \
-D INSTALL_ANDROID_EXAMPLES='OFF' \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_JAVA=ON \
-D BUILD_FAT_JAVA_LIB=ON \
-D ANDROID_PROJECTS_BUILD_TYPE='GRADLE' \
-D ANDROID_GRADLE_PLUGIN_VERSION='7.2.1' \
-D GRADLE_VERSION='7.5' \
-D ANT_EXECUTABLE=/opt/homebrew/bin/ant \
-D JAVA_INCLUDE_PATH=/opt/homebrew/opt/openjdk/include \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH="/Users/luohanjie/Softwares/opencv_contrib-4.5.4/modules" ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- General configuration for OpenCV 4.5.4 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /Users/luohanjie/Softwares/opencv_contrib-4.5.4/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2022-07-15T04:50:33Z
-- Host: Darwin 21.5.0 arm64
-- Target: Android 27 aarch64
-- CMake: 3.23.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: NEON FP16
--
-- C/C++:
-- Built as dynamic libs?: NO
-- C++ standard: 11
-- C++ Compiler: /Users/luohanjie/Library/Android/sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ (ver 14.0.1)
-- C++ flags (Release): -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -frtti -stdlib=libc++ -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -frtti -stdlib=libc++ -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -g -fno-limit-debug-info -O0 -DDEBUG -D_DEBUG
-- C Compiler: /Users/luohanjie/Library/Android/sdk/ndk/24.0.8215888/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
-- C flags (Release): -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -g -fno-limit-debug-info -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Qunused-arguments -Wl,--no-undefined -Wl,--as-needed
-- Linker flags (Debug): -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Qunused-arguments -Wl,--no-undefined -Wl,--as-needed
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: z dl m log
-- 3rdparty dependencies: libcpufeatures libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf quirc tegra_hal
--
-- OpenCV modules:
-- To be built: aruco barcode bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform java line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: alphamat cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf julia matlab ovis python2 python3 sfm ts viz
-- Applications: -
-- Documentation: NO
-- Non-free algorithms: YES
--
-- Android NDK: /Users/luohanjie/Library/Android/sdk/ndk/24.0.8215888 (ver 24.0.8215888)
-- Android ABI: arm64-v8a
-- STL type: c++_shared
-- Native API level: 27
-- Android SDK: /Users/luohanjie/Library/Android/sdk (tools: 33.0.0 build tools: 33.0.0)
--
-- GUI: NONE
--
-- Media I/O:
-- ZLib: z (ver 1.2.11)
-- JPEG: build-libjpeg-turbo (ver 2.1.0-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: build (ver 1.6.37)
-- TIFF: build (ver 42 - 4.2.0)
-- JPEG 2000: build (ver 2.4.0)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- Python (for build): /opt/homebrew/bin/python3
--
-- Java: export all functions
-- ant: /opt/homebrew/bin/ant (ver )
-- Java wrappers: YES
-- Java tests: NO
--
-- Install to: /Users/luohanjie/Softwares/opencv-4.5.4/build_android/install
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/luohanjie/Softwares/opencv-4.5.4/build_android
1
2
make -j20
make install

Eigen

1
brew install eigen

安装完成后,eigen文件夹位于/opt/homebrew/Cellar/eigen/3.4.0_1文件夹内,并非系统文件夹。

1
2
3
4
brew link --overwrite eigen

To relink, run:
brew unlink eigen && brew link eigen

运行后,会在 系统文件夹/usr/local/include文件夹下创建一个文件夹链接(名为eigen3)指向上面的真实eigen文件夹[11]

Python

1
2
3
4
5
6
7
8
9
10
11
which python3
/opt/homebrew/bin/python3

which python
python not found

echo "alias python=python3" >> ~/.zshrc
source ~/.zshrc

which python
python: aliased to python3

Build OpenCV 4.6.0 with FFmpeg 5.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
brew link zlib
brew upgrade ffmpeg
ffmpeg -version

ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 14.0.0 (clang-1400.0.29.102)
configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/5.1.2 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-neon
libavutil 57. 28.100 / 57. 28.100
libavcodec 59. 37.100 / 59. 37.100
libavformat 59. 27.100 / 59. 27.100
libavdevice 59. 7.100 / 59. 7.100
libavfilter 8. 44.100 / 8. 44.100
libswscale 6. 7.100 / 6. 7.100
libswresample 4. 7.100 / 4. 7.100
libpostproc 56. 6.100 / 56. 6.100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cmake -D CMAKE_SYSTEM_PROCESSOR=arm64 \
-D CMAKE_OSX_ARCHITECTURES=arm64 \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/Users/luohanjie/Softwares/opencv_contrib/modules \
-D PYTHON3_LIBRARY=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/libpython3.9.dylib \
-D PYTHON3_INCLUDE_DIR=/opt/homebrew/Caskroom/miniforge/base/envs/cv/include/python3.9 \
-D PYTHON3_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 \
-D PYTHON_DEFAULT_EXECUTABLE=/opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 \
-D PYTHON3_PACKAGES_PATH=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_EXAMPLES=OFF \
-D WITH_OPENJPEG=ON \
-D WITH_IPP=OFF \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_OPENGL=ON \
-D WITH_VTK=ON \
-D VTK_DIR=/opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/cmake/vtk-9.1 \
-D WITH_QT=ON \
-D Qt5_DIR=$(brew --prefix qt5)/lib/cmake/Qt5 \
-D BUILD_opencv_java=OFF \
-D BUILD_ZLIB=OFF ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- General configuration for OpenCV 4.6.0 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /Users/luohanjie/Softwares/opencv_contrib/modules
-- Version control (extra): 9d84eae
--
-- Platform:
-- Timestamp: 2022-11-02T10:18:17Z
-- Host: Darwin 21.6.0 arm64
-- CMake: 3.24.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: NEON FP16
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ (ver 14.0.0.14000029)
-- C++ flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,-dead_strip
-- Linker flags (Debug): -Wl,-dead_strip
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies:
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: java world
-- Disabled by dependency: -
-- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev hdf julia matlab ovis python2
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: YES
--
-- GUI: QT5
-- QT: YES (ver 5.15.5 )
-- QT OpenGL support: YES (Qt5::OpenGL 5.15.5)
-- Cocoa: YES
-- OpenGL support: YES (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/OpenGL.framework /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/OpenGL.framework)
-- VTK support: YES (ver 9.1.0)
--
-- Media I/O:
-- ZLib: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/lib/libz.tbd (ver 1.2.11)
-- JPEG: build-libjpeg-turbo (ver 2.1.2-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: libpng (ver 1.6.37)
-- TIFF: libtiff (ver 42 / 4.2.0)
-- JPEG 2000: build (ver 2.4.0)
-- OpenEXR: OpenEXR::OpenEXR (ver 3.1.5)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: YES
-- avcodec: YES (59.37.100)
-- avformat: YES (59.27.100)
-- avutil: YES (57.28.100)
-- swscale: YES (6.7.100)
-- avresample: NO
-- GStreamer: YES (1.20.3)
-- AVFoundation: YES
--
-- Parallel framework: GCD
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Lapack: YES (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/Accelerate.framework -lm -ldl)
-- Eigen: YES (ver 3.4.0)
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.19.1)
--
-- OpenCL: YES (no extra features)
-- Include path: NO
-- Link libraries: -framework OpenCL
--
-- Python 3:
-- Interpreter: /opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3 (ver 3.9.13)
-- Libraries: /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/libpython3.9.dylib (ver 3.9.13)
-- numpy: /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages/numpy/core/include (ver 1.19.5)
-- install path: /opt/homebrew/Caskroom/miniforge/base/envs/cv/lib/python3.9/site-packages/cv2/python-3.9
--
-- Python (for build): /opt/homebrew/Caskroom/miniforge/base/envs/cv/bin/python3
--
-- Java:
-- ant: /opt/homebrew/bin/ant (ver 1.10.12)
-- JNI: /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/include /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/include/darwin /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/include
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/luohanjie/Softwares/opencv-4.6.0/build_mac
1
2
make j-20
sudo make install

注意opencv_contrib4.6.0tag版本可能会存在ceres问题,改用主分支解决。
BUILD_ZLIB可能会存在问题-D BUILD_ZLIB=OFF解决。
FFmpeg 5.1导致videoio问题,在modules/videoio/src/ffmpeg_codecs.hpp中添加#include <libavcodec/version.h>解决。

pytorch

1
2
conda activate cv
conda install pytorch torchvision torchaudio -c pytorch
1
2
3
4
5
6
7
8
9
10
11
python
Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 17:00:33)
[Clang 13.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import math
>>> print(torch.backends.mps.is_available())
True
>>> print(torch.backends.mps.is_built())
True
>>>

To run PyTorch code on the GPU, use torch.device(“mps”) analogous to torch.device(“cuda”) on an Nvidia GPU. Hence, in this example, we move all computations to the GPU[12]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
dtype = torch.float
device = torch.device("mps")

# Create random input and output data
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)

# Randomly initialize weights
a = torch.randn((), device=device, dtype=dtype)
b = torch.randn((), device=device, dtype=dtype)
c = torch.randn((), device=device, dtype=dtype)
d = torch.randn((), device=device, dtype=dtype)

learning_rate = 1e-6
for t in range(2000):
# Forward pass: compute predicted y
y_pred = a + b * x + c * x ** 2 + d * x ** 3

# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)

# Backprop to compute gradients of a, b, c, d with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_a = grad_y_pred.sum()
grad_b = (grad_y_pred * x).sum()
grad_c = (grad_y_pred * x ** 2).sum()
grad_d = (grad_y_pred * x ** 3).sum()

# Update weights using gradient descent
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d


print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')
1
Result: y = 0.9504813551902771 + 1.0364974737167358 x + -1.9110007286071777 x^2 + 0.7653123736381531 x^3

  1. https://sayak.dev/install-opencv-m1/ ↩︎

  2. https://brew.sh/ ↩︎

  3. https://blog.roboflow.com/m1-opencv/ ↩︎

  4. https://v3u.cn/a_id_189 ↩︎

  5. https://blog.51cto.com/nxlhero/4022651 ↩︎

  6. https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/tensorflow-install-mac-metal-jul-2021.ipynb ↩︎

  7. https://jayrobwilliams.com/files/html/OpenCV_Install.html ↩︎

  8. https://f5soft.site/zh/notes/2021/0310/ ↩︎

  9. https://www.ioiox.com/archives/34.html ↩︎

  10. https://github.com/romkatv/powerlevel10k ↩︎

  11. https://zhuanlan.zhihu.com/p/133653348 ↩︎

  12. https://towardsdatascience.com/installing-pytorch-on-apple-m1-chip-with-gpu-acceleration-3351dc44d67c ↩︎