|
1 |
| -# GoogleLens |
| 1 | +# GoogleLens |
| 2 | + |
| 3 | +GoogleLens is a Python library that allows you to perform image searches using Google Lens. You can upload images via URLs, file paths, or binary data and retrieve visual match results. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You can install the library via pip: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install GoogleLens |
| 11 | +``` |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- Upload images from a URL, file path, or binary data. |
| 16 | +- Extract raw text or structured visual match data from the Google Lens response. |
| 17 | +- Retrieve the main and similar visual match results, including titles, thumbnails, and page URLs. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Uploading Images and Retrieving Results |
| 22 | + |
| 23 | +#### Example 1: Upload image from URL |
| 24 | + |
| 25 | +```python |
| 26 | +from googlelens import GoogleLens |
| 27 | + |
| 28 | +# Initialize GoogleLens instance |
| 29 | +lens = GoogleLens() |
| 30 | + |
| 31 | +# Upload image from URL |
| 32 | +url = "https://example.com/sample-image.jpg" |
| 33 | +result_url = lens.upload_image(url) |
| 34 | + |
| 35 | +# Extract and print visual match results |
| 36 | +print("Results from URL search:") |
| 37 | +print(result_url.extract_visual_results()) |
| 38 | +``` |
| 39 | + |
| 40 | +#### Example 2: Upload image from file path |
| 41 | + |
| 42 | +```python |
| 43 | +from googlelens import GoogleLens |
| 44 | + |
| 45 | +# Initialize GoogleLens instance |
| 46 | +lens = GoogleLens() |
| 47 | + |
| 48 | +# Upload image from a file path |
| 49 | +file_path = "path/to/sample-image.jpg" |
| 50 | +result_file = lens.upload_image(file_path) |
| 51 | + |
| 52 | +# Extract and print visual match results |
| 53 | +print("Results from file search:") |
| 54 | +print(result_file.extract_visual_results()) |
| 55 | +``` |
| 56 | + |
| 57 | +#### Example 3: Upload image using bytes |
| 58 | + |
| 59 | +```python |
| 60 | +from googlelens import GoogleLens |
| 61 | + |
| 62 | +# Initialize GoogleLens instance |
| 63 | +lens = GoogleLens() |
| 64 | + |
| 65 | +# Read image as bytes |
| 66 | +with open("path/to/sample-image.jpg", "rb") as f: |
| 67 | + image_bytes = f.read() |
| 68 | + |
| 69 | +# Upload image using bytes |
| 70 | +result_bytes = lens.upload_image(image_bytes) |
| 71 | + |
| 72 | +# Extract and print visual match results |
| 73 | +print("Results from bytes search:") |
| 74 | +print(result_bytes.extract_visual_results()) |
| 75 | +``` |
| 76 | + |
| 77 | +### Extracting Raw Text |
| 78 | + |
| 79 | +You can also extract raw text from the Google Lens results, if available: |
| 80 | + |
| 81 | +```python |
| 82 | +# Extract raw text from the Google Lens response |
| 83 | +raw_text = result_url.extract_raw_text() |
| 84 | +print("Raw text extracted from the response:") |
| 85 | +print(raw_text) |
| 86 | +``` |
| 87 | + |
| 88 | +## API Reference |
| 89 | + |
| 90 | +### `GoogleLens` |
| 91 | + |
| 92 | +Main class to interact with Google Lens. |
| 93 | + |
| 94 | +- `upload_image(image_input: Union[str, bytes]) -> GoogleLensResults` |
| 95 | + - Uploads an image to Google Lens and returns the search results. |
| 96 | + - `image_input`: Can be a URL (str), file path (str), or binary data (bytes). |
| 97 | + |
| 98 | +### `GoogleLensResults` |
| 99 | + |
| 100 | +Handles the parsing and extraction of useful data from the Google Lens response. |
| 101 | + |
| 102 | +- `extract_visual_results() -> Dict[str, Union[None, Dict[str, str], List[Dict[str, str]]]]` |
| 103 | + - Extracts the visual match results including the main match and a list of similar matches. |
| 104 | + |
| 105 | +- `extract_raw_text() -> List[str]` |
| 106 | + - Extracts raw text from the Google Lens response, if available. |
| 107 | + |
| 108 | +## Contributing |
| 109 | + |
| 110 | +Feel free to submit issues or pull requests. Contributions are welcome! |
| 111 | + |
| 112 | +## License |
| 113 | + |
| 114 | +This project is licensed under the MIT License. |
0 commit comments