iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 8
1
Modern Web

續說 Ruby on Rails系列 第 9

[Day 9] 來做圖像辨識吧 OcrSpace gem

有時候我們在做網站爬蟲,遇到登入網站會員需要填入識別碼,有時候又是數字又是英文,你可以裝 tesseract 直接在command line使用,或用圖像辨識工具 OcrSpace 免費版一天可以使用500次

來進入今天的主題~~

申請帳號

首先你要先去 OcrSpace官網
註冊頁申請一個帳號

然後到剛才註冊的信箱收信

就可以拿到API key了

ocr_space gem

有好新人幫我們把語法包裝成gem了,先安裝:

gem 'ocr_space'

別忘記 bundle 喔

在想用OcrSpace的檔案裡面

實作

require 'ocr_space'

resource = OcrSpace::Resource.new(apikey: "YOUR API KEY")

如果今天要解析這張圖

圖片來自url

result = resource.clean_convert url: "https://i.imgur.com/hbDvSlo.png"

puts result

=> #If you want to find the secrets of the universe, think in terms of energy, frequency and vibration. AZ QUOTES

圖片來自檔案

result = resource.clean_convert file: "/Users/suyesh/Desktop/nicola_tesla.jpg"

puts result

=> #If you want to find the secrets of the universe, think in terms of energy, frequency and vibration. AZ QUOTES

加上option

翻看source code可以看到 #clean_convert 可以加上兩個option
languageisOverlayRequired

clean_convert(apikey: @api_key, language: 'eng', isOverlayRequired: false, file: nil, url: nil)

這兩個option介紹如下

isOverlayRequired:

#[Optional] 預設為false,選擇是否圖片上覆蓋文字
Allows you to specify if the image/pdf text overlay is required. Overlay could be #used to show the text over the image

language:

#Czech = ce; Danish = dan; Dutch = dut; English = eng; Finnish = fin; French = fre; German = ger;Hungarian=hun;
#Italian = ita; Norwegian = nor; Polish = pol; Portuguese = por; Spanish = spa; Swedish = swe; #ChineseSimplified = chs; Greek = gre; Japanese = jpn; Russian = rus; Turkish = tur; ChineseTraditional = cht; #Korean = kor


上一篇
[Day 8] Rails TimeWithZone 跟 Date 比較問題
下一篇
[Day 10] 網頁爬蟲 Selenium
系列文
續說 Ruby on Rails10
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言