-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot_llm
More file actions
executable file
·272 lines (227 loc) · 7.09 KB
/
screenshot_llm
File metadata and controls
executable file
·272 lines (227 loc) · 7.09 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash
# Configuration
LLM_API_BASE="http://127.0.0.1:8080/v1"
LLM_MODEL="qwen3-vl-8b"
LLM_API_KEY="sk-no-key-required"
# Function to display usage
usage() {
echo "Usage: $0 <command> [args]"
echo "Commands:"
echo " zoom [prompt] Take a screenshot and zoom based on prompt"
echo " list List available models"
echo " help Show this help message"
exit 1
}
function list_models() {
curl -s "$LLM_API_BASE/models" | jq -r '.data[].id'
}
function take_screenshot() {
local output_file=$(mktemp --suffix=.png -p /dev/shm)
trap "rm -f -- '$output_file'" EXIT
echo "Taking screenshot..."
spectacle --current --background --nonotify -o "$output_file"
if [ ! -f "$output_file" ]; then
echo "Error: Failed to take screenshot."
exit 1
fi
echo "$output_file"
}
function query_vlm() {
local image_path="$1"
local prompt="$2"
local extra_json="$3"
local base64_image=$(base64 -w 0 "$image_path")
# Build basic structure using jq for safety
local payload=$(jq -n \
--arg model "$LLM_MODEL" \
--arg prompt "$prompt" \
--arg b64 "$base64_image" \
'{
model: $model,
messages: [
{
role: "user",
content: [
{ type: "text", text: $prompt },
{ type: "image_url", image_url: { url: ("data:image/png;base64," + $b64) } }
]
}
]
}')
if [ -n "$extra_json" ]; then
# Merge extra_json into payload
payload=$(echo "$payload" | jq -s '.[0] * .[1]' - <(echo "$extra_json"))
fi
echo "Calling LLM..." >&2
curl -s -X POST \
"$LLM_API_BASE/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LLM_API_KEY" \
-d "$payload"
}
function get_zoom_bbox() {
local image_path="$1"
local prompt="$2"
read -r -d '' tools_json << EOF
{
"tools": [
{
"type": "function",
"function": {
"name": "image_zoom_in_tool",
"description": "Crop and zoom in on specific regions of an image by cropping it based on a bounding box (bbox) and an optional object label",
"parameters": {
"type": "object",
"properties": {
"bbox_list": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bbox_2d": {
"type": "array",
"items": { "type": "number" },
"minItems": 4,
"maxItems": 4,
"description": "The bounding box of the region to zoom in, as [x1, y1, x2, y2], where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner relative coordinates in [0, 1000]."
},
"label": {
"type": "string",
"description": "Optional name or label of the object in the specified bounding box"
}
},
"required": ["bbox_2d"]
}
},
"img_idx": {
"type": "string",
"description": "The local uuid of input image"
}
},
"required": ["bbox_list", "img_idx"]
}
}
}
],
"tool_choice": {
"type": "function",
"function": {
"name": "image_zoom_in_tool"
}
}
}
EOF
local response=$(query_vlm "$image_path" "$prompt" "$tools_json")
if [ -z "$response" ]; then
echo "Error: Empty response from LLM." >&2
return 1
fi
local tool_calls=$(echo "$response" | jq -r '.choices[0].message.tool_calls[0].function.arguments')
if [ "$tool_calls" == "null" ] || [ -z "$tool_calls" ]; then
echo "LLM did not return a tool call for image_zoom_in_tool." >&2
echo "LLM Response: $response" >&2
return 1
fi
echo "$tool_calls"
}
function crop_and_zoom_image() {
local image_path="$1"
local tool_calls="$2"
# Parse bbox_list from TOOL_CALLS
local bbox_2d_array=$(echo "$tool_calls" | jq -r '.bbox_list[0].bbox_2d')
local label=$(echo "$tool_calls" | jq -r '.bbox_list[0].label // ""')
# Convert BBOX_2D_ARRAY to individual coordinates
local x1=$(echo "$bbox_2d_array" | jq -r '.[0]')
local y1=$(echo "$bbox_2d_array" | jq -r '.[1]')
local x2=$(echo "$bbox_2d_array" | jq -r '.[2]')
local y2=$(echo "$bbox_2d_array" | jq -r '.[3]')
# Get original image dimensions
local orig_width=$(identify -format "%w" "$image_path")
local orig_height=$(identify -format "%h" "$image_path")
# Convert relative coordinates (0-1000) to absolute pixels using integer arithmetic
local abs_x1=$(( ($x1 * orig_width) / 1000 ))
local abs_y1=$(( ($y1 * orig_height) / 1000 ))
local abs_x2=$(( ($x2 * orig_width) / 1000 ))
local abs_y2=$(( ($y2 * orig_height) / 1000 ))
local crop_width=$(echo "scale=0; $abs_x2 - $abs_x1" | bc -l)
local crop_height=$(echo "scale=0; $abs_y2 - $abs_y1" | bc -l)
# Ensure minimum crop size
local min_dim=32
if (( $(echo "$crop_width < $min_dim" | bc -l) )) || (( $(echo "$crop_height < $min_dim" | bc -l) )); then
echo "Warning: Bounding box is too small. Adjusting crop dimensions." >&2
crop_width=$min_dim
crop_height=$min_dim
fi
# Target pixels for resizing
local target_pixels=262144
local current_pixels=$(echo "scale=0; $crop_width * $crop_height" | bc -l)
local new_width=$crop_width
local new_height=$crop_height
if (( $(echo "$current_pixels < $target_pixels" | bc -l) )); then
local beta=$(echo "sqrt($target_pixels / ($crop_width * $crop_height))" | bc -l)
new_width=$(printf "%.0f" $(echo "scale=0; $crop_width * $beta" | bc -l))
new_height=$(printf "%.0f" $(echo "scale=0; $crop_height * $beta" | bc -l))
fi
# Round to nearest 32
new_width=$(printf "%.0f" $(echo "scale=0; ($new_width + 16) / 32 * 32" | bc -l))
new_height=$(printf "%.0f" $(echo "scale=0; ($new_height + 16) / 32 * 32" | bc -l))
# Ensure dimensions are at least 32x32
new_width=$((new_width > 32 ? new_width : 32))
new_height=$((new_height > 32 ? new_height : 32))
mkdir -p "$HOME/Pictures/zoomed_images"
local output_file="$HOME/Pictures/zoomed_images/$(sed 's/[[:space:]]\+/_/g' <<< "$label")_$(date +"%Y%m%d-%H%M%S").png"
echo "Cropping and resizing image..." >&2
magick convert "$image_path" -crop "${crop_width}x${crop_height}+${abs_x1}+${abs_y1}" \
-resize "${new_width}x${new_height}" \
-filter Lanczos \
"$output_file"
if [ ! -f "$output_file" ]; then
echo "Error: Failed to create zoomed image." >&2
return 1
fi
echo "$output_file"
}
function cmd_zoom() {
local prompt="$1"
local screenshot_temp_file=$(take_screenshot)
if [ -z "$prompt" ]; then
prompt=$(zenity --entry --text="Enter your input:" --title="Input")
fi
local tool_calls
tool_calls=$(get_zoom_bbox "$screenshot_temp_file" "$prompt")
if [ $? -ne 0 ]; then
return 1
fi
echo "LLM requested tool call: $tool_calls"
local zoomed_image
zoomed_image=$(crop_and_zoom_image "$screenshot_temp_file" "$tool_calls")
if [ $? -ne 0 ]; then
return 1
fi
echo "Zoomed image saved to $zoomed_image"
xdg-open "$zoomed_image"
# Demonstrate feeding the cropped image back into the LLM
echo "--------------------------------------------------"
echo "Demonstration: Feeding zoomed image back to LLM..."
echo "--------------------------------------------------"
local analysis=$(query_vlm "$zoomed_image" "Describe this zoomed image in detail.")
local content=$(echo "$analysis" | jq -r '.choices[0].message.content')
echo "LLM Analysis of Zoomed Image:"
echo "$content"
}
#######
# Main execution
COMMAND="$1"
shift
case "$COMMAND" in
zoom)
cmd_zoom "$@"
;;
list)
list_models
;;
help|*)
usage
;;
esac
#######