摘要:這篇文章是在上測(cè)試和運(yùn)行的的下的安裝和配置請(qǐng)移步到這里應(yīng)用程序進(jìn)程樹默認(rèn)啟動(dòng)時(shí)初始化個(gè)用于處理圖片的工作進(jìn)程首先安裝需要的工具包使用的版本而不是系統(tǒng)自帶的創(chuàng)建項(xiàng)目模塊圖像處理獲取寬高把原始的二進(jìn)制圖片數(shù)據(jù)轉(zhuǎn)換為把轉(zhuǎn)換為的圖
這篇文章是在OSX上測(cè)試和運(yùn)行的的, Ubuntu下的安裝和配置請(qǐng)移步到這里
應(yīng)用程序進(jìn)程樹, 默認(rèn)啟動(dòng) Poolboy 時(shí), 初始化10個(gè)用于處理圖片的 Python 工作進(jìn)程(Worker)
首先安裝OpenCV需要的工具包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 系統(tǒng)自帶的 Python
alias python="/usr/local/bin/python"創(chuàng)建 Elixir 項(xiàng)目
? 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 模塊
require Logger defmodule OpencvThumbnailServer do use Application def start(_type, _args) do Logger.info "Start opencv thumbnail server" OpencvThumbnailServer.Supervisor.start_link() end end
defmodule OpencvThumbnailServer.Supervisor do use Supervisor @config 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
defmodule OpencvThumbnailServer.Worker do use GenServer @config 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圖像處理
獲取寬高
# -*- 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): # 把原始的二進(jìn)制圖片數(shù)據(jù)轉(zhuǎn)換為NpArray nparray = np.fromstring(buffer, dtype=np.uint8) # 把 nparray 轉(zhuǎn)換為 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 和 Python 之間傳輸二進(jìn)制數(shù)據(jù)
Erlang 的binary()數(shù)據(jù)類型和 Python 之間的映射關(guān)系, 在Python 2.x 中二進(jìn)制數(shù)據(jù)類型為 str() 表示, Python 3.x 中為 bytes()
buf = resp.read(), 其中變量 buf 的類型為
在 Elixir 我們看到變量 buf 的值為:
{:ok, <<255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, 0, 72, 0, 0, 255, 219, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, ...>>}調(diào)用 Python 函數(shù)
{:ok, data} = OpencvThumbnailServer.Api.load_image_url("https://segmentfault.com/img/bVwhAW") OpencvThumbnailServer.Api.get_dimension(data) {:ok, {800, 431}}創(chuàng)建 Python 模塊
之前的 Python 圖像處理模塊可以組織到一個(gè)項(xiàng)目中多帶帶維護(hù). 這里使用工具 cookiecutter 創(chuàng)建 Python 一個(gè)基本的項(xiàng)目骨架, 用于實(shí)現(xiàn)縮略圖的功能
cookiecutter 可以通過多種方式安裝, 包括pip, easy_install, conda, brew
pip install cookiecutter easy_install cookiecutter conda install -c https://conda.binstar.org/pydanny cookiecutter brew install cookiecutter(Mac OS X)
目錄結(jié)構(gòu)
? opencv_thumbnail git:(master) tree . ├── AUTHORS.rst ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── build │?? ├── bdist.macosx-10.11-x86_64 │?? └── lib │?? └── opencv_thumbnail │?? ├── __init__.py │?? └── opencv_thumbnail.py ├── dist │?? └── opencv_thumbnail-0.1.0-py2.7.egg ├── docs │?? ├── Makefile │?? ├── authors.rst │?? ├── conf.py │?? ├── contributing.rst │?? ├── history.rst │?? ├── index.rst │?? ├── installation.rst │?? ├── make.bat │?? ├── readme.rst │?? └── usage.rst ├── opencv_thumbnail │?? ├── __init__.py │?? ├── __init__.pyc │?? ├── opencv_thumbnail.py │?? └── opencv_thumbnail.pyc ├── opencv_thumbnail.egg-info │?? ├── PKG-INFO │?? ├── SOURCES.txt │?? ├── dependency_links.txt │?? ├── not-zip-safe │?? └── top_level.txt ├── requirements_dev.txt ├── setup.cfg ├── setup.py ├── tests │?? ├── __init__.py │?? └── test_opencv_thumbnail.py ├── tox.ini └── travis_pypi_setup.py 9 directories, 36 filesAPI實(shí)現(xiàn)
調(diào)用需要從 Poolboy 池中取出一個(gè)工作進(jìn)程, 并調(diào)用工作進(jìn)程的call_python, 進(jìn)程使用完成后返還給 Poolboy 進(jìn)程池, 這里對(duì)調(diào)用過程封裝一下, 以簡(jiǎn)化使用.
defmodule OpencvThumbnailServer.Api do alias OpencvThumbnailServer.Worker def get_dimension(data) do worker = :poolboy.checkout(:opencv_thumbnail_server_pool) {w, h} = Worker.call_python(worker, :get_dimension, [data]) :poolboy.checkin(:opencv_thumbnail_server_pool, worker) {w, h} end def load_image_url(url) do worker = :poolboy.checkout(:opencv_thumbnail_server_pool) image_bin = Worker.call_python(worker, :load_image_url, [url]) :poolboy.checkin(:opencv_thumbnail_server_pool, worker) image_bin end end源碼
https://github.com/developerworks/opencv_thumbnail_server
參考資料利用Python和OpenCV將URL直接轉(zhuǎn)換成OpenCV格式
How to read raw png from an array in python opencv?
Install OpenCV for Python on Mac OS X
Installing scikit-image
How can i read an image from an internet url in python cv2 , scikit image and mahotas
Using Elixir, erlport with Python 2.7.9, receiving an arity error
How to read image from in memory buffer (StringIO) or from url with opencv python library
Python OpenCV convert image to byte string?
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/37987.html
摘要:圖片中有人臉,將自動(dòng)視為人臉區(qū)域?yàn)橹匾獏^(qū)域,將不會(huì)被裁剪掉。自動(dòng)識(shí)別其它重要區(qū)域。如果圖片中未識(shí)別出人臉,則會(huì)根據(jù)特征分布計(jì)算出重區(qū)域。源碼地址安裝根據(jù)上的說明,在上安裝沒有問題,但在我的上就掛了。首先遇到的問題就是,安裝不過去。 Tclip 用于圖片裁剪,有以下特點(diǎn): 能進(jìn)行人臉識(shí)別。圖片中有人臉,將自動(dòng)視為人臉區(qū)域?yàn)橹匾獏^(qū)域,將不會(huì)被裁剪掉。 自動(dòng)識(shí)別其它重要區(qū)域。如果圖片中...
摘要:圖像指紋與漢明距離在介紹下面其他判別相似度的方法前,先補(bǔ)充一些概念。漢明距離為,即代表兩張圖片完全一樣。下一次將講述利用和以訓(xùn)練好的模型來進(jìn)行人臉識(shí)別。本文參考文章和圖片來源的文章賴勇浩的文章下一篇地址利用進(jìn)行識(shí)別相似圖片二 文章簡(jiǎn)介 在網(wǎng)上看到python做圖像識(shí)別的相關(guān)文章后,真心感覺python的功能實(shí)在太強(qiáng)大,因此將這些文章總結(jié)一下,建立一下自己的知識(shí)體系。當(dāng)然了,圖像識(shí)別這個(gè)...
閱讀 1716·2023-04-26 01:02
閱讀 4880·2021-11-24 09:39
閱讀 1815·2019-08-30 15:44
閱讀 2900·2019-08-30 11:10
閱讀 1795·2019-08-30 10:49
閱讀 993·2019-08-29 17:06
閱讀 619·2019-08-29 16:15
閱讀 910·2019-08-29 15:17