2020-07-07 03:22:46 +05:30
|
|
|
import http.client
|
2020-07-02 07:46:03 +05:30
|
|
|
import json
|
2020-07-07 03:22:46 +05:30
|
|
|
import random
|
2020-07-02 07:46:03 +05:30
|
|
|
import time
|
2020-07-07 03:22:46 +05:30
|
|
|
import urllib.error
|
|
|
|
import urllib.parse
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
# Face API Key and Endpoint
|
|
|
|
f = open('api_key.txt', 'r')
|
|
|
|
data = f.read().split("\n")
|
|
|
|
subscription_key = data[0]
|
|
|
|
uri_base = data[1]
|
|
|
|
|
|
|
|
cloak_image_base = 'http://sandlab.cs.uchicago.edu/fawkes/files/cloak/{}_ultra_cloaked.png'
|
|
|
|
original_image_base = 'http://sandlab.cs.uchicago.edu/fawkes/files/cloak/{}.png'
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
def detect_face(image_url):
|
2020-07-07 03:22:46 +05:30
|
|
|
r = requests.get(image_url)
|
|
|
|
if r.status_code != 200:
|
|
|
|
return None
|
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
headers = {
|
|
|
|
# Request headers
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
# Request parameters
|
|
|
|
'returnFaceId': 'true',
|
|
|
|
'returnFaceLandmarks': 'false',
|
|
|
|
'recognitionModel': 'recognition_01',
|
|
|
|
'returnRecognitionModel': 'false',
|
|
|
|
'detectionModel': 'detection_01',
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({
|
|
|
|
'url': image_url
|
|
|
|
})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("POST", "/face/v1.0/detect?%s" % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
2020-07-07 21:44:38 +05:30
|
|
|
#
|
|
|
|
# print(data)
|
2020-07-02 07:46:03 +05:30
|
|
|
conn.close()
|
|
|
|
return data[0]["faceId"]
|
|
|
|
|
|
|
|
|
|
|
|
def verify_face(faceId, personGroupId, personId):
|
|
|
|
# html header
|
|
|
|
headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
# image URL
|
|
|
|
body = json.dumps({
|
|
|
|
"faceId": faceId,
|
|
|
|
"personId": personId,
|
|
|
|
"PersonGroupId": personGroupId
|
|
|
|
})
|
|
|
|
|
|
|
|
# Call Face API
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("POST", "/face/v1.0/verify?%s" % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
|
|
|
conn.close()
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def create_personGroupId(personGroupId, personGroupName):
|
|
|
|
headers = {
|
|
|
|
# Request headers
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({
|
|
|
|
"name": personGroupName
|
|
|
|
})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("PUT", "/face/v1.0/persongroups/{}?%s".format(personGroupId) % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = response.read()
|
|
|
|
print(data)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
def create_personId(personGroupId, personName):
|
|
|
|
headers = {
|
|
|
|
# Request headers
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({
|
|
|
|
"name": personName
|
|
|
|
})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("POST", "/face/v1.0/persongroups/{}/persons?%s".format(personGroupId) % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
2020-07-07 03:22:46 +05:30
|
|
|
# print(data)
|
2020-07-02 07:46:03 +05:30
|
|
|
conn.close()
|
|
|
|
return data["personId"]
|
|
|
|
|
|
|
|
|
|
|
|
def add_persistedFaceId(personGroupId, personId, image_url):
|
2020-07-07 03:22:46 +05:30
|
|
|
r = requests.get(image_url)
|
|
|
|
if r.status_code != 200:
|
|
|
|
return None
|
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
'personGroupId': personGroupId,
|
|
|
|
'personId': personId
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({
|
|
|
|
'url': image_url
|
|
|
|
})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
2020-07-07 03:22:46 +05:30
|
|
|
conn.request("POST",
|
|
|
|
"/face/v1.0/persongroups/{}/persons/{}/persistedFaces?%s".format(personGroupId, personId) % params,
|
|
|
|
body, headers)
|
2020-07-02 07:46:03 +05:30
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
|
|
|
conn.close()
|
2020-07-07 03:22:46 +05:30
|
|
|
if "persistedFaceId" not in data:
|
|
|
|
return None
|
2020-07-02 07:46:03 +05:30
|
|
|
return data["persistedFaceId"]
|
|
|
|
|
|
|
|
|
|
|
|
def list_personGroupPerson(personGroupId):
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("GET", "/face/v1.0/persongroups/{}/persons?%s".format(personGroupId) % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
|
|
|
conn.close()
|
|
|
|
for person in data:
|
|
|
|
print(person["personId"], len(person["persistedFaceIds"]))
|
|
|
|
|
|
|
|
|
|
|
|
def get_personGroupPerson(personGroupId, personId):
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
2020-07-07 03:22:46 +05:30
|
|
|
conn.request("GET", "/face/v1.0/persongroups/{}/persons/{}?%s".format(personGroupId, personId) % params, body,
|
|
|
|
headers)
|
2020-07-02 07:46:03 +05:30
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
|
|
|
print(data)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
def train_personGroup(personGroupId):
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("POST", "/face/v1.0/persongroups/{}/train?%s".format(personGroupId) % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = response.read()
|
|
|
|
print(data)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
def eval(original_faceIds, personGroupId, protect_personId):
|
|
|
|
headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({
|
|
|
|
'faceIds': original_faceIds,
|
|
|
|
'personGroupId': personGroupId,
|
|
|
|
'maxNumOfCandidatesReturned': 1
|
|
|
|
})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("POST", "/face/v1.0/identify?%s" % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = json.loads(response.read())
|
|
|
|
conn.close()
|
|
|
|
face = data[0]
|
2020-07-07 03:22:46 +05:30
|
|
|
print(face)
|
2020-07-02 07:46:03 +05:30
|
|
|
if len(face["candidates"]) and face["candidates"][0]["personId"] == protect_personId:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def delete_personGroupPerson(personGroupId, personId):
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
2020-07-07 03:22:46 +05:30
|
|
|
conn.request("DELETE", "/face/v1.0/persongroups/{}/persons/{}?%s".format(personGroupId, personId) % params, body,
|
|
|
|
headers)
|
2020-07-02 07:46:03 +05:30
|
|
|
response = conn.getresponse()
|
|
|
|
data = response.read()
|
|
|
|
print(data)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
def get_trainStatus(personGroupId):
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
params = urllib.parse.urlencode({})
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("GET", "/face/v1.0/persongroups/{}/training?%s".format(personGroupId) % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = response.read()
|
|
|
|
conn.close()
|
2020-07-07 21:44:38 +05:30
|
|
|
return data
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
def test_cloak():
|
|
|
|
NUM_TRAIN = 10
|
|
|
|
total_idx = range(0, 82)
|
|
|
|
TRAIN_RANGE = random.sample(total_idx, NUM_TRAIN)
|
2020-07-02 07:46:03 +05:30
|
|
|
|
2020-07-07 21:44:38 +05:30
|
|
|
TEST_RANGE = random.sample([i for i in total_idx if i not in TRAIN_RANGE], 20)
|
2020-07-02 07:46:03 +05:30
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
personGroupId = 'all'
|
|
|
|
|
|
|
|
# delete_personGroup(personGroupId)
|
2020-07-07 21:44:38 +05:30
|
|
|
# create_personGroupId(personGroupId, personGroupId)
|
2020-07-07 03:22:46 +05:30
|
|
|
|
|
|
|
with open("protect_personId.txt", 'r') as f:
|
|
|
|
protect_personId = f.read()
|
|
|
|
print(protect_personId)
|
|
|
|
delete_personGroupPerson(personGroupId, protect_personId)
|
|
|
|
|
|
|
|
protect_personId = create_personId(personGroupId, 'Emily')
|
|
|
|
with open("protect_personId.txt", 'w') as f:
|
|
|
|
f.write(protect_personId)
|
|
|
|
|
|
|
|
print("Created protect personId: {}".format(protect_personId))
|
|
|
|
for idx in TRAIN_RANGE:
|
|
|
|
image_url = cloak_image_base.format(idx)
|
|
|
|
r = add_persistedFaceId(personGroupId, protect_personId, image_url)
|
|
|
|
if r is not None:
|
|
|
|
print("Added {}".format(idx))
|
|
|
|
else:
|
|
|
|
print("Unable to add {}-th image of protect person".format(idx))
|
|
|
|
|
|
|
|
# add other people
|
2020-07-07 21:44:38 +05:30
|
|
|
# for idx_person in range(1300, 5000):
|
|
|
|
# personId = create_personId(personGroupId, str(idx_person))
|
|
|
|
# print("Created personId: {}".format(idx_person))
|
|
|
|
# for idx_image in range(10):
|
|
|
|
# image_url = "http://sandlab.cs.uchicago.edu/fawkes/files/target_data/{}/{}.jpg".format(
|
|
|
|
# idx_person, idx_image)
|
|
|
|
# r = add_persistedFaceId(personGroupId, personId, image_url)
|
|
|
|
# if r is not None:
|
|
|
|
# print("Added {}".format(idx_image))
|
|
|
|
# else:
|
|
|
|
# print("Unable to add {}-th image".format(idx_image))
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
# train model based on personGroup
|
2020-07-07 21:44:38 +05:30
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
train_personGroup(personGroupId)
|
2020-07-07 21:44:38 +05:30
|
|
|
|
|
|
|
while json.loads(get_trainStatus(personGroupId))['status'] != 'succeeded':
|
|
|
|
time.sleep(2)
|
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
# list_personGroupPerson(personGroupId)
|
2020-07-02 07:46:03 +05:30
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
# test original image
|
|
|
|
idx_range = TEST_RANGE
|
2020-07-02 07:46:03 +05:30
|
|
|
acc = 0.
|
2020-07-07 03:22:46 +05:30
|
|
|
tot = 0.
|
2020-07-02 07:46:03 +05:30
|
|
|
for idx in idx_range:
|
2020-07-07 03:22:46 +05:30
|
|
|
original_image_url = original_image_base.format(idx)
|
2020-07-02 07:46:03 +05:30
|
|
|
faceId = detect_face(original_image_url)
|
2020-07-07 03:22:46 +05:30
|
|
|
if faceId is None:
|
|
|
|
print("{} does not exist".format(idx))
|
|
|
|
continue
|
2020-07-02 07:46:03 +05:30
|
|
|
original_faceIds = [faceId]
|
|
|
|
|
|
|
|
# verify
|
|
|
|
res = eval(original_faceIds, personGroupId, protect_personId)
|
|
|
|
if res:
|
|
|
|
acc += 1.
|
2020-07-07 03:22:46 +05:30
|
|
|
tot += 1.
|
2020-07-02 07:46:03 +05:30
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
acc /= tot
|
|
|
|
print(acc) # 1.0
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
def list_personGroups():
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("GET", "/face/v1.0/persongroups?%s" % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = response.read()
|
|
|
|
print(data)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
def delete_personGroup(personGroupId):
|
|
|
|
headers = {
|
|
|
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
params = urllib.parse.urlencode({
|
|
|
|
})
|
|
|
|
|
|
|
|
body = json.dumps({})
|
|
|
|
|
|
|
|
conn = http.client.HTTPSConnection(uri_base)
|
|
|
|
conn.request("DELETE", "/face/v1.0/persongroups/{}?%s".format(personGroupId) % params, body, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
data = response.read()
|
|
|
|
print(data)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-07-07 03:22:46 +05:30
|
|
|
test_cloak()
|
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
# delete_personGroup('cloaking')
|
|
|
|
# delete_personGroup('cloaking-emily')
|
|
|
|
# delete_personGroup('pubfig')
|
|
|
|
# list_personGroups()
|
|
|
|
# exit()
|
2020-07-07 03:22:46 +05:30
|
|
|
# personGroupId = 'cloaking'
|
2020-07-02 07:46:03 +05:30
|
|
|
# create_personGroupId(personGroupId, 'cloaking')
|
2020-07-07 03:22:46 +05:30
|
|
|
# delete_personGroupPerson(personGroupId, '0ac606cd-24b3-440f-866a-31adf2a1b446')
|
|
|
|
# add_protect_person(personGroupId, 'Emily')
|
|
|
|
# protect_personId = create_personId(personGroupId, 'Emily')
|
|
|
|
# add_sybil_person(personGroupId, 'sybil')
|
|
|
|
#
|
|
|
|
# # train model based on personGroup
|
|
|
|
# train_personGroup(personGroupId)
|
|
|
|
# get_trainStatus(personGroupId)
|
|
|
|
# add_other_person(personGroupId)
|
|
|
|
# list_personGroupPerson(personGroupId)
|
|
|
|
#
|
|
|
|
# idx_range = range(72, 82)
|
|
|
|
# original_faceIds = []
|
|
|
|
# for idx in idx_range:
|
|
|
|
# original_image_url = "https://super.cs.uchicago.edu/~shawn/cloaked/{}_o.png".format(idx)
|
|
|
|
# faceId = detect_face(original_image_url)
|
|
|
|
# original_faceIds.append(faceId)
|
|
|
|
#
|
|
|
|
# # verify
|
|
|
|
# eval(original_faceIds, personGroupId, protect_personId)
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-07 03:22:46 +05:30
|
|
|
test_cloak()
|