-
Notifications
You must be signed in to change notification settings - Fork 3
/
lang_ocr.py
170 lines (135 loc) · 4.52 KB
/
lang_ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys
import subprocess
import openai
from PIL import Image
import pytesseract
import time
import os
import os
import pytesseract
from PIL import Image
import openai
import subprocess
# Path to image folder
image_dir = '/path/to/images'
# OpenAI API key
openai_api_key = 'your_key'
# Supported formats
formats = ['.jpg', '.png']
def ocr_image(filepath):
"""Run OCR on image and return text"""
image = Image.open(filepath)
text = pytesseract.image_to_string(image)
return text
def clean_text(text):
"""Clean up text for filename"""
text = text.strip().replace(' ', '_')
return text
def categorize(text):
"""Use GPT-3 to categorize text"""
# Call OpenAI API
...
def rename_file(filepath, text):
filename = clean_text(text)[:50] + '.jpg'
new_path = os.path.join(image_dir, filename)
os.rename(filepath, new_path)
def add_metadata(filepath, text, category):
"""Add tags and comments"""
tags = category
comments = text
# Call xattr to set tags and comments
...
for filename in os.listdir(image_dir):
filepath = os.path.join(image_dir, filename)
if os.path.splitext(filename)[1].lower() in formats:
text = ocr_image(filepath)
category = categorize(text)
rename_file(filepath, text)
add_metadata(filepath, text, category)
# Remove special characters, whitespace etc
new_name = cleanup_text(text)
# Limit length
new_name = new_name[:50]
# Path to the folder where screenshots are saved
screenshot_folder = '/Users/david/Desktop/Screenshots_Automate'
for filename in os.listdir(image_dir):
filepath = os.path.join(image_dir, filename)
# Supported image formats
SUPPORTED_FORMATS = ['.jpg', '.jpeg', '.png', '.tiff', '.bmp']
# OpenAI API key (replace with your actual key)
openai_api_key = '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'
# Error handling decorator
def handle_errors(func):
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"Error: {e}")
return None
return inner
@handle_errors
def load_image(image_path):
"""Load an image based on its extension and return an image object."""
ext = os.path.splitext(image_path)[1].lower()
if ext in SUPPORTED_FORMATS:
image = Image.open(image_path)
else:
raise ValueError("Unsupported image format")
return image
@handle_errors
def get_text_with_ocr(image):
"""Use Tesseract OCR to extract text from an image."""
text = pytesseract.image_to_string(image)
if not text:
raise Exception("No text found in the image")
return text
@handle_errors
def categorize(text):
"""Use OpenAI GPT-3 to categorize text."""
openai.api_key = openai_api_key
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt="Categorize and tag this image description: " + text,
max_tokens=50
)
return response.choices[0].text.strip()
except Exception as e:
print(f"Error in GPT-3 request: {e}")
return None
def set_tags_and_comments(image_path, tags, comments):
try:
# Set tags using `xattr`
subprocess.run(['xattr', '-w', 'com.apple.metadata:_kMDItemUserTags', tags, image_path])
# Set comments using `xattr`
subprocess.run(['xattr', '-w', 'com.apple.metadata:kMDItemFinderComment', comments, image_path])
except Exception as e:
print(f"Error setting tags and comments: {e}")
def process_screenshot(file_path):
# Load image
image = load_image(file_path)
if not image:
return
# Extract text using OCR
text = get_text_with_ocr(image)
if not text:
return
# Classify text
category = categorize(text)
if not category:
return
text = get_text_with_ocr(filepath)
# Set tags and comments
set_tags_and_comments(file_path, category, "Your comments here")
set_tags_and_comments(filepath, text, "Comments")
def monitor_folder():
while True:
for file_name in os.listdir(screenshot_folder):
file_path = os.path.join(screenshot_folder, file_name)
if os.path.isfile(file_path) and file_name.lower().endswith(tuple(SUPPORTED_FORMATS)):
process_screenshot(file_path)
os.rename(filepath, os.path.join(image_dir, new_name))
#os.remove(file_path) #Optionally, delete the processed screenshot
time.sleep(1) # Adjust the sleep time as needed
if __name__ == "__main__":
monitor_folder()