You will need to generate a private key at RapidAPI, then you can use it here.
import requests
# I keep mine in another file, which is not sent to GitHub
# Or put yours in the string 'key' below
try:
from secrets import mashape as key
except ImportError as e:
key = " KEY GOES HERE "
Face++ has a comprehensive API, some of which is free. Their site allows you to build apps to do all kinds of clever things.
We're going to use their free API via RapidAPI, which is a convenient place to try APIs for all sorts of things. Let's try a simple detection on this face...
url = "https://faceplusplus-faceplusplus.p.rapidapi.com/facepp/v3/detect"
target = "https://static1.squarespace.com/static/549dcda5e4b0a47d0ae1db1e/t/5af31248562fa79d0f19ca68/1525879588450/?format=300w"
params = {'image_url': target}
headers={
"X-Mashape-Key": key,
"Content-Type": "application/x-www-form-urlencoded"
}
response = requests.post(url, headers=headers, data=params)
response.json()
{'image_id': 'NOknGGbV/+A3ERkzEq8tJw==', 'request_id': '1553365760,c737662c-c122-40a1-bf0d-153021043a4d', 'time_used': 376, 'faces': [{'face_rectangle': {'width': 82, 'top': 78, 'left': 60, 'height': 82}, 'face_token': '167b1cf6cd8b52093596f1327dc3c8e3'}]}
Turns out we can ask for lots of other information, e.g. about the facial expression.
attributes = ['gender',
'age',
'smiling',
'headpose',
'facequality',
'blur',
'eyestatus',
'emotion',
'ethnicity',
'beauty',
'mouthstatus',
'eyegaze',
'skinstatus'
]
params = {'image_url': target,
'return_attributes': ','.join(attributes),
}
response = requests.post(url, headers=headers, data=params)
response.json()
{'image_id': 'sAqOk498IudAZHDVDfWUJg==', 'request_id': '1553186533,491b9f79-18a5-460e-8f7f-3cd69cd713e5', 'time_used': 288, 'faces': [{'attributes': {'emotion': {'sadness': 0.0, 'neutral': 0.0, 'disgust': 0.008, 'anger': 0.002, 'surprise': 0.002, 'fear': 0.002, 'happiness': 99.987}, 'beauty': {'female_score': 67.194, 'male_score': 56.279}, 'gender': {'value': 'Male'}, 'age': {'value': 49}, 'mouthstatus': {'close': 0.01, 'surgical_mask_or_respirator': 0.0, 'open': 99.99, 'other_occlusion': 0.0}, 'glass': {'value': 'None'}, 'skinstatus': {'dark_circle': 3.044, 'stain': 40.645, 'acne': 20.696, 'health': 2.109}, 'headpose': {'yaw_angle': -13.445905, 'pitch_angle': 4.7359223, 'roll_angle': 11.515112}, 'blur': {'blurness': {'threshold': 50.0, 'value': 8.219}, 'motionblur': {'threshold': 50.0, 'value': 8.219}, 'gaussianblur': {'threshold': 50.0, 'value': 8.219}}, 'smile': {'threshold': 50.0, 'value': 100.0}, 'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.037, 'no_glass_eye_close': 0.0, 'occlusion': 0.002, 'no_glass_eye_open': 99.959, 'normal_glass_eye_close': 0.0, 'dark_glasses': 0.002}, 'right_eye_status': {'normal_glass_eye_open': 1.236, 'no_glass_eye_close': 0.0, 'occlusion': 0.0, 'no_glass_eye_open': 98.763, 'normal_glass_eye_close': 0.0, 'dark_glasses': 0.0}}, 'facequality': {'threshold': 70.1, 'value': 58.089}, 'ethnicity': {'value': 'WHITE'}, 'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.442, 'vector_z_component': 0.992, 'vector_x_component': -0.058, 'vector_y_component': 0.108, 'position_y_coordinate': 0.367}, 'left_eye_gaze': {'position_x_coordinate': 0.377, 'vector_z_component': 0.794, 'vector_x_component': -0.361, 'vector_y_component': -0.49, 'position_y_coordinate': 0.363}}}, 'face_rectangle': {'width': 130, 'top': 131, 'left': 80, 'height': 130}, 'face_token': '1512868d91c3e9ea2095207ae3ce9c85'}]}
# Don't execute this cell.
url = "https://imageclass.io/classify/seismic.json"
target = "https://dl.dropboxusercontent.com/u/14965965/seismic_example.png"
params = {'url': target,
'time': '600,1600', # time range in ms
'trace': '1000,1400', # trace range
'dx': '25', # trace spacing in m
'attribute': 'frequency,facies,resolution', # optional
}
headers={
"API_key": key,
"Accept": "application/json"
}
response = unirest.post(url, headers=headers, params=params)
response.body
{u'seismic': [{u'attribute': {u'f_peak': {u'range': 7, u'value': 39}, u'wavelet': {u'confidence': 89.932, u'value': u'ricker'}, u'phase': {u'error': 12.23421, u'value': 1.73568}, u'fresnel': {u'error': 400, u'value': 1750}, u'colourbar': {u'confidence': 98.53456, u'value': u'black-red'}}, u'facies': [{u'center': {u'x': 58.583333, u'y': 20.989305}, u'name': 'parallel', u'confidence': 76.45311}, {u'center': {u'x': 213.556323, u'y': 202.234543}, u'name': 'mounded', u'confidence': 67.3253245}, {u'center': {u'x': 458.346354, u'y': 120.345636}, u'name': 'noisy', u'confidence': 54.353466}], u'img_height': 387, u'img_id': u'524305ff333db315e583a4bdb5d03d0d', u'img_width': 620, u'session_id': u'928365a231974ae1a87ab51ca2066700', u'url': u'https://dl.dropboxusercontent.com/u/14965965/seismic_example.png'}
I found some code by endolith to do frequency estimation.
I got it working for seismic stuff in another notebook, Frequency_from_image. Then I set up a Flask
app on PythonAnywhere, which was surprisingly easy. It's serving at http://kwinkunks.pythonanywhere.com.
It's headless — it only accepts API calls. See below for an example.
Let's get the frequency content of the seismic in this image. I have already measured the pixel positions of region of the image that I want, in the time range 0 to 3 s, and the x range 8 to 16 km.
import requests
url = "http://kwinkunks.pythonanywhere.com/freq"
target = "https://math.berkeley.edu/~sethian/2006/Applications/Seismic/time_mig_img.jpg"
params = {'url': target,
'method': 'xing',
'tmin': 0.0,
'tmax': 3.0,
'region': '199,141,1608,1087',
'avg': 'trim'
}
headers={
"Accept": "application/json"
}
response = requests.get(url, headers=headers, params=params)
response.json()
{'job_uuid': 'eef5f2a0-4bf6-11e9-819f-0a84fd45cba2', 'parameters': {'avg': 'trim', 'method': 'xing', 'region': '199,141,1608,1087', 'time_range': [0.0, 3.0], 'trace_spacing': 'regular', 'traces': [128, 256, 384, 512, 640, 769, 897, 1025, 1153, 1281], 'url': 'https://math.berkeley.edu/~sethian/2006/Applications/Seismic/time_mig_img.jpg'}, 'result': {'freq': {'max': 157.57, 'min': 1.09, 'n': 10, 'peak': 20.3, 'sd': 1.12}, 'greyscale': True, 'histogram': {'bins': [-128.0, -99.66666666666667, -71.33333333333334, -43.0, -14.666666666666671, 13.666666666666657, 42.0, 70.33333333333331, 98.66666666666666, 127.0], 'counts': [147566, 166666, 248099, 325574, 155382, 89953, 69530, 54919, 75225]}, 'img_size': {'height': 1409, 'width': 946}, 'phase': {'avg': 3.35, 'n': 10, 'sd': 26.32}, 'snr': {'avg': 0.46, 'sd': 0.05}}}