def call_face_emotion_api(img): # Face API and Emotion API Variables _url_face = 'https://api.projectoxford.ai/face/v1.0/detect' _key_face = '5d99eec09a7e4b2a916eba7f75671600' # primary key _url_emo = 'https://api.projectoxford.ai/emotion/v1.0/recognize' _key_emo = "b226d933ab854505b9b9877cf2f4ff7c" # primary key _maxNumRetries = 10 global age, gender, emo cv2.imwrite('test.jpg', img) pathToFileInDisk = r'test.jpg' with open( pathToFileInDisk, 'rb' ) as f: data = f.read() """ Face API """ # Face detection parameters params = { 'returnFaceAttributes': 'age,gender,glasses', 'returnFaceLandmarks': 'true'} headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face headers['Content-Type'] = 'application/octet-stream' json = None faceResult, status_code = processRequest('post', _url_face, json, data, headers, params, _maxNumRetries ) """ Emotion API """ headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_emo headers['Content-Type'] = 'application/octet-stream' json = None emoResult, status_code2 = processRequest('post', _url_emo, json, data, headers, None, _maxNumRetries ) """ Results """ print 'Found {} faces'.format(len(faceResult)) for currFace in faceResult: #faceRectangle = currFace['faceRectangle'] faceAttributes = currFace['faceAttributes'] age = str(faceAttributes['age']) gender = faceAttributes['gender'] glasses = faceAttributes['glasses'] print age, gender, glasses for currFace in emoResult: emo = max(currFace['scores'].iteritems(), key=operator.itemgetter(1))[0] print emo return age, gender, glasses, emo
def call_face_emotion_api(img): global age, gender, emo cv2.imwrite('test.jpg', img) pathToFileInDisk = r'test.jpg' with open( pathToFileInDisk, 'rb' ) as f: data = f.read() """ Face API """ # Face detection parameters params = { 'returnFaceAttributes': 'age,gender,glasses', 'returnFaceLandmarks': 'true'} headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face headers['Content-Type'] = 'application/octet-stream' json = None faceResult = processRequest( _url_face, json, data, headers, params, _maxNumRetries ) """ Emotion API """ headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_emo headers['Content-Type'] = 'application/octet-stream' json = None emoResult = processRequest( _url_emo, json, data, headers, None, _maxNumRetries ) """ Outputs """ print 'Found {} faces'.format(len(faceResult)) for currFace in faceResult: #faceRectangle = currFace['faceRectangle'] faceAttributes = currFace['faceAttributes'] age = str(faceAttributes['age']) gender = faceAttributes['gender'] glasses = faceAttributes['glasses'] print age, gender, glasses for currFace in emoResult: emo = max(currFace['scores'].iteritems(), key=operator.itemgetter(1))[0] print emo return age, gender, glasses, emo
def addPersonFace(personGroupId, personId, urlImage, pathToImageInDisk, imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face if ((urlImage == None) or (urlImage == "")) and (imageData == None): headers['Content-Type'] = 'application/octet-stream' json = None with open(pathToImageInDisk, 'rb') as f: data = f.read() if ((pathToImageInDisk == None) or (pathToImageInDisk == "")) and (imageData == None): headers['Content-Type'] = 'application/json' json = {'url': urlImage} data = None if (imageData != None): headers['Content-Type'] = 'application/octet-stream' data = imageData json = None url = _url + 'persongroups/' + personGroupId + '/persons/' + personId + '/persistedFaces' result, status_code = processRequest('post', url, json, data, headers, None, 2) return result['persistedFaceId']
def faceDetect(urlImage, pathToImageInDisk, imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face if ((urlImage == None) or (urlImage == "")) and (imageData == None): headers['Content-Type'] = 'application/octet-stream' json = None with open(pathToImageInDisk, 'rb') as f: data = f.read() if ((pathToImageInDisk == None) or (pathToImageInDisk == "")) and (imageData == None): headers['Content-Type'] = 'application/json' json = {'url': urlImage} data = None if (imageData != None): headers['Content-Type'] = 'application/octet-stream' data = imageData json = None # Face detection parameters params = { 'returnFaceAttributes': 'age,gender,glasses', 'returnFaceLandmarks': 'true' } url_face = _url + 'detect' result, status_code = processRequest('post', url_face, json, data, headers, params, 10) return result
def state_cap2(self, key): ''' CAPTURES / PROCESS IMAGE TEAM 2 ''' # Captures the image and returns the encoded and the opencv versions img, self.curr_image = self.snp(ret_orig=True) self.local_shape = self.curr_image.shape self.curr_image = cv2.resize(self.curr_image, (self.shape[1], self.shape[0])) self.team2 = self.curr_image.copy() msg_pos = (self.shape[1] / 2 - 30, self.shape[0] / 2) if (time.time() - self.blink) <= 2: cv2.putText(self.curr_image, "Ready...", (msg_pos[0] - 30, msg_pos[1]), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4) cv2.putText(self.curr_image, "Emotion: " + self.rnd_emotion, (msg_pos[0] - 200, msg_pos[1] + 50), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 6) cv2.imshow(self.window_name, self.curr_image) elif 2.25 >= (time.time() - self.blink) > 2: cv2.putText(self.curr_image, "Go!", (msg_pos[0] - 30, msg_pos[1]), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4) cv2.imshow(self.window_name, self.curr_image) self.img_changed = True elif (time.time() - self.blink) > 2.25: if self.img_changed: cv2.putText(self.curr_image, "Enjoy yourself while you wait...", (msg_pos[0] - 350, msg_pos[1]), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4) cv2.imshow(self.window_name, self.curr_image) self.img = img self.img_changed = False # starts the process only once else: result = processRequest(None, self.img, self.headers, None, self.key) if len(result): self.team2_data.append( [result, self.rnd_emotion, self.local_shape]) self.img_changed_2 = True self.img_changed = True self.game_state = 'main' # goes back to MAIN SCREEN else: print "No results!" self.blink = time.time()
def createPerson(personGroupId, personName, userData): headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': _key_face, } body = {'name': personName, 'userData': userData} url = _url + 'persongroups/' + personGroupId + '/persons' result, status_code = processRequest('post', url, body, None, headers, None, 2) return result['personId']
def faceDetect(imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face headers['Content-Type'] = 'application/octet-stream' data = imageData json = None # Face detection parameters params = { 'returnFaceAttributes': 'age,gender,glasses', 'returnFaceLandmarks': 'true'} url_face = _url + 'detect' result, status_code = processRequest('post', url_face, json, data, headers, params, 10 ) return result
def faceIdentify(personGroupId, faceIds, nbCandidate): headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': _key_face, } json = { "personGroupId": personGroupId, "faceIds": faceIds, "maxNumOfCandidatesReturned": nbCandidate } url = _url + 'identify' result, status_code = processRequest('post', url, json, None, headers, None, 2 ) return result
def createPerson(personGroupId, personName, userData): headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': _key_face, } body = { 'name': personName, 'userData': userData } url = _url + 'persongroups/' + personGroupId + '/persons' result, status_code = processRequest('post', url, body, None, headers, None, 2 ) return result['personId']
def faceIdentify(personGroupId, faceIds, nbCandidate): headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': _key_face, } json = { "personGroupId": personGroupId, "faceIds": faceIds, "maxNumOfCandidatesReturned": nbCandidate } url = _url + 'identify' result, status_code = processRequest('post', url, json, None, headers, None, 2) return result
def faceDetect(imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face headers['Content-Type'] = 'application/octet-stream' data = imageData json = None # Face detection parameters params = { 'returnFaceAttributes': 'age,gender,glasses', 'returnFaceLandmarks': 'true' } url_face = _url + 'detect' result, status_code = processRequest('post', url_face, json, data, headers, params, 10) return result
def recognizeEmotion(urlImage, pathToImageInDisk, imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_emo if ((urlImage==None) or (urlImage=="")) and (imageData==None): headers['Content-Type'] = 'application/octet-stream' json = None with open( pathToImageInDisk, 'rb' ) as f: data = f.read() if ((pathToImageInDisk==None) or (pathToImageInDisk=="")) and (imageData==None): headers['Content-Type'] = 'application/json' json = { 'url': urlImage } data = None if (imageData!=None): headers['Content-Type'] = 'application/octet-stream' data = imageData json = None result, status_code = processRequest('post', _url_emo, json, data, headers, None, _maxNumRetries ) return result
def addPersonFace(personGroupId, personId, urlImage, pathToImageInDisk, imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face if ((urlImage==None) or (urlImage=="")) and (imageData==None): headers['Content-Type'] = 'application/octet-stream' json = None with open( pathToImageInDisk, 'rb' ) as f: data = f.read() if ((pathToImageInDisk==None) or (pathToImageInDisk=="")) and (imageData==None): headers['Content-Type'] = 'application/json' json = { 'url': urlImage } data = None if (imageData!=None): headers['Content-Type'] = 'application/octet-stream' data = imageData json = None url = _url + 'persongroups/' + personGroupId + '/persons/' + personId + '/persistedFaces' result, status_code = processRequest('post', url, json, data, headers, None, 2 ) return result['persistedFaceId']
def recognizeEmotion(urlImage, pathToImageInDisk, imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_emo if ((urlImage == None) or (urlImage == "")) and (imageData == None): headers['Content-Type'] = 'application/octet-stream' json = None with open(pathToImageInDisk, 'rb') as f: data = f.read() if ((pathToImageInDisk == None) or (pathToImageInDisk == "")) and (imageData == None): headers['Content-Type'] = 'application/json' json = {'url': urlImage} data = None if (imageData != None): headers['Content-Type'] = 'application/octet-stream' data = imageData json = None result, status_code = processRequest('post', _url_emo, json, data, headers, None, _maxNumRetries) return result
def faceDetect(urlImage, pathToImageInDisk, imageData): headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key_face if ((urlImage==None) or (urlImage=="")) and (imageData==None): headers['Content-Type'] = 'application/octet-stream' json = None with open( pathToImageInDisk, 'rb' ) as f: data = f.read() if ((pathToImageInDisk==None) or (pathToImageInDisk=="")) and (imageData==None): headers['Content-Type'] = 'application/json' json = { 'url': urlImage } data = None if (imageData!=None): headers['Content-Type'] = 'application/octet-stream' data = imageData json = None # Face detection parameters params = { 'returnFaceAttributes': 'age,gender,glasses', 'returnFaceLandmarks': 'true'} url_face = _url + 'detect' result, status_code = processRequest('post', url_face, json, data, headers, params, 10 ) return result