forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_test.py
More file actions
52 lines (49 loc) · 1.69 KB
/
img_test.py
File metadata and controls
52 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from os import path
import requests
import time
label_num = 6
def get_top_keys(key_list: list):
return key_list[0: min(label_num, len(key_list))]
def translate(query):
url = 'http://fanyi.youdao.com/translate'
data = {
"i": query, # 待翻译的字符串
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": "16081210430989",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_CLICKBUTTION"
}
res = requests.post(url, data=data).json()
return res['translateResult'][0][0]['tgt'] # 打印翻译后的结果
def img_formatter(res, filePath):
# 翻译英文标签为中文
time.sleep(3)
labels = [item['tag']['en'] for item in get_top_keys(res['result']['tags'])]
labels_cn = []
for label in labels:
label_cn = translate(label)
labels_cn.append(label_cn)
return {
'labels': labels_cn,
'property': '{' +
'name: ' + '\'' + path.split(filePath)[1] + '\'' + ', ' +
'ext: ' + '\'' + path.splitext(filePath)[-1][1:] + '\'' + ', ' +
'size: ' + '\'' + str(path.getsize(filePath)) + '\''+ ', ' +
'path: ' + '\'' + filePath + '\''
+ '}'
}
def img_tag(image_path: str):
api_key = 'acc_ec9b217a28c4e19'
api_secret = 'de16ce61cf5497198e70815b1104e6e7'
response = requests.post(
'https://api.imagga.com/v2/tags',
auth=(api_key, api_secret),
files={'image': open(image_path, 'rb')})
print(img_formatter(response.json(), image_path))
img_tag("/root/jfs/357.png")