python - 如何在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))
解决方案
我猜你想要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))
推荐阅读
- ios - iphone 设备模拟器上看不到按钮的文本
- math - 从数字序列中获取公式
- python - 如何从原始电影镜头生成 5 个子数据集?
- python - KNeighborsRegressor 作为去噪算法
- firebase - firebase 使用 where 获取一些数据
- reactjs - React Hooks 和变量作用域
- javascript - 排序箭头留在数据表中
- javascript - 将 csv 转换为 json,然后在 javascript 中解析它
- javascript - Javascript - 当用户点击退出按钮时,确认框重定向用户
- git - 克隆,从 Github 中提取数据并将 repo 存储在 Flutter 本地