Last active
September 12, 2017 04:21
-
-
Save matmoody/377ecc65e3b5e688821b to your computer and use it in GitHub Desktop.
Live Face Recognition With Nest Cam And Project Oxford
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
class LiveFace(): | |
# Connect To Nest Cam REST Streaming API | |
def get_stream(self, cam_url): | |
# Manipulate the response so we can detect if motion was detected | |
DATA_PREFIX = 'data: ' | |
headers = {"Accept": "application/json", "Accept": "text/event-stream"} | |
response = requests.get(cam_url, headers=headers, allow_redirects=True, | |
stream=True) | |
for line in response.iter_lines(chunk_size=1): | |
if line.startswith(DATA_PREFIX): | |
data = line[len(DATA_PREFIX):] | |
if not data == 'null': | |
data_dict = json.loads(data) | |
# Check for motion event so it isn't triggered by sound alone. | |
if data_dict['data']['last_event']['has_motion'] == True: | |
image_url = data_dict['data']['last_event']['image_url'] | |
# Call request_recognition func with the url to the last motion event | |
self.request_recognition(image_url) | |
else: | |
print "Nothing New To Report." | |
# Send Most Recent Motion Event Image URL For Face Recognition | |
def request_recognition(self, image_url): | |
# We are going to request only Age and Gender attributes | |
url = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceAttributes=age,gender" | |
payload = '{"url":"%s"}' % image_url | |
headers = {"Ocp-Apim-Subscription-Key": "PROJECT_OXFORD_PRIMARY_KEY"} | |
response = requests.post(url, data=payload, headers=headers) | |
response = json.loads(response.content) | |
if response: | |
gender = response[0]["faceAttributes"]["gender"] | |
age = response[0]["faceAttributes"]["age"] | |
print "This person is a %s year old %s" % (age, gender) | |
else: | |
print "No person identified." | |
living_room = LiveFace() | |
living_room.get_stream("https://developer-api.nest.com/devices/cameras/YOUR_CAMERA_ID?auth=YOUR_ACCESS_TOKEN") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment