ruby - faceplusplus 识别身份证, ocr, id card, faceid,
访问量: 1036
供以后参考。
值得一提的是,就使用curl 来提交文件即可。不要使用 restclient 等ruby第三方包。因为发现这些都对post 请求支持的不好。对添加附件支持的不好。
代码如下:
def check_id_card options id_card_approval = options[:id_card_approval] type = options[:type] id_card_full_path = id_card_approval.send type url = 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard' api_key = 'xr4IiPXr8K9wxd1UeV42HmhAppO8QGis' api_secret = 'yA6bf25mtdFPdyrbamLmrVB0yiGrL1Yg' id_card_full_path = id_card_full_path.gsub('/public', 'public') command = %Q{curl -X POST "#{url}" \ -F "api_key=#{api_key}" \ -F "api_secret=#{api_secret}" \ -F "image_file=@#{id_card_full_path}"} $logger.info "== command: #{command}" response = `#{command}` $logger.debug "---- response: #{response}" if response.blank? || JSON.parse(response)['cards'].blank? $logger.info "== invalid card: |#{response}|" # 这里用的是元编程....好吧,注释比代码多. # front, back, man id_card_approval.send "#{type}_checked_at=", Time.now id_card_approval.save! return end