Python Forum
save parameters an data from image to csv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: save parameters an data from image to csv (/thread-17224.html)



save parameters an data from image to csv - pifko22 - Apr-02-2019

my code goes through photos from a folder and lists their parameters (age, emotion). I only need emotion.

  So all the photos I have in my folder should be sent for analysis. The results then need to be saved in a file (preferably csv) in which one column will be the emotion that has been recognized by people (i.e. file name) and the other emotion recognized by Face API.

e.g. In the case of a photo in folder j, joy4 will look like this:

j / joy4 joy happiness (since the system recognized happiness, see appendix)

This is my code

j / joy4 joy of happiness



import requests
import os
import time
import pprint
#import csv


BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'
headers = {
    'Ocp-Apim-Subscription-Key': 'cccf20b44cb9445a8a4239ce324acfed',
    'Content-Type': 'application/octet-stream'
}
parameters = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,emotion'
}

def post_image(img_data):
    response = requests.post(BASE_URL, params=parameters,
                             headers=headers, data=img_data)
    try:
        return response.json()
    except:
        return None



img_path = '' #nastavit cestu ku priecinku s fotkami (napr. a, b, c)

files = os.listdir(img_path)

for file in files:
    file = os.path.join(img_path,file)
    print(file)
    img_data = open(file, 'rb').read()
    print(post_image(img_data))
    # pprint.pformat(post_image(img_data))
    time.sleep(3)
İmage