Skip to content

Commit b8fffeb

Browse files
authored
Add files via upload
1 parent c4c57bf commit b8fffeb

9 files changed

+479
-22
lines changed

LICENSE

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2024 Abol
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2024 Abolfazl Bagheri
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,114 @@
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.

examples/upload_file_example.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from googlelens import GoogleLens
2+
3+
def main():
4+
# Initialize the GoogleLens object
5+
lens = GoogleLens()
6+
7+
# Path to the local image file
8+
image_path = 'path_to_your_image.png'
9+
10+
# Upload the image and get results
11+
try:
12+
results = lens.upload_image(image_path)
13+
print("Image uploaded successfully!")
14+
15+
# Extract and print raw text
16+
raw_text = results.extract_raw_text()
17+
print("Raw extracted text:", raw_text)
18+
19+
# Extract and print visual results
20+
visual_results = results.extract_visual_results()
21+
print("Extracted visual results:", visual_results)
22+
23+
except Exception as e:
24+
print(f"Error during image upload: {e}")
25+
26+
if __name__ == '__main__':
27+
main()

examples/upload_url_example.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from googlelens import GoogleLens
2+
3+
def main():
4+
# Initialize the GoogleLens object
5+
lens = GoogleLens()
6+
7+
# URL of the image
8+
image_url = 'https://example.com/path_to_your_image.png'
9+
10+
# Upload the image from URL and get results
11+
try:
12+
results = lens.upload_image(image_url)
13+
print("Image uploaded from URL successfully!")
14+
15+
# Extract and print raw text
16+
raw_text = results.extract_raw_text()
17+
print("Raw extracted text:", raw_text)
18+
19+
# Extract and print visual results
20+
visual_results = results.extract_visual_results()
21+
print("Extracted visual results:", visual_results)
22+
23+
except Exception as e:
24+
print(f"Error during image upload: {e}")
25+
26+
if __name__ == '__main__':
27+
main()

googlelens/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .googlelens import GoogleLens, GoogleLensResults
2+
3+
__all__ = ['GoogleLens', 'GoogleLensResults']

0 commit comments

Comments
 (0)