Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6be6594

Browse files
committed
2 parents d16b893 + 7266632 commit 6be6594

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

‎taiyangxue/background/app.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
import base64
3+
from bs4 import BeautifulSoup as BS
4+
import baiduapi as bd
5+
import httpx
6+
from PIL import Image
7+
import io
8+
import difflib
9+
import datetime
10+
11+
12+
def grabImage(file=None):
13+
if file:
14+
image = Image.open(file)
15+
output_buffer = io.BytesIO()
16+
image.save(output_buffer, format='JPEG')
17+
return output_buffer.getvalue()
18+
else:
19+
# 获取并保存图片 直接从必应上获取
20+
rsp = httpx.get("https://cn.bing.com/")
21+
bs = BS(rsp.content, "html.parser")
22+
bglink = bs.find("link").get("href")
23+
url = str(rsp.url) + bglink
24+
25+
image = httpx.get(url).content
26+
return image
27+
28+
def isINeed(image):
29+
# # 压缩图片
30+
img = Image.open(io.BytesIO(image))
31+
x, y = img.size
32+
x_s = round(x/2)
33+
y_s = int(y * x_s / x)
34+
out = img.resize((x_s, y_s), Image.ANTIALIAS)
35+
36+
# 图片转码
37+
output_buffer = io.BytesIO()
38+
out.save(output_buffer, format='JPEG')
39+
out.save(r"D:\abc.jpg")
40+
byte_data = output_buffer.getvalue()
41+
# 图片识别
42+
result = bd.imageRecognition(byte_data)
43+
print("result:", result)
44+
# 结果分析
45+
46+
## 计算特征
47+
keywords = ['植物', '树', '天空', '阳光','霞光', '晚霞','海洋','大海','森林','湖泊','草原','沙漠','高山','瀑布']
48+
score = 0
49+
for r in result:
50+
# 进行对比
51+
for k in keywords:
52+
root = r.get('keyword', '')
53+
ratio = difflib.SequenceMatcher(None, root, k).ratio()
54+
mscore = r.get('score')
55+
score += mscore*ratio
56+
print(" text:%s\t vs kwd:%s\tmscore:%f\tratio:%f\tresult:%f" % (root, k, mscore, ratio, mscore*ratio))
57+
return score
58+
59+
def run(test=False):
60+
filename = None
61+
if test:
62+
filename = r'C:\Users\alisx\Pictures\Saved Pictures1032781円.jpg'
63+
64+
image = grabImage(filename)
65+
score = isINeed(image)
66+
if score > 0.5:
67+
with open(r"C:\Users\alisx\Pictures\Saved Pictures\bing_%s.jpg" % datetime.date.today(), 'wb') as f:
68+
f.write(image)
69+
70+
if __name__ == '__main__':
71+
run()
72+

‎taiyangxue/background/baiduapi.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import httpx
2+
import base64
3+
4+
def getAccessToken():
5+
# client_id 为官网获取的AK, client_secret 为官网获取的SK
6+
clientId = '2r...Yq' # 换成你的
7+
clientSecret = 'd6...Dd' # 换成你的
8+
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s' % (clientId, clientSecret)
9+
response = httpx.get(host)
10+
if response.status_code == 200:
11+
ret = response.json()
12+
return ret.get('access_token')
13+
else:
14+
raise "获取AccessToken失败:" + str(response.status_code)
15+
16+
def imageRecognition(image):
17+
img = base64.b64encode(image)
18+
params = {"image":img}
19+
access_token = getAccessToken()
20+
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=" + access_token
21+
headers = {'content-type': 'application/x-www-form-urlencoded'}
22+
response = httpx.post(request_url, data=params, headers=headers)
23+
if response.status_code == 200:
24+
return response.json().get("result")
25+
else:
26+
raise "获取AccessToken失败:" + str(response.status_code)
27+
28+
if __name__ == "__main__":
29+
# print(getAccessToken())
30+
imagefilepath = r'C:\Users\alisx\Pictures\road.jpg'
31+
with open(imagefilepath,"rb") as f:
32+
print(imageRecognition(f.read()))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /