百度大脑相似图片搜索

图片导入图片库

需要调整参数:1中为filepath【本地文件夹位置】

                         2中为host【官网获取的API Key】和【官网获取的Secret Key】和filepath【本地

                           文件夹位置】

1.遍历本地图片文件夹,并将图片按照大小排序

#coding=utf-8

from os import listdir
import os
import pandas as pd


#将文件夹内的文件名读进列表m
filepath=【本地文件夹位置】
filename_list=listdir(filepath)

h=[]


for filename in filename_list:#依次读入列表中的内容
    fileloc=filepath+'\\'+filename  #图片位置
    imagePath = os.path.join(fileloc)
    imageSize = os.path.getsize(imagePath)
    imageSize /= 1024 # 除以1024是代表Kb
    h.append({'图片名称':filename,'图片大小(kb)':imageSize})

# #可以同过简单后缀名判断,筛选出你所需要的文件(这里以.jpg为例)
# for filename in filename_list:#依次读入列表中的内容
#     if filename.endswith('jpg'):# 后缀名'jpg'匹对
#         h.append(filename)#如果是'jpg'文件就添加进列表h


#列表转为dataframe
imageinfo=pd.DataFrame(h)
imageinfo
imageinfo.sort_values(by='图片大小(kb)',ascending=False)#按照'图片大小(kb)'排序

2.图像上传

# encoding:utf-8
import requests
import base64
from os import listdir
import os

# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的API Key】&client_secret=【官网获取的Secret Key】'
response = requests.get(host)
if response:
    print(response.json()['access_token'])
    access_token=response.json()['access_token']

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add"
# 二进制方式打开图片文件

filepath=【本地文件夹位置】
filename_list=listdir(filepath)

#可以同过简单后缀名判断,筛选出你所需要的文件(这里以.jpg为例)
for filename in filename_list:#依次读入列表中的内容
    fileloc=filepath+'\\'+filename  #图片位置
    f = open(fileloc, 'rb')
    img = base64.b64encode(f.read())
    #配置参数并上传图片
    params = {"brief":filename,"image":img,"tags":"1,1"}#brief为概要,用图片名称
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print (response.json())

相似图从图片库中检索

参数:host【官网获取的API Key】和【官网获取的Secret Key】

           f中【图片位置】

# encoding:utf-8

import requests
import base64
import pandas as pd

'''
相似图检索—检索
'''
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的API Key】&client_secret=【官网获取的Secret Key】'
response = requests.get(host)
if response:
    print(response.json()['access_token'])
    access_token=response.json()['access_token']
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/search"
# 二进制方式打开图片文件
f = open(【图片位置】, 'rb')
img = base64.b64encode(f.read())
params = {"image":img,"pn":0,"rn":10}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
pd.DataFrame(response.json()['result'])

详情看百度智能云图像搜索API文档图像搜索 - 相似图片搜索 | 百度智能云文档 (baidu.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值