這篇文章是在OSX上測試和運行的的, Ubuntu下的安裝和配置請移步到這裏
應用程序進程樹, 默認 Poolboy 中初始化10個用於處理圖片的 Python 工作進程(Worker)
1
2
3
4
5
6
|
ruby
-
e
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew
install
python
brew
tap
homebrew
/
science
brew
install
opencv
sudo
pip
install
numpy
sudo
pip
install
matplotlib
|
使用 Homebrew 的 Python 版本, 而不是 Mac OS X 系統自帶的 Python
1
|
alias
python
=
'/usr/local/bin/python'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
➜
mix
new
opencv_thumbnail_server
--
sup
*
creating
README
.
md
*
creating
.
gitignore
*
creating
mix
.
exs
*
creating
config
*
creating
config
/
config
.
exs
*
creating
lib
*
creating
lib
/
opencv_thumbnail_server
.
ex
*
creating
test
*
creating
test
/
test_helper
.
exs
*
creating
test
/
opencv_thumbnail_server_test
.
exs
Your
Mix
project
was
created
successfully
.
You
can
use
"mix"
to
compile
it
,
test
it
,
and
more
:
cd
opencv_thumbnail_server
mix
test
Run
"mix help"
for
more
commands
.
|
Elixir 模塊
1
2
3
4
5
6
7
8
|
require
Logger
defmodule
OpencvThumbnailServer
do
use
Application
def
start
(
_type
,
_args
)
do
Logger
.
info
"Start opencv thumbnail server"
OpencvThumbnailServer
.
Supervisor
.
start_link
(
)
end
end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
defmodule
OpencvThumbnailServer
.
Supervisor
do
use
Supervisor
<
a
href
=
"http://www.jobbole.com/members/chkconfig"
>
@
config
<
/
a
>
Application
.
get_env
:
opencv_thumbnail_server
,
:
settings
def
start_link
(
)
do
Supervisor
.
start_link
(
__MODULE__
,
[
]
,
name
:
{
:
global
,
__MODULE__
}
)
end
def
init
(
[
]
)
do
pool_options
=
@
config
[
:
poolboy
]
{
_
,
name
}
=
pool_options
[
:
name
]
children
=
[
:
poolboy
.
child_spec
(
name
,
pool_options
,
@
config
[
:
module_name
]
)
]
supervise
(
children
,
strategy
:
:
one_for_all
,
max_restarts
:
1000
,
max_seconds
:
3600
)
end
end
|
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
|
defmodule
OpencvThumbnailServer
.
Worker
do
use
GenServer
<
a
href
=
"http://www.jobbole.com/members/chkconfig"
>
@
config
<
/
a
>
Application
.
get_env
(
:
opencv_thumbnail_server
,
:
settings
)
def
start_link
(
python_module
)
do
GenServer
.
start_link
(
__MODULE__
,
python_module
,
[
]
)
end
def
call_python
(
worker
,
function
,
args
)
do
GenServer
.
call
(
worker
,
{
:
call_python
,
function
,
args
}
,
10_000
)
end
def
init
(
python_module
)
do
IO
.
puts
"Start worker"
{
:
ok
,
pid
}
=
:
python
.
start_link
(
[
{
:
python_path
,
@
config
[
:
python_path
]
}
,
{
:
python
,
@
config
[
:
python
]
}
]
)
state
=
{
python_module
,
pid
}
{
:
ok
,
state
}
end
def
handle_call
(
{
:
call_python
,
function
,
args
}
,
_from
,
state
)
do
{
module
,
pid
}
=
state
result
=
:
python
.
call
(
pid
,
module
,
function
,
args
)
reply
=
{
:
ok
,
result
}
{
:
reply
,
reply
,
state
}
end
def
handle_call
(
_request
,
_from
,
state
)
do
{
:
stop
,
:
error
,
:
bad_call
,
state
}
end
def
handle_info
(
_msg
,
{
module
,
py_pid
}
)
do
{
:
stop
,
:
error
,
{
module
,
py_pid
}
}
end
def
terminate
(
_reason
,
{
_
,
py_pid
}
)
do
:
python
.
stop
(
py_pid
)
:
ok
end
end
|
獲取寬高
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
|
# -*- coding: utf-8 -*-
import
urllib2
as
urllib
import
numpy
as
np
import
cv2
def
load_image_url
(
url
)
:
resp
=
urllib
.
urlopen
(
url
)
buf
=
resp
.
read
(
)
return
buf
def
load_image_file
(
filename
)
:
image
=
cv2
.
imdecode
(
filename
,
cv2
.
IMREAD_COLOR
)
return
image
def
get_photo_sizes
(
)
:
return
[
[
160
,
160
]
,
[
320
,
320
]
,
[
640
,
640
]
,
[
1060
,
1060
]
,
[
1280
,
1280
]
]
def
show
(
buf
)
:
# print buf
# x = cv2.imdecode(image, cv2.IMREAD_COLOR)
# d = cv2.cvtColor(c, cv2.COLOR_RGB2BGR)
np_ndarray
=
np
.
fromstring
(
buf
,
dtype
=
np
.
uint8
)
x
=
cv2
.
imdecode
(
np_ndarray
,
cv2
.
IMREAD_UNCHANGED
)
return
cv2
.
imshow
(
'NBA Image'
,
x
)
def
write
(
buf
)
:
nparray
=
np
.
fromstring
(
buf
,
dtype
=
np
.
uint8
)
img
=
cv2
.
imdecode
(
nparray
,
cv2
.
IMREAD_UNCHANGED
)
return
cv2
.
imwrite
(
'/tmp/imwrite.png'
,
img
)
# def get_dimension():
# url = 'http://img1.gtimg.com/16/1601/160106/16010642_1200x1000_0.jpg'
# resp = urllib.urlopen(url)
# buf = resp.read()
# x = np.fromstring(buf, dtype=np.uint8)
# img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
# # height = np.size(img, 0)
# # width = np.size(img, 1)
# height, width = image.shape[:2]
# return (width, height)
def
get_dimension
(
buffer
)
:
# 把原始的二進制圖片數據轉換爲NpArray
nparray
=
np
.
fromstring
(
buffer
,
dtype
=
np
.
uint8
)
# 把 nparray 轉換爲 opencv 的圖像格式
image
=
cv2
.
imdecode
(
nparray
,
cv2
.
IMREAD_UNCHANGED
)
height
,
width
=
image
.
shape
[
:
2
]
return
(
width
,
height
)
def
convert_color
(
)
:
url
=
'http://ww3.sinaimg.cn/mw690/6941baebgw1epzcuv9vmxj20me0hy0u1.jpg'
resp
=
urllib
.
urlopen
(
url
)
buf
=
resp
.
read
(
)
x
=
np
.
fromstring
(
buf
,
dtype
=
np
.
uint8
)
img
=
cv2
.
imdecode
(
x
,
cv2
.
IMREAD_UNCHANGED
)
if
__name__
==
'__main__'
:
get_dimension
(
)
|
Erlang 的binary()
數據類型和 Python 之間的映射關係, 在Python 2.x 中二進制數據類型爲 str()
表示, Python 3.x 中爲 bytes()
buf = resp.read()
, 其中變量 buf
的類型爲
在 Elixir 我們看的如下的值
1
2
3
|
{
:
ok
,
<<
255
,
216
,
255
,
224
,
0
,
16
,
74
,
70
,
:
ok
,
<<
255
,
216
,
255
,
224
,
0
,
16
,
74
,
70
,
73
,
70
,
0
,
1
,
216
,
255
,
224
,
0
,
16
,
74
,
70
,
73
,
70
,
0
,
1
,
1
,
224
,
|