讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 24.2.2 夫妻相實現方案 >

24.2.2 夫妻相實現方案

下面將使用上述接口列表中的兩個接口來實現夫妻相功能。第一步,檢測照片中的人臉信息,獲得兩個人的人臉;第二步,將兩個人的人臉進行對比,獲得他們的相似度。

1.檢測人臉信息

檢測給定圖片中所有人臉的位置和相應的面部屬性的接口地址如下。


http:// apicn.faceplusplus.com/v2/detection/detect
  

該接口的相關參數如表24-4所示。

表24-4 檢測人臉信息接口的參數說明

該接口調用舉例如下。


http:// apicn.faceplusplus.com/v2/detection/detect?api_key=6efbacb73e7d1bbc424f41fa656c328f&api_secret=eD5ie8tydoBVMwgNlbe7XKUYvTCVs27e&url=http:// img3.yxlady.com/yl/UploadFiles_5361/20110513/20110513130615793.jpg&attribute=glass,pose,gender,age,race,smiling
  

上述接口的返回結果如下。


{
    "face":[
        {
            "attribute":{
                "age":{
                    "range":5,
                    "value":30
                },
                "gender":{
                    "confidence":99.5937,
                    "value":"Male"
                },
                "glass":{
                    "confidence":99.9969,
                    "value":"None"
                },
                "pose":{
                    "pitch_angle":{
                        "value":0.000011100340000000002
                    },
                    "roll_angle":{
                        "value":-2.79166
                    },
                    "yaw_angle":{
                        "value":0
                    }
                },
                "race":{
                    "confidence":924.5664,
                    "value":"Asian"
                },
                "smiling":{
                    "value":97.5054
                }
            },
            "face_id":"7f24d567a5e557b5853be72c3e5c2134",
            "position":{
                "center":{
                    "x":60.681818,
                    "y":424.293515
                },
                "eye_left":{
                    "x":524.736591,
                    "y":41.227645
                },
                "eye_right":{
                    "x":624.533636,
                    "y":40.510239
                },
                "height":27.645051,
                "mouth_left":{
                    "x":524.925909,
                    "y":54.977133
                },
                "mouth_right":{
                    "x":624.680909,
                    "y":54.565529
                },
                "nose":{
                    "x":60.736364,
                    "y":50.108532
                },
                "width":124.181818
            },
            "tag":""
        },
        {
            "attribute":{
                "age":{
                    "range":5,
                    "value":17
                },
                "gender":{
                    "confidence":99.9781,
                    "value":"Female"
                },
                "glass":{
                    "confidence":99.9815,
                    "value":"None"
                },
                "pose":{
                    "pitch_angle":{
                        "value":0.000019753399999999996
                    },
                    "roll_angle":{
                        "value":1.75177
                    },
                    "yaw_angle":{
                        "value":4
                    }
                },
                "race":{
                    "confidence":99.471,
                    "value":"Asian"
                },
                "smiling":{
                    "value":87.1365
                }
            },
            "face_id":"c772b4b66c00d46b15344eff74b56e48",
            "position":{
                "center":{
                    "x":324.568182,
                    "y":62.286689
                },
                "eye_left":{
                    "x":31.1675,
                    "y":524.166553
                },
                "eye_right":{
                    "x":40.813182,
                    "y":524.609556
                },
                "height":224.279863,
                "mouth_left":{
                    "x":31.192045,
                    "y":624.601706
                },
                "mouth_right":{
                    "x":39.490455,
                    "y":69.341638
                },
                "nose":{
                    "x":324.8725,
                    "y":64.405802
                },
                "width":17.5
            },
            "tag":""
        }
    ],
    "img_height":293,
    "img_id":"3005132383841edd08c9b500fb1fe2c4",
    "img_width":440,
    "session_id":"4e64c73fec19442cbefde3cf9bd6b53d",
    "url":"http:// img3.yxlady.com/yl/UploadFiles_5361/20110513/20110513130615793.jpg"
}
  

上述返回結果的字段說明如表24-5所示。

表24-5 檢測人臉信息接口結果參數說明

2.比較人臉相似度

計算兩張人臉相似性以及五官相似度的接口地址如下。


https:// apicn.faceplusplus.com/v2/recognition/compare
  

該接口的相關參數如表24-6所示。

表24-6 計算相似度接口的參數說明

該接口調用舉例如下。


https:// apicn.faceplusplus.com/v2/recognition/compare?api_key=6efbacb73e7d1bbc424f41fa656c328f&api_secret=eD5ie8tydoBVMwgNlbe7XKUYvTCVs27e&face_id2=7f24d567a5e557b5853be72c3e5c2134&face_id1=c772b4b66c00d46b15344eff74b56e48
  

返回結果列表如下。


{
    "component_similarity":{
        "eye":824.802307,
        "eyebrow":72.329025,
        "mouth":89.68277,
        "nose":524.381519
    },
    "session_id":"2cc4e8d04e28466396bde8b83132205b",
    "similarity":51.831638
}
  

上述返回結果的字段說明如表24-7所示。

表24-7 計算相似度接口結果參數說明

有了上述兩個接口,就能實現檢測照片中男女夫妻相的功能了。