コメントでのtetsunosukeさんのようにBase64でエンコードするやり方が分かりやすいように思いましたが、もとの質問者さんのコードを踏襲して、以下のコードでもいけました。
Python3
1import requests
2
3url = "https://aimaker.io/image/classification/api"
4
5# 以下のAPIキーはダミー
6apikey = "APIKEY1234"
7apiid = "4107"
8payload = {'apikey': apikey, 'id': apiid}
9
10image = "test.jpg"
11data = open(image, 'rb')
12file = {'file': data}
13
14res = requests.post(url, params=payload, files=file)
15print(res.json())
16
実行結果
sh
1$ python3 t3.py
2{'url': 'https://aimaker.io/uptmp/8645e500bf6754e5953e53c236c5cf24c45e4c68.jpg', 'labels': [{'score': 0.036, 'label': '味噌ラーメン'}, {'score': 0.03, 'label': '塩ラーメン'}, {'score': 0.918, 'label': '醤油ラーメン'}, {'score': 0.015, 'label': 'とんこつラーメン'}], 'state': 1}
3
このPOSTリクエストは実際には以下のように生成されたのですが、(localhost:8000
にダミーサーバーを立ち上げ)URI部分とMIMEマルチパート部分でパラメータが分かれていても、受け付けてくれるのですね。
POST /image/classification/api?apikey=APIKEY1234&id=4107 HTTP/1.1
Host: localhost:8000
User-Agent: python-requests/2.21.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 7792
Content-Type: multipart/form-data; boundary=4790562c38a018601c5d618ea72c0ac3
--4790562c38a018601c5d618ea72c0ac3
Content-Disposition: form-data; name="file"; filename="test.jpg"
...test.jpg の画像データ
--4790562c38a018601c5d618ea72c0ac3--