Python使用Java实现以图搜图api接口

按图搜索淘宝商品(拍立淘) API 返回值说明

请求参数

请求参数:imgid=http://g-search3.alicdn.com/img/bao/uploaded/i4/O1CN01IDpcD81zHbpHs1YgT_!!2200811456689.jpg&cat=

参数说明:imgid:图片地址(支持淘宝或天猫图片地址,外部地址先调用上传图片(upload_img)接口,返回图片地址)
如:https://img.alicdn.com/imgextra/i3/15353738/TB2HDHAqN9YBuNjy0FfXXXIsVXa_!!15353738-0-beehive-scenes.jpg

响应参数       获取key和密钥

名称类型必须示例值描述

title

String0亲子装短袖t恤社会人衣服全家装一家三口母子母女纯棉夏装上衣潮宝贝标题

cid

Int050015374

list_type

String0外观相似宝贝列表类型

pic_url

String0//g-search3.alicdn.com/img/bao/uploaded/i4/TB26ry1rBsmBKNjSZFsXXaXSVXa_!!2628705716.jpg宝贝图片

promotion_price

String038.71优惠价

price

Float039.50价格

sales

Int07销量

num_iid

Bigint0575727312808宝贝ID

sample_id

Bigint01627115368商品风格标识ID

seller_nick

String0专属味道之dzw掌柜昵称

is_tmall

Bool0false

post_fee

String00.00物流费用

area

String0广东 东莞店铺所在地

detail_url

String0//item.taobao.com/item.htm?id=575727312808&ns=1#detail宝贝链接

java请求示例:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.net.URLConnection;

public class Example {
	private static String readAll(Reader rd) throws IOException {
		StringBuilder sb = new StringBuilder();
		int cp;
		while ((cp = rd.read()) != -1) {
			sb.append((char) cp);
		}
		return  sb.toString();
	}
	public static JSONObject postRequestFromUrl(String url, String body) throws IOException, JSONException {
		URL realUrl = new URL(url);
		URLConnection conn = realUrl.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.print(body);
		out.flush();
		InputStream instream = conn.getInputStream();
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));
			String jsonText = readAll(rd);
			JSONObject json = new JSONObject(jsonText);
			return json;
		} finally {
			instream.close();
		}
	}
	public static JSONObject getRequestFromUrl(String url) throws IOException, JSONException {
		URL realUrl = new URL(url);
		URLConnection conn = realUrl.openConnection();
		InputStream instream = conn.getInputStream();
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));
			String jsonText = readAll(rd);
			JSONObject json = new JSONObject(jsonText);
			return json;
		} finally {
			instream.close();
		}
	}
	public static void main(String[] args) throws IOException, JSONException {
		// 请求示例 url 默认请求参数已经URL编码处理
		String url = "https://api-gw.onebound.cn/taobao/item_search_img/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&imgid=http://g-search3.alicdn.com/img/bao/uploaded/i4/O1CN01IDpcD81zHbpHs1YgT_!!2200811456689.jpg&cat=";
		JSONObject json = getRequestFromUrl(url);
		System.out.println(json.toString());
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用JavaCV实现以图搜图的代码示例: ```java import org.bytedeco.javacpp.opencv_core.*; import org.bytedeco.javacpp.opencv_features2d.*; import org.bytedeco.javacpp.opencv_xfeatures2d.*; import org.bytedeco.javacpp.indexer.FloatRawIndexer; import org.bytedeco.javacpp.indexer.UByteRawIndexer; import java.io.File; import java.util.ArrayList; import java.util.List; import static org.bytedeco.javacpp.opencv_core.CV_32F; import static org.bytedeco.javacpp.opencv_core.CV_8U; import static org.bytedeco.javacpp.opencv_core.NORM_L2; import static org.bytedeco.javacpp.opencv_features2d.Features2d.drawKeypoints; import static org.bytedeco.javacpp.opencv_imgcodecs.imread; import static org.bytedeco.javacpp.opencv_imgcodecs.imwrite; import static org.bytedeco.javacpp.opencv_xfeatures2d.SIFT.create; public class ImageSearch { public static void main(String[] args) { // 加载原图并提取SIFT特征 Mat img1 = imread("path/to/image1.jpg"); Mat img1Gray = new Mat(); cvtColor(img1, img1Gray, COLOR_BGR2GRAY); MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); Mat descriptors1 = new Mat(); create().detectAndCompute(img1Gray, new Mat(), keypoints1, descriptors1); // 加载待搜索图像并提取SIFT特征 Mat img2 = imread("path/to/image2.jpg"); Mat img2Gray = new Mat(); cvtColor(img2, img2Gray, COLOR_BGR2GRAY); MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); Mat descriptors2 = new Mat(); create().detectAndCompute(img2Gray, new Mat(), keypoints2, descriptors2); // 匹配SIFT特征 MatOfDMatch matches = new MatOfDMatch(); DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED); matcher.match(descriptors1, descriptors2, matches); // 筛选距离最近的匹配点 float minDistance = Float.MAX_VALUE; DMatch[] matchArray = matches.toArray(); for (int i = 0; i < matchArray.length; i++) { float distance = matchArray[i].distance(); if (distance < minDistance) { minDistance = distance; } } List<DMatch> goodMatches = new ArrayList<>(); for (int i = 0; i < matchArray.length; i++) { if (matchArray[i].distance() <= 2 * minDistance) { goodMatches.add(matchArray[i]); } } // 绘制匹配结果 Mat result = new Mat(); drawMatches(img1, keypoints1, img2, keypoints2, new MatOfDMatch(goodMatches.toArray(new DMatch[0])), result); imwrite("path/to/result.jpg", result); } } ``` 以上代码中,我们使用JavaCV提供的SIFT算法对原图像和待搜索图像提取SIFT特征,然后使用FLANN匹配器进行匹配,筛选出距离最近的匹配点,并绘制匹配结果。注意,由于JavaCV并不支持直接绘制匹配点,因此我们需要使用OpenCV的C++接口中提供的drawMatches函数,将其转换为Mat类型后再进行绘制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佩奇搞IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值