finally got image ocr works

This commit is contained in:
2016-02-29 23:39:03 +08:00
parent cadd884b81
commit 8ea2dd75fd
4 changed files with 47 additions and 0 deletions
+1
View File
@@ -82,6 +82,7 @@ gem 'font-awesome-rails', '~> 4.4'
gem "sentry-raven" #, :github => "getsentry/raven-ruby"
gem "paranoia", "~> 2.0"
gem 'faraday'
group :development do
gem 'capistrano'
+1
View File
@@ -421,6 +421,7 @@ DEPENDENCIES
coffee-rails (~> 4.1.0)
devise
devise-bootstrap-views
faraday
font-awesome-rails (~> 4.4)
groupdate
houston
+44
View File
@@ -0,0 +1,44 @@
class Api::V1::OcrController < ApplicationController
skip_before_action :authenticate_user_from_token
BAIDU_KEY = 'a16b7587834498e95bef733ecbfa155d'
API_URL = 'http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid'
def index
image_base_str = Base64.strict_encode64(IO.read('/Users/twer/Desktop/1.jpg'))
# conn = Faraday.new(:url => API_URL) do |faraday|
# faraday.request :url_encoded
# faraday.request :multipart
# faraday.response :logger
# faraday.adapter Faraday.default_adapter
# end
# response = conn.post do |req|
# req.url '/idl_baidu/baiduocrpay/idlocrpaid'
# req.headers['apikey'] = BAIDU_KEY
# req.body = {fromdevice: 'pc',
# clientip: '10.10.10.0',
# detecttype: 'LocateRecognize',
# languagetype: 'CHN_ENG',
# imagetype: '2',
# # image: 'http://apistore.baidu.com/idlapi/img_demo_data/ocr/LocateRecognize//62.jpg'
# image: Faraday::UploadIO.new('/Users/twer/Desktop/1.jpg', 'image/jpeg')
# }
# end
url = 'http://apistore.baidu.com/idlapi/img_demo_data/ocr/LocateRecognize//62.jpg'
response = RestClient::Request.execute(method: :post, url: API_URL,
timeout: 10,
headers: {apikey: BAIDU_KEY},
payload:
{fromdevice: 'pc',
clientip: '127.0.0.1',
detecttype: 'LocateRecognize',
languagetype: 'CHN_ENG',
imagetype: '1',
image: image_base_str
})
json = JSON.parse(response.body)
ap json
render text: json
end
end
+1
View File
@@ -124,6 +124,7 @@ Rails.application.routes.draw do
get :most_loved_posts_in_month
end
get '/ocr', to: 'ocr#index'
end
end
end