Azure Face API using python

%3CLINGO-SUB%20id%3D%22lingo-sub-1008560%22%20slang%3D%22en-US%22%3EAzure%20Face%20API%20using%20python%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1008560%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Team%2C%3C%2FP%3E%3CP%3EWish%20to%20know%20how%20to%3CSTRONG%3E%20send%20local%20image%20URL%20to%20Azure%20face%20API%3C%2FSTRONG%3E.%20Please%20find%20my%20code%20below%20.%20I%20am%20receiving%20invalid%20URL%20error.%3C%2FP%3E%3CP%3Eplease%20do%20let%20me%20know%20at%20your%20earliest%20convenience.%26nbsp%3B%20Thanks%20in%20Advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF6600%22%3E%3CU%3E%3CSTRONG%3ECode%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3Ecam1%20%3D%20cv2.VideoCapture(1)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ewhile(True)%3A%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%23%20Capture%20frame-by-frame%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%23ret0%2C%20frame0%20%3D%20cam0.read()%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Eret1%2C%20frame1%20%3D%20cam1.read()%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ecv2.imwrite(%22images%2Fface1.png%22%2Cframe1)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Eurl%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Frafaces.cognitiveservices.azure.com%2Fface%2Fv1.0%2Fdetect%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Frafaces.cognitiveservices.azure.com%2Fface%2Fv1.0%2Fdetect%3C%2FA%3E%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3Equerystring%20%3D%20%7B%22q%22%3A%20%22requests%2Blanguage%3Apython%22%2C%22returnFaceAttributes%22%3A%22age%2Cgender%2Csmile%2Cemotion%2Cglasses%2Cnoise%22%2C%22maxNumOfCandidatesReturned%20%22%3A%22Tru%22%2C%22maxNumOfCandidatesReturned%2520%22%3A%22Tru%22%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3Epayload%20%3D%20%22%7B%5Cr%5Cn%20%5C%22url%5C%22%3A%5C%22images%2Fface1.png%5C%22%5Cr%5Cn%7D%22%20%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Eheaders%20%3D%20%7B%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Content-Type'%3A%20%22application%2Fjson%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Ocp-Apim-Subscription-Key'%3A%20%227af27b63c4a94e80b8d5d045a6927cd6%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'User-Agent'%3A%20%22PostmanRuntime%2F7.19.0%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Accept'%3A%20%22%2F%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Cache-Control'%3A%20%22no-cache%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Postman-Token'%3A%20%2244fbc05f-41ba-40dc-9c8a-72708fc93a3c%2Caf17bcb2-4835-412e-b968-5437592c9fb1%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Host'%3A%20%22rafaces.cognitiveservices.azure.com%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Accept-Encoding'%3A%20%22gzip%2C%20deflate%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Content-Length'%3A%20%2284%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'Connection'%3A%20%22keep-alive%22%2C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E'cache-control'%3A%20%22no-cache%22%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3Eresponse1%20%3D%20requests.request(%22POST%22%2C%20url%2C%20data%3Dpayload%2C%20headers%3Dheaders%2C%20params%3Dquerystring)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ejson_response%20%3D%20response1.json()%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Eprint(json_response)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ex%20%3D%20len(json_response)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Eprint(x)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ecv2.imshow('image0'%2C%20frame1)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Esleep(10)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Eif%20cv2.waitKey(1)%20%26amp%3B%200xFF%20%3D%3D%20ord('q')%3A%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3Ebreak%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF6600%22%3E%3CU%3E%3CSTRONG%3EResponse%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%7B'error'%3A%20%7B'code'%3A%20'InvalidURL'%2C%20'message'%3A%20'Invalid%20image%20URL.'%7D%7D%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

Hi Team,

Wish to know how to send local image URL to Azure face API. Please find my code below . I am receiving invalid URL error.

please do let me know at your earliest convenience.  Thanks in Advance.

 

Code

 

cam1 = cv2.VideoCapture(1)
while(True):
# Capture frame-by-frame
#ret0, frame0 = cam0.read()
ret1, frame1 = cam1.read()
cv2.imwrite("images/face1.png",frame1)
url = "https://rafaces.cognitiveservices.azure.com/face/v1.0/detect"

querystring = {"q": "requests+language:python","returnFaceAttributes":"age,gender,smile,emotion,glasses,noise","maxNumOfCandidatesReturned ":"Tru","maxNumOfCandidatesReturned%20":"Tru"}

payload = "{\r\n \"url\":\"images/face1.png\"\r\n}"
headers = {
'Content-Type': "application/json",
'Ocp-Apim-Subscription-Key': "7af27b63c4a94e80b8d5d045a6927cd6",
'User-Agent': "PostmanRuntime/7.19.0",
'Accept': "/",
'Cache-Control': "no-cache",
'Postman-Token': "44fbc05f-41ba-40dc-9c8a-72708fc93a3c,af17bcb2-4835-412e-b968-5437592c9fb1",
'Host': "rafaces.cognitiveservices.azure.com",
'Accept-Encoding': "gzip, deflate",
'Content-Length': "84",
'Connection': "keep-alive",
'cache-control': "no-cache"
}

response1 = requests.request("POST", url, data=payload, headers=headers, params=querystring)
json_response = response1.json()
print(json_response)
x = len(json_response)
print(x)
cv2.imshow('image0', frame1)
sleep(10)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

 

 

Response

{'error': {'code': 'InvalidURL', 'message': 'Invalid image URL.'}}

0 Replies