Skip to content

Instantly share code, notes, and snippets.

@mimosa
Created October 14, 2015 10:52
Show Gist options
  • Save mimosa/2c33cc9875ff70749fa4 to your computer and use it in GitHub Desktop.
Save mimosa/2c33cc9875ff70749fa4 to your computer and use it in GitHub Desktop.
if defined? ::Typhoeus
  Faraday.default_adapter = :typhoeus
end

@facexx = Facexx.new( '9428ebfb8effb87c9f1fa43998683ca4',  'FP_7BIg7V93Fhr7jwiDqVzA93skdQrJX')

data = @facexx.get('http://7xng7n.com1.z0.glb.clouddn.com/2.pic_hd.jpg/face.jpg')
  • face++ 中的 x,y 轴均是相对坐标,使用的时候需要 x*img_width/100, y*img_height/100
  • 识别时使用小尺寸、低质量图片
  • http://7xng7n.com1.z0.glb.clouddn.com/2.pic_hd.jpg/hd.jpg 为用户使用类原图
  • 测试将返回数据坐标,标注到原图,看看是否因为 float 产生坐标偏移问题。
{
"face": [
{
"attribute": {
"age": {
"range": 5,
"value": 31
},
"gender": {
"confidence": 99.9992,
"value": "Male"
},
"glass": {
"confidence": 99.9563,
"value": "None"
},
"pose": {
"pitch_angle": {
"value": 0.000022
},
"roll_angle": {
"value": -1.60319
},
"yaw_angle": {
"value": 5.634009
}
},
"race": {
"confidence": 78.8328,
"value": "Asian"
},
"smiling": {
"value": 0.925485
}
},
"face_id": "9f0bfa045bebc2cb2a788d2492363bdf",
"position": {
"center": {
"x": 55.60472,
"y": 48.916667
},
"eye_left": {
"x": 40.717994,
"y": 41.189833
},
"eye_right": {
"x": 70.450442,
"y": 40.719667
},
"height": 41.166667,
"mouth_left": {
"x": 43.867552,
"y": 58.410167
},
"mouth_right": {
"x": 69.533333,
"y": 58.225833
},
"nose": {
"x": 59.873451,
"y": 48.55
},
"width": 72.861357
},
"tag": "",
"result": [
{
"face_id": "9f0bfa045bebc2cb2a788d2492363bdf",
"landmark": {
"contour_chin": {
"x": 58.793525,
"y": 70.298333
},
"contour_left1": {
"x": 20.416542,
"y": 42.027167
},
"contour_left2": {
"x": 20.43794,
"y": 46.124
},
"contour_left3": {
"x": 20.749845,
"y": 50.3425
},
"contour_left4": {
"x": 21.819102,
"y": 54.602667
},
"contour_left5": {
"x": 23.702311,
"y": 58.824167
},
"contour_left6": {
"x": 27.903812,
"y": 62.5185
},
"contour_left7": {
"x": 33.639735,
"y": 65.771
},
"contour_left8": {
"x": 41.068727,
"y": 67.909667
},
"contour_left9": {
"x": 49.712436,
"y": 69.941833
},
"contour_right1": {
"x": 85.954084,
"y": 41.0955
},
"contour_right2": {
"x": 85.440177,
"y": 45.029833
},
"contour_right3": {
"x": 85.126122,
"y": 49.118667
},
"contour_right4": {
"x": 84.485357,
"y": 52.949833
},
"contour_right5": {
"x": 82.888006,
"y": 57.097
},
"contour_right6": {
"x": 80.089772,
"y": 60.8275
},
"contour_right7": {
"x": 76.564827,
"y": 63.824833
},
"contour_right8": {
"x": 71.919647,
"y": 66.3965
},
"contour_right9": {
"x": 66.328182,
"y": 69.02
},
"left_eye_bottom": {
"x": 40.794113,
"y": 42.593167
},
"left_eye_center": {
"x": 40.802355,
"y": 41.405167
},
"left_eye_left_corner": {
"x": 33.951729,
"y": 41.75
},
"left_eye_lower_left_quarter": {
"x": 37.191464,
"y": 42.1355
},
"left_eye_lower_right_quarter": {
"x": 44.090655,
"y": 42.2495
},
"left_eye_pupil": {
"x": 41.06078,
"y": 41.281167
},
"left_eye_right_corner": {
"x": 47.518175,
"y": 41.5025
},
"left_eye_top": {
"x": 40.989845,
"y": 40.267
},
"left_eye_upper_left_quarter": {
"x": 37.588521,
"y": 40.751167
},
"left_eye_upper_right_quarter": {
"x": 44.159235,
"y": 40.435
},
"left_eyebrow_left_corner": {
"x": 26.783723,
"y": 37.842667
},
"left_eyebrow_lower_left_quarter": {
"x": 31.84106,
"y": 36.352333
},
"left_eyebrow_lower_middle": {
"x": 37.98028,
"y": 35.731667
},
"left_eyebrow_lower_right_quarter": {
"x": 44.100074,
"y": 35.472333
},
"left_eyebrow_right_corner": {
"x": 49.766593,
"y": 35.231167
},
"left_eyebrow_upper_left_quarter": {
"x": 31.10699,
"y": 34.3665
},
"left_eyebrow_upper_middle": {
"x": 37.21089,
"y": 32.992333
},
"left_eyebrow_upper_right_quarter": {
"x": 43.789257,
"y": 32.908167
},
"mouth_left_corner": {
"x": 44.643414,
"y": 58.764167
},
"mouth_lower_lip_bottom": {
"x": 58.788521,
"y": 60.525
},
"mouth_lower_lip_left_contour1": {
"x": 52.388227,
"y": 58.541167
},
"mouth_lower_lip_left_contour2": {
"x": 47.98911,
"y": 59.645667
},
"mouth_lower_lip_left_contour3": {
"x": 53.25298,
"y": 60.442
},
"mouth_lower_lip_right_contour1": {
"x": 63.749522,
"y": 58.464833
},
"mouth_lower_lip_right_contour2": {
"x": 66.612215,
"y": 59.135667
},
"mouth_lower_lip_right_contour3": {
"x": 62.240471,
"y": 60.064667
},
"mouth_lower_lip_top": {
"x": 58.154231,
"y": 58.838833
},
"mouth_right_corner": {
"x": 69.026932,
"y": 58.294
},
"mouth_upper_lip_bottom": {
"x": 58.517145,
"y": 58.379
},
"mouth_upper_lip_left_contour1": {
"x": 55.852539,
"y": 56.725667
},
"mouth_upper_lip_left_contour2": {
"x": 49.592642,
"y": 57.645333
},
"mouth_upper_lip_left_contour3": {
"x": 52.076233,
"y": 58.362833
},
"mouth_upper_lip_right_contour1": {
"x": 61.249742,
"y": 56.667
},
"mouth_upper_lip_right_contour2": {
"x": 65.439294,
"y": 57.277
},
"mouth_upper_lip_right_contour3": {
"x": 63.959971,
"y": 58.125
},
"mouth_upper_lip_top": {
"x": 58.5195,
"y": 57.083833
},
"nose_contour_left1": {
"x": 52.435614,
"y": 41.519333
},
"nose_contour_left2": {
"x": 49.87167,
"y": 47.251833
},
"nose_contour_left3": {
"x": 52.853569,
"y": 51.997833
},
"nose_contour_lower_middle": {
"x": 58.335247,
"y": 52.189167
},
"nose_contour_right1": {
"x": 60.867403,
"y": 41.468333
},
"nose_contour_right2": {
"x": 64.789698,
"y": 46.928
},
"nose_contour_right3": {
"x": 63.285651,
"y": 51.858167
},
"nose_left": {
"x": 47.735688,
"y": 50.500667
},
"nose_right": {
"x": 67.284768,
"y": 50.451
},
"nose_tip": {
"x": 58.709051,
"y": 49.160167
},
"right_eye_bottom": {
"x": 70.757027,
"y": 42.343333
},
"right_eye_center": {
"x": 70.706402,
"y": 41.111
},
"right_eye_left_corner": {
"x": 64.247241,
"y": 41.289
},
"right_eye_lower_left_quarter": {
"x": 67.440765,
"y": 41.938167
},
"right_eye_lower_right_quarter": {
"x": 73.901987,
"y": 41.790333
},
"right_eye_pupil": {
"x": 69.995879,
"y": 41.002833
},
"right_eye_right_corner": {
"x": 76.563944,
"y": 41.215333
},
"right_eye_top": {
"x": 70.456218,
"y": 40.078167
},
"right_eye_upper_left_quarter": {
"x": 67.865195,
"y": 40.249
},
"right_eye_upper_right_quarter": {
"x": 73.817219,
"y": 40.267
},
"right_eyebrow_left_corner": {
"x": 63.056954,
"y": 34.937
},
"right_eyebrow_lower_left_quarter": {
"x": 68.622517,
"y": 35.184167
},
"right_eyebrow_lower_middle": {
"x": 73.77454,
"y": 35.301167
},
"right_eyebrow_lower_right_quarter": {
"x": 78.323473,
"y": 35.747333
},
"right_eyebrow_right_corner": {
"x": 81.66181,
"y": 37.748833
},
"right_eyebrow_upper_left_quarter": {
"x": 68.276968,
"y": 32.617833
},
"right_eyebrow_upper_middle": {
"x": 74.246652,
"y": 32.364
},
"right_eyebrow_upper_right_quarter": {
"x": 79.812509,
"y": 34.099333
}
}
}
],
"session_id": "8f6bbd3f162c479f92858e5c93d5495a"
}
],
"img_height": 800,
"img_id": "85dfd5e1be4e9635736e90d5543085e6",
"img_width": 453,
"session_id": "0717217f2b8d417f8ddb42683e3986b8",
"url": "http://7xng7n.com1.z0.glb.clouddn.com/2.pic_hd.jpg/face.jpg"
}
# -*- encoding: utf-8 -*-
class Facexx
def initialize(key, secret, endpoint = 'http://apicn.faceplusplus.com/v2')
@params = {
api_key: key,
api_secret: secret
}
@connection = Faraday.new endpoint, ssl: { verify: false }
end
def get(url)
data = detect(url)
unless data.nil? || data['face'].empty?
data['face'].each do |face|
extra = landmark face['face_id']
face.merge!(extra) if extra
end
end
data
end
private
def detect(url)
parser post('/detection/detect',
url: url,
mode: 'oneface',
attribute: 'gender,age,race,smiling,glass,pose'
)
end
def landmark(face_id)
parser post('/detection/landmark', face_id: face_id)
end
def post(path, payload)
@connection.post path, @params.merge(payload)
end
def parser(resp)
MultiJson.load(resp.body) if resp.status == 200
end
end
gem 'faraday', require: false
gem 'typhoeus', require: 'typhoeus/adapters/faraday' # Faraday adapter
gem 'oj', require: false # MultiJson adapter
gem 'multi_json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment