def computeFaceDetectionAmazon(self,face_image_url, file_name, gender): table_name = 'FFS3' partition_name = 'Amazon' urllib.request.urlretrieve(face_image_url, file_name) with open(file_name, 'rb') as image: response = self.amazon_client.detect_faces(Image={'Bytes': image.read()}, Attributes=['ALL']) faces = response['FaceDetails'] success = False faceDetected = False genderPrediction = 'None' if len(faces) == 0: success = False faceDetected = False else: faceDetected = True genderPrediction = faces[0]["Gender"]["Value"] if gender.lower()==genderPrediction.lower(): success = True else: success = False face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower()
def computeFaceDetectionIBM(self,face_image_url, file_name, gender): table_name = 'IBM' partition_name = 'IBM' urllib.request.urlretrieve(face_image_url, file_name) with open(file_name, 'rb') as image_file: response = self.IBM_visual_recognition.detect_faces(image_file) faces = response['images'][0]['faces'] success = False faceDetected = False genderPrediction = 'None' if len(faces) == 0: success = False faceDetected = False elif len(faces)>0: faceDetected = True genderPrediction = faces[0]["gender"]["gender"] if gender.lower()==genderPrediction.lower(): success = True face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(response) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower()
def computeFaceDetectionMicrosoft(self,face_image_url, file_name, gender): table_name = 'Microsoft' partition_name = 'Microsoft' data = {'url': face_image_url} response = requests.post(self.msft_face_detection_url, params=self.msft_params, headers=self.msft_headers, json=data) faces = response.json() success = False genderPrediction = 'None' faceDetected = False if len(faces) == 0: success = False faceDetected = False elif len(faces)>0: faceDetected = True genderPrediction = faces[0]["faceAttributes"]["gender"] if gender==genderPrediction: success = True face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower()
def computeFaceDetectionFacePlusPlus(self,face_image_url, file_name, gender): table_name = 'FacePlusPlus' partition_name = 'FacePlusPlus' boundary = '----------%s' % hex(int(time.time() * 1000)) data = [] data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key') data.append(self.faceplusplus_key) data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret') data.append(self.faceplusplus_secret) data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes') data.append('gender') data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'image_url') data.append(face_image_url) data.append('--%s--\r\n' % boundary) http_body='\r\n'.join(data) req=urllib.request.Request(self.faceplusplus_http_url) req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary) req.data = str.encode(http_body) try: resp = urllib.request.urlopen(req, timeout=5) qrcont=resp.read().decode("utf-8") faces = json.loads(qrcont) success = False faceDetected = False genderPrediction = 'None' if 'faces' in faces.keys(): faceDetected = True genderPrediction = faces["faces"][0]["attributes"]["gender"]["value"] if gender.lower()==genderPrediction.lower(): success = True else: success = None faceDetected = None time.sleep(2) face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower() except urllib.request.HTTPError as e: return None, None, None, None
def computeFaceDetectionGoogle(self,face_image_url, file_name, gender): table_name = 'Google' partition_name = 'Google' self.clientImgAnnotator = vision.ImageAnnotatorClient() image = vision.types.Image() image.source.image_uri = face_image_url try: response = self.clientImgAnnotator.face_detection(image=image) r = MessageToDict(response, preserving_proto_field_name = True) success = False faceDetected = False genderPrediction = 'None' if len(r) == 0: success = False faceDetected = False faces=[] elif len(r)>0: faceDetected = True success = False faces = r['face_annotations'] face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower() except WatsonApiException as ex: print("Method failed with status code " + str(ex.code) + ": " + ex.message)
def computeFaceDetectionSightEngine(self,face_image_url, file_name, gender): table_name = 'SightEngine' partition_name = 'SightEngine' output = self.SEclient.check('face-attributes').set_url(face_image_url) r = response.json() faces = r["faces"] success = False genderPrediction = 'None' faceDetected = False if len(faces) == 0: success = False faceDetected = False elif len(faces)>0: faceDetected = True femaleProb = faces[0]["attributes"]["female"] maleProb = faces[0]["attributes"]["male"] if femaleProb>maleProb: genderPrediction = "female" else: genderPrediction = "male" if gender==genderPrediction: success = True face_entry = Entity() face_entry.PartitionKey = partition_name face_entry.RowKey = file_name face_entry.Result = json.dumps(faces) face_entry.DetectionSuccess = success self.table_service.insert_entity(table_name, face_entry) return success, faces, faceDetected, genderPrediction.lower()