Skip to content Skip to sidebar Skip to footer

Сompare Two Faces Using Python3 Module Face_recognition?

sorry for my bad english. I am trying to compare two faces using python3 module 'face_recognition' here is an example of calculating euclidean distance in python pdist([vector1, v

Solution 1:

Here is what you need!

import json


def load(str_data):
    str_data = str_data.replace("[", "").replace("]", "")
    result = []
    for i, line in enumerate(str_data.split("\n")):
        result.append([])
        for element in line.replace("  ", " ").split(" "):
            try:
                result[i].append(float(element))
            except ValueError:
                pass
    return result


def distance(model, test):
    distance = 0
    for i, line in enumerate(model):
        dist_line = 0
        for j, element in enumerate(line):
            dist_line += (element - test[i][j]) ** 2
        distance += dist_line ** 0.5
    return distance


if __name__ == "__main__":
    Find_me_image = '[-0.04221933  0.04460172  0.10287622 -0.14319997 -0.13808066  0.00552465\n -0.04414323 -0.07157505  0.23200855 -0.12091423  0.16892464 -0.16992114\n -0.2487883   0.09141497 -0.14198568  0.1824664  -0.11484738 -0.1130986\n -0.14396232 -0.06075872 -0.00201617  0.07473749 -0.01706937  0.05610432\n -0.11021845 -0.30173326 -0.02712429 -0.10394925 -0.05155517 -0.21909578\n  0.03083897  0.16680503 -0.09715255 -0.0407755  -0.01714687  0.08432341\n -0.01913652 -0.13662203  0.21924476  0.04394831 -0.20848413 -0.03259828\n  0.04784738  0.30321479  0.22730266 -0.02372641 -0.01165112 -0.12765107\n  0.13877977 -0.3403039   0.0424962   0.10813272  0.0511388   0.12078771\n  0.04942191 -0.13038178  0.02736722  0.15339687 -0.24367541  0.10453884\n  0.13450858 -0.09997959  0.01744595 -0.10602434  0.2614505   0.10681546\n -0.12075276 -0.12065229  0.195976   -0.11606392 -0.0447496   0.08198876\n -0.13573587 -0.18409243 -0.19127932  0.01680213  0.35644779  0.16652581\n -0.12988403 -0.00341757 -0.15569599 -0.09128557 -0.03799717  0.09235845\n  0.06296059 -0.07972728  0.00744779  0.07452074  0.23394027 -0.0726112\n -0.00072305  0.2978259  -0.01452125 -0.06529554 -0.08694689  0.01903715\n -0.14941891  0.10714116 -0.1096215   0.00143995  0.00146057  0.00348109\n  0.06795555  0.10826397 -0.18627991  0.21965174 -0.04136307 -0.01491791\n  0.03774849 -0.07495191 -0.03808937 -0.02331351  0.29242265 -0.23740929\n  0.13265632  0.1274993   0.17672779  0.11845816  0.01477844  0.07670261\n  0.11437597 -0.03779818 -0.21296507  0.03480547  0.06180557 -0.01749492\n -0.023851    0.11586148]'
    New_image = '[-0.04221933  0.04460172  0.10287622 -0.14319997 -0.13808064  0.00552465\n -0.04414323 -0.07157505  0.23200855 -0.12091423  0.16892464 -0.16992114\n -0.2487883   0.09141497 -0.14198568  0.18246  -0.11484738 -0.1130986\n -0.14396232 -0.06075872 -0.0020117  0.07473749 -0.01706937  0.05610432\n -0.11021845 -0.30173326 -0.02712429 -0.10394925 -0.05155517 -0.21909578\n  0.03083897  0.16680503 -0.09715255 -0.0407755  -0.01714687  0.08432341\n -0.01913652 -0.13662203  0.21924476  0.04394831 -0.20848413 -0.03259828\n  0.04784738  0.30321479  0.22730266 -0.02372641 -0.0116112 -0.12765107\n  0.13877977 -0.3403039   0.0424962   0.10813272  0.0511388   0.12078771\n  0.04942191 -0.13038178  0.02736722  0.15339687 -0.24367541  0.10453884\n  0.13450858 -0.09997959  0.01744595 -0.10602434  0.2614505   0.10681546\n -0.12075276 -0.12065229  0.195976   -0.11606392 -0.0447496   0.08198876\n -0.1357387 -0.18409243 -0.19127932  0.01680213  0.35644779  0.16652581\n -0.12988403 -0.00341757 -0.15569599 -0.09128557 -0.03799717  0.09235845\n  0.06296059 -0.07972728  0.00744779  0.07452074  0.23394027 -0.0726112\n -0.00072305  0.2978259  -0.01452125 -0.06529554 -0.08694689  0.0193715\n -0.14941891  0.10714116 -0.1096215   0.00143995  0.00146057  0.00348109\n  0.06795555  0.10826397 -0.18627991  0.21965174 -0.04136307 -0.01491791\n  0.03774849 -0.07495191 -0.03808937 -0.02331351  0.29242265 -0.23740929\n  0.13265632  0.1274993   0.1762779  0.11845816  0.01477844  0.07670261\n  0.11437597 -0.03779818 -0.21296507  0.03480547  0.0618057 -0.01749492\n -0.023851    0.1158648]'print(distance(
        load(Find_me_image),
        load(New_image)
    ))

You need first to convert your datas using load function. Then calculate distance using distance function. As your datas are the sames, I modify New_image datas to test the function.

Post a Comment for "Сompare Two Faces Using Python3 Module Face_recognition?"