Image Search (Google Lens)
Identify anything from an image using Google Lens via SerpAPI.
Setup
Requires SERPAPI_KEY environment variable. Get a key at https://serpapi.com/ (100 free searches/month).
No pip dependencies needed — uses only Python stdlib (urllib, json, base64).
Usage
From CLI / Agent (exec tool)
CODEBLOCK0
Search Types
| Type | Use Case | Returns |
|---|
| INLINECODE4 (default) | General identification | Entity name + visual matches + text |
| INLINECODE5 |
Find similar images | Visually similar results with sources |
|
exact_matches | Find image origin | Pages containing this exact image |
|
products | Shopping / price lookup | Products with prices and buy links |
|
about_this_image | Image provenance | Metadata about the image's origin |
Output Format
The script outputs structured markdown:
CODEBLOCK1
Use --json for raw SerpAPI response when you need thumbnails, image dimensions, or other metadata.
Agent Decision Guide
When a user sends an image:
- 1. Already identified by vision model? If the main model confidently recognizes the entity, skip reverse search.
- Uncertain identification? Run
lens_search.py to verify. Compare model's guess with Lens results. - Need details beyond identification? First identify with Lens, then
web_search for deeper info. - Shopping intent? Use
--type products to get prices and buy links directly. - Local file from user? The script handles local files by auto-uploading to get a searchable URL.
Combining with Other Tools
Typical multi-tool workflow:
CODEBLOCK2
Limitations
- - SerpAPI free tier: 100 searches/month. Paid plans from $50/month.
- Local file upload uses freeimage.host (free) or imgbb (needs IMGBBAPIKEY).
- Google Lens results vary by region; use
--country for localized results. - Some niche/long-tail entities may not return useful visual matches.
图片搜索 (Google Lens)
通过SerpAPI使用Google Lens识别图片中的任何内容。
环境配置
需要设置SERPAPI_KEY环境变量。在https://serpapi.com/获取密钥(每月100次免费搜索)。
无需pip依赖——仅使用Python标准库(urllib、json、base64)。
使用方法
从命令行/代理(执行工具)
bash
通过图片URL搜索
python3 {baseDir}/scripts/lens_search.py https://example.com/photo.jpg
通过本地文件搜索(自动上传获取URL)
python3 {baseDir}/scripts/lens_search.py /path/to/image.png
添加文字查询进行细化(例如,查找产品的红色版本)
python3 {baseDir}/scripts/lens_search.py https://example.com/bag.jpg --query red
商品搜索(返回价格)
python3 {baseDir}/scripts/lens_search.py https://example.com/sneakers.jpg --type products
查找完全匹配(该图片在网络上出现的位置)
python3 {baseDir}/scripts/lens
search.py https://example.com/photo.jpg --type exactmatches
程序化使用的原始JSON输出
python3 {baseDir}/scripts/lens_search.py https://example.com/photo.jpg --json
本地化结果(例如,日元标价的日本商品)
python3 {baseDir}/scripts/lens_search.py https://example.com/laptop.jpg --type products --country jp
搜索类型
| 类型 | 使用场景 | 返回结果 |
|---|
| all(默认) | 通用识别 | 实体名称 + 视觉匹配 + 文字 |
| visual_matches |
查找相似图片 | 视觉相似结果及来源 |
| exact_matches | 查找图片来源 | 包含该精确图片的页面 |
| products | 购物/价格查询 | 带价格和购买链接的商品 |
| about
thisimage | 图片溯源 | 关于图片来源的元数据 |
输出格式
脚本输出结构化Markdown:
识别实体
视觉匹配(前5个)
- - 丹尼·德维托 — 维基百科(维基百科)✅ 完全匹配
https://en.wikipedia.org/wiki/Danny_DeVito
当需要缩略图、图片尺寸或其他元数据时,使用--json获取原始SerpAPI响应。
代理决策指南
当用户发送图片时:
- 1. 视觉模型已识别? 如果主模型能自信识别实体,跳过反向搜索。
- 识别不确定? 运行lenssearch.py进行验证。将模型猜测与Lens结果对比。
- 需要识别之外的详细信息? 先用Lens识别,再用websearch获取深入信息。
- 购物意图? 使用--type products直接获取价格和购买链接。
- 用户提供本地文件? 脚本通过自动上传获取可搜索URL来处理本地文件。
与其他工具结合使用
典型的多工具工作流程:
- 1. 用户发送图片 → 这是什么建筑?
- reverseimagesearch → 识别出科隆大教堂
- web_search(科隆大教堂 历史 建筑) → 详细信息
- 结合视觉匹配和网络知识撰写回答
限制说明
- - SerpAPI免费套餐:每月100次搜索。付费套餐从每月50美元起。
- 本地文件上传使用freeimage.host(免费)或imgbb(需要IMGBBAPIKEY)。
- Google Lens结果因地区而异;使用--country获取本地化结果。
- 某些小众/长尾实体可能无法返回有用的视觉匹配结果。