import requests
import os
url = 'http://10.10.10.100:5000/classification' # 自己想要请求的接口地址
def sendImg(img_path,img_type='image/jpeg'):
img_name=os.path.basename(img_path)
with open(img_path , "rb")as f_abs: # 以2进制方式打开图片
body = {
# 有些上传图片时可能会有其他字段,比如图片的时间什么的,这个根据自己的需要
'photo': (img_name, f_abs, img_type)
}
# 上传图片的时候,不使用data和json,用files
response = requests.post(url=url, files=body)
return response.text
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'gif', 'GIF', 'JPEG', 'jpeg' ])
#是否是可以检测的图片类型
def allowed_file(filename):
global basedir, orihtml, imageSize, labels, ALLOWED_EXTENSIONS
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
if __name__ == '__main__':
# 上传图片
check_path=r'./'
for root, dirs, files in os.walk(check_path):
for file in files:
file_path = os.path.join(root, file)
if allowed_file(file_path):
res = sendImg(file_path) # 调用sendImg方法
print(file_path,res)