daily pastebin goal
0%
SHARE
TWEET

Face Recognition with Project Oxford via Raspberry Pi

a guest Jun 5th, 2015 438 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import picamera
  3. import http.client, urllib.request, urllib.parse, urllib.error, base64
  4. import json
  5.  
  6.  
  7. def takePicture():
  8.     with picamera.PiCamera() as camera:
  9.         camera.resolution = (1024, 768)
  10.         camera.start_preview()
  11.         # Camera warm-up time
  12.         time.sleep(2)
  13.         camera.capture('foo.jpg')
  14.  
  15. def findFaces():
  16.     takePicture()
  17.        
  18.     headers = {
  19.        # Basic Authorization Sample
  20.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  21.        'Content-type': 'application/octet-stream',
  22.     }
  23.  
  24.     params = urllib.parse.urlencode({
  25.        # Specify your subscription key
  26.        'subscription-key': 'SUBSCRIPTION_KEY',
  27.        # Specify values for optional parameters, as needed
  28.        #'analyzesFaceLandmarks': 'false',
  29.        #'analyzesAge': 'yes',
  30.        #'analyzesGender': 'false',
  31.        #'analyzesHeadPose': 'false',
  32.     })
  33.  
  34.     body = ""
  35.     filename = 'foo.jpg'
  36.     f = open(filename, "rb")
  37.     body = f.read()
  38.     f.close()
  39.    
  40.     try:
  41.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  42.        conn.request("POST", "/face/v0/detections?%s" % params, body, headers)
  43.        response = conn.getresponse()
  44.        
  45.        data = response.read()
  46.        print(data)
  47.        conn.close()
  48.        data = json.loads(data.decode("utf-8"))
  49.        
  50.        return data
  51.     except Exception as e:
  52.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  53.  
  54. def identifyFace():
  55.    
  56.     headers = {
  57.        # Basic Authorization Sample
  58.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  59.        'Content-type': 'application/json',
  60.     }
  61.  
  62.     params = urllib.parse.urlencode({
  63.        # Specify your subscription key
  64.        'subscription-key': 'SUBSCRIPTION_KEY',
  65.     })
  66.  
  67.  
  68.     data = findFaces()
  69.     if len(data) < 5:
  70.         print("No face detected")
  71.  
  72.     faceIds = data[0]['faceId']
  73.     print(faceIds)
  74.     body = '{"faceIds":["' +faceIds + '"],"personGroupId":"sample_group","maxNumOfCandidatesReturned":3}'
  75.     print( body)
  76.  
  77.     personId = ""
  78.  
  79.     try:
  80.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  81.        conn.request("POST", "/face/v0/identifications?%s" % params, body, headers)
  82.        response = conn.getresponse()
  83.        
  84.        data = response.read()
  85.        print(data)
  86.        data = json.loads(data.decode("utf-8"))
  87.        print(data[0])
  88.        print(data[0]["candidates"])
  89.        print(data[0]["candidates"][0])
  90.        print(data[0]["candidates"][0]["personId"])
  91.        #for key, value in data[0]["candidates"][0] :
  92.        #     print(value)
  93.        personId = data[0]["candidates"][0]["personId"]
  94.        print("No fail")
  95.        
  96.        conn.close()
  97.     except Exception as e:
  98.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  99.  
  100.     headers = {
  101.        # Basic Authorization Sample
  102.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  103.        'Content-type': 'application/json',
  104.     }
  105.  
  106.     params = urllib.parse.urlencode({
  107.        # Specify your subscription key
  108.        'subscription-key': 'SUBSCRIPTION_KEY',
  109.     })
  110.  
  111.     try:
  112.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  113.        url = "/face/v0/personGroups/sample_group/persons/" +  personId
  114.        conn.request("GET", (url + "?%s") % params, "", headers)
  115.        response = conn.getresponse()
  116.        data = response.read()
  117.        data = json.loads(data.decode("utf-8"))
  118.        print("#################################")
  119.        print("#################################")
  120.        print("#################################")
  121.        print("#################################")
  122.        print("WELCOME " + data["name"])
  123.        conn.close()
  124.     except Exception as e:
  125.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  126.  
  127.  
  128.  
  129.    
  130. def createPerson():
  131.     headers = {
  132.        # Basic Authorization Sample
  133.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  134.        'Content-type': 'application/json',
  135.     }
  136.  
  137.     params = urllib.parse.urlencode({
  138.        # Specify your subscription key
  139.        'subscription-key': 'SUBSCRIPTION_KEY',
  140.        #'personGroupId':'sample_group'
  141.     })
  142.  
  143.    
  144.     body = '{"name":"sample_group","userData":"user-provided data attached to the person group"}'
  145.     print( body)
  146.  
  147.     try:
  148.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  149.        conn.request("POST", "/face/v0/persongroups/sample_group/persons?%s" % params, body, headers)
  150.        response = conn.getresponse()
  151.        data = response.read()
  152.        print(data)
  153.        conn.close()
  154.     except Exception as e:
  155.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  156.  
  157.  
  158. def getPersonGroup():
  159.     headers = {
  160.        # Basic Authorization Sample
  161.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  162.        'Content-type': 'application/json',
  163.     }
  164.  
  165.     params = urllib.parse.urlencode({
  166.        # Specify your subscription key
  167.        'subscription-key': 'SUBSCRIPTION_KEY',
  168.        #'personGroupId':'sample_group'
  169.     })
  170.  
  171.    
  172.     try:
  173.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  174.        conn.request("GET", "/face/v0/persongroups/sample_group?%s" % params, "", headers)
  175.        response = conn.getresponse()
  176.        data = response.read()
  177.        print(data)
  178.        conn.close()
  179.     except Exception as e:
  180.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  181.  
  182.  
  183.  
  184. def trainPersonGroup():
  185.     headers = {
  186.        # Basic Authorization Sample
  187.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  188.        'Content-type': 'application/json',
  189.     }
  190.  
  191.     params = urllib.parse.urlencode({
  192.        # Specify your subscription key
  193.        'subscription-key': 'SUBSCRIPTION_KEY',
  194.        #'personGroupId':'sample_group'
  195.     })
  196.  
  197.    
  198.     body = '{"name":"sample_group","userData":"user-provided data attached to the person group"}'
  199.     print( body)
  200.  
  201.     try:
  202.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  203.        conn.request("POST", "/face/v0/persongroups/sample_group/training?%s" % params, body, headers)
  204.        response = conn.getresponse()
  205.        data = response.read()
  206.        print(data)
  207.        conn.close()
  208.     except Exception as e:
  209.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  210.  
  211.  
  212. def getTrainingStatusPersonGroup():
  213.     headers = {
  214.        # Basic Authorization Sample
  215.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  216.        'Content-type': 'application/json',
  217.     }
  218.  
  219.     params = urllib.parse.urlencode({
  220.        # Specify your subscription key
  221.        'subscription-key': 'SUBSCRIPTION_KEY',
  222.     })
  223.  
  224.     try:
  225.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  226.        conn.request("GET", "/face/v0/persongroups/sample_group/training?%s" % params, body, headers)
  227.        response = conn.getresponse()
  228.        data = response.read()
  229.        print(data.decode("utf-8"))
  230.        conn.close()
  231.     except Exception as e:
  232.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  233.  
  234.        
  235. def addPersonToGroup():
  236.     headers = {
  237.        # Basic Authorization Sample
  238.        # 'Authorization': 'Basic %s' % base64.encodestring('{username}:{password}'),
  239.        'Content-type': 'application/json',
  240.     }
  241.  
  242.     params = urllib.parse.urlencode({
  243.        # Specify your subscription key
  244.        'subscription-key': 'SUBSCRIPTION_KEY',
  245.        #'personGroupId':'sample_group'
  246.     })
  247.  
  248.  
  249.     faceIds = ""
  250.     times = 3
  251.     while times > 0:
  252.         faceIds = faceIds + '"' + findFaces()[0]['faceId'] + '",'
  253.         times -= 1
  254.    
  255.        
  256.     body = '{"faceIds":[' +faceIds + '],"name":"Jakob","userData":"Jakob!"}'
  257.     print( body)
  258.  
  259.     try:
  260.        conn = http.client.HTTPSConnection('api.projectoxford.ai')
  261.        conn.request("POST", "/face/v0/persongroups/sample_group/persons?%s" % params, body, headers)
  262.        response = conn.getresponse()
  263.        data = response.read()
  264.        print(data)
  265.        conn.close()
  266.     except Exception as e:
  267.        print("[Errno {0}] {1}".format(e.errno, e.strerror))
  268.  
  269.  
  270. #addPersonToGroup()
  271. #createPersonGroup()
  272. #getPersonGroup()
  273. identifyFace()
  274. #trainPersonGroup()
  275. #getTrainingStatusPersonGroup()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top