首页 > 解决方案 > 如何在python中循环?

问题描述

我有从照片中检索参数的代码。我需要把它放在循环中,这样它就会从文件夹中加载我所有的照片并用 Python 写下来`在此处输入代码

import requests

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

img_path = 'C:\\Users\\Pifko\\Desktop\\bakalarka\\subject13session2_Moment5up.jpg' 
img_data = open(img_path, 'rb').read()

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

标签: pythonpython-2.7cycle

解决方案


我猜你想要os.listdir

for  fname in os.listdir(basepath):
     if fname[-3:] in ["jpg","gif","png"]:
        print("this is an image:",os.path.join(basepath,name))

推荐阅读