vision-electronic-indexing-pi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,481 @@
1
+ # Vision Electronic Indexing MCP + Pi Workflow
2
+
3
+ This project turns electronics / PCB photos into a structured inventory workflow:
4
+
5
+ ```text
6
+ Image is taken -> saved in a folder -> Pi processes each photo -> raw IC data is extracted
7
+ -> agent/user verifies datasheets -> enriched inventory is exported to CSV
8
+ ```
9
+
10
+ The core vision step is a local Python MCP server that sends images to Cloudflare Workers AI. A project-local Pi extension bridges that MCP server into native Pi tools. A batch script implements the folder-to-CSV workflow with **agent-assisted datasheet enrichment**.
11
+
12
+ ## What this project does
13
+
14
+ - Processes one electronics image or a folder of images.
15
+ - Extracts visible IC/package markings, confidence, position hints, and review flags.
16
+ - Runs a second IC consensus pass when multiple ICs are detected in one image.
17
+ - Preserves individual IC marking observations, because the model may read one chip correctly and another incorrectly.
18
+ - Saves raw JSON for auditability.
19
+ - Creates `parts_to_lookup.json` for datasheet enrichment.
20
+ - Produces a final CSV, using `datasheet_cache.json` when enrichment is available.
21
+
22
+ ## What this project intentionally does not fully automate
23
+
24
+ Datasheet lookup is intentionally **Option A: agent-assisted enrichment**.
25
+
26
+ The script prepares the parts that need lookup, but it does not silently scrape the web and trust the result. Instead, Pi or a human should verify each part against a datasheet, fill `datasheet_cache.json`, and rerun the script to generate the final enriched CSV.
27
+
28
+ This makes wrong OCR or wrong datasheet matches easier to catch.
29
+
30
+ ## Repository layout
31
+
32
+ ```text
33
+ vision_inventory_mcp.py # MCP server and core image-processing logic
34
+ inventory.py # Original GUI/helper app
35
+ requirements.txt # Python dependencies
36
+ scripts/inventory_folder_to_csv.py # Batch workflow script
37
+ .pi/extensions/vision-inventory-mcp/ # Project-local Pi extension
38
+ ```
39
+
40
+ ## Requirements
41
+
42
+ Python 3.10 or newer is recommended.
43
+
44
+ Install dependencies:
45
+
46
+ ```bash
47
+ python3 -m pip install -r requirements.txt
48
+ ```
49
+
50
+ Optional HEIC/HEIF support for iPhone photos:
51
+
52
+ ```bash
53
+ python3 -m pip install pillow-heif
54
+ ```
55
+
56
+ ## Cloudflare setup
57
+
58
+ Create a `.env` file in the project root:
59
+
60
+ ```env
61
+ CLOUDFLARE_ACCOUNT_ID=your_cloudflare_account_id
62
+ CLOUDFLARE_AUTH_TOKEN=your_cloudflare_workers_ai_token
63
+ ```
64
+
65
+ The token can also be provided as:
66
+
67
+ ```env
68
+ CLOUDFLARE_API_TOKEN=your_cloudflare_workers_ai_token
69
+ ```
70
+
71
+ Default Cloudflare Workers AI model:
72
+
73
+ ```text
74
+ @cf/meta/llama-4-scout-17b-16e-instruct
75
+ ```
76
+
77
+ Override it with:
78
+
79
+ ```bash
80
+ export WORKERS_AI_MODEL=@cf/meta/llama-4-scout-17b-16e-instruct
81
+ ```
82
+
83
+ ## Pi package / extension setup
84
+
85
+ This repo is also a Pi package. Recommended install:
86
+
87
+ ```bash
88
+ pi install git:github.com/<you>/<this-repo>
89
+ ```
90
+
91
+ For local development, open this repo in Pi and trust the project. Pi should auto-discover:
92
+
93
+ ```text
94
+ .pi/extensions/vision-inventory-mcp/index.ts
95
+ .pi/skills/vision-inventory-workflow/SKILL.md
96
+ .pi/prompts/vision-inventory-agent-bom.md
97
+ ```
98
+
99
+ If Pi is already running, use `/reload` or restart Pi.
100
+
101
+ The package intentionally does **not** bundle these external dependencies:
102
+
103
+ - Python dependencies from `requirements.txt` (`mcp`, `requests`, `pillow`, `python-dotenv`; optional `pillow-heif`).
104
+ - A Pi web-search/browser tool or skill for datasheet enrichment.
105
+ - Cloudflare Workers AI credentials.
106
+
107
+ Run setup once:
108
+
109
+ ```text
110
+ /vision-inventory-setup
111
+ ```
112
+
113
+ The setup command prompts for Cloudflare credentials the first time and stores them at:
114
+
115
+ ```text
116
+ ~/.pi/agent/vision-inventory/credentials.json
117
+ ```
118
+
119
+ The file is written with `chmod 600` when supported. To change credentials later:
120
+
121
+ ```text
122
+ /vision-inventory-credentials
123
+ ```
124
+
125
+ The extension exposes these tools:
126
+
127
+ | Pi tool | Purpose |
128
+ |---|---|
129
+ | `vision_inventory_process_image` | Analyze one electronics image. |
130
+ | `vision_inventory_process_folder` | Analyze all supported images in a folder. |
131
+ | `vision_inventory_save` | Save inventory output as JSON or CSV. |
132
+
133
+ It also adds these commands:
134
+
135
+ ```text
136
+ /vision-inventory-setup
137
+ /vision-inventory-credentials
138
+ /vision-inventory-restart
139
+ /vision-inventory-bom <image_folder> <output_dir> [options]
140
+ /vision-inventory-agent-bom <image_folder> <output_dir> [options]
141
+ ```
142
+
143
+ Use `/vision-inventory-restart` if the MCP bridge needs to be restarted after changing credentials or Python code.
144
+
145
+ Use `/vision-inventory-bom` to run only the deterministic folder-to-CSV workflow from Pi. Examples:
146
+
147
+ ```text
148
+ /vision-inventory-bom ./photos ./output
149
+ /vision-inventory-bom ./photos ./output --recursive
150
+ /vision-inventory-bom ./photos ./output --skip-vision
151
+ ```
152
+
153
+ Options are forwarded to `scripts/inventory_folder_to_csv.py`.
154
+
155
+ Use `/vision-inventory-agent-bom` for the full agent-operated procedure: vision processing, reading `parts_to_lookup.json`, web/datasheet enrichment, writing `datasheet_cache.json`, rerunning with `--skip-vision`, and summarizing uncertain rows:
156
+
157
+ ```text
158
+ /vision-inventory-agent-bom ./photos ./output
159
+ /vision-inventory-agent-bom ./photos ./output --recursive
160
+ ```
161
+
162
+ Datasheet enrichment requires an installed/enabled web-search or browser tool/skill. This package explicitly does not bundle one.
163
+
164
+ The extension launches:
165
+
166
+ ```bash
167
+ python3 vision_inventory_mcp.py
168
+ ```
169
+
170
+ If your Python command is different, start Pi with:
171
+
172
+ ```bash
173
+ PI_VISION_INVENTORY_PYTHON=python pi
174
+ ```
175
+
176
+ ## Supported image formats
177
+
178
+ ```text
179
+ .jpg
180
+ .jpeg
181
+ .png
182
+ .webp
183
+ .bmp
184
+ .gif
185
+ .heic
186
+ .heif
187
+ ```
188
+
189
+ HEIC/HEIF requires `pillow-heif`.
190
+
191
+ ## Image preprocessing
192
+
193
+ Before sending an image to Cloudflare Workers AI, the server:
194
+
195
+ 1. Opens the image with Pillow.
196
+ 2. Applies EXIF orientation correction.
197
+ 3. Resizes only if the image is larger than `max_side`.
198
+ 4. Converts transparency to a white background.
199
+ 5. Converts the image to RGB.
200
+ 6. Encodes it as JPEG.
201
+ 7. Sends it as a base64 image data URL.
202
+
203
+ Current default image settings:
204
+
205
+ ```text
206
+ max_side: 4000
207
+ jpeg_quality: 96
208
+ ```
209
+
210
+ ## IC consensus behavior
211
+
212
+ For this lab workflow, the program assumes that all ICs visible in one image should be the same part family/marking.
213
+
214
+ The processing flow is:
215
+
216
+ 1. First pass: general visible inventory extraction.
217
+ 2. If multiple ICs are found, second pass: IC-only consensus verification.
218
+ 3. Final output includes:
219
+ - `items`: one consensus IC item when possible.
220
+ - `ic_marking_observations`: per-chip marking observations.
221
+ - `first_pass_items`: original first-pass IC candidates.
222
+ - `warnings`: notes about consensus verification.
223
+
224
+ This does not guarantee correct OCR. It helps expose uncertainty and preserves alternate readings.
225
+
226
+ ## Main batch workflow
227
+
228
+ Use this when you have a folder of newly taken images and want a final CSV.
229
+
230
+ From Pi, you can run the workflow as a slash command:
231
+
232
+ ```text
233
+ /vision-inventory-bom ./photos ./output
234
+ ```
235
+
236
+ From a normal shell, run the underlying script directly as shown below.
237
+
238
+ ### 1. Put images in a folder
239
+
240
+ Example:
241
+
242
+ ```text
243
+ photos/
244
+ image_001.jpeg
245
+ image_002.jpeg
246
+ image_003.jpeg
247
+ ```
248
+
249
+ ### 2. Run the batch workflow
250
+
251
+ ```bash
252
+ python3 scripts/inventory_folder_to_csv.py ./photos ./output
253
+ ```
254
+
255
+ This creates:
256
+
257
+ ```text
258
+ output/
259
+ raw/ # one raw JSON result per image
260
+ parts_to_lookup.json # parts/evidence that need datasheet lookup
261
+ datasheet_cache.template.json # template for enrichment
262
+ inventory.csv # deduplicated BOM CSV, possibly with missing enrichment fields
263
+ inventory_evidence.csv # per-image evidence CSV used to build the BOM
264
+ ```
265
+
266
+ Use recursive folder scanning if needed:
267
+
268
+ ```bash
269
+ python3 scripts/inventory_folder_to_csv.py ./photos ./output --recursive
270
+ ```
271
+
272
+ Limit images during testing:
273
+
274
+ ```bash
275
+ python3 scripts/inventory_folder_to_csv.py ./photos ./output --limit 3
276
+ ```
277
+
278
+ ### 3. Enrich datasheets with Pi/web search
279
+
280
+ Open `output/parts_to_lookup.json`. For each part, search for the datasheet and verify the description.
281
+
282
+ Prefer official manufacturer datasheets when possible:
283
+
284
+ 1. Texas Instruments
285
+ 2. Analog Devices / Maxim
286
+ 3. STMicroelectronics
287
+ 4. ONsemi
288
+ 5. Nexperia
289
+ 6. Other manufacturer PDFs
290
+
291
+ Copy the template:
292
+
293
+ ```bash
294
+ cp output/datasheet_cache.template.json output/datasheet_cache.json
295
+ ```
296
+
297
+ Fill each entry. Example:
298
+
299
+ ```json
300
+ {
301
+ "SN74LS283N": {
302
+ "normalized_part": "SN74LS283N",
303
+ "description": "74ls (4 bit) adder low power schottky ttl 5v DIP",
304
+ "datasheet_url": "https://www.ti.com/lit/ds/symlink/sn74ls283.pdf",
305
+ "manufacturer": "Texas Instruments",
306
+ "verified": true,
307
+ "notes": "Verified against TI datasheet. N package is PDIP."
308
+ },
309
+ "MAX232N": {
310
+ "normalized_part": "MAX232N",
311
+ "description": "MAX232N dual EIA-232/RS-232 driver receiver 5v DIP",
312
+ "datasheet_url": "https://www.ti.com/lit/ds/symlink/max232.pdf",
313
+ "manufacturer": "Texas Instruments",
314
+ "verified": true,
315
+ "notes": "Verified against TI datasheet."
316
+ }
317
+ }
318
+ ```
319
+
320
+ Recommended Pi prompt for enrichment:
321
+
322
+ ```text
323
+ Read output/parts_to_lookup.json. For each part, web-search the datasheet, prefer official manufacturer PDFs, verify function/package/voltage/family, then fill output/datasheet_cache.json. Keep descriptions in this short format: "74ls (4 bit) adder low power schottky ttl 5v DIP". Set verified=false if uncertain.
324
+ ```
325
+
326
+ ### 4. Regenerate CSV without reprocessing images
327
+
328
+ After filling `datasheet_cache.json`, rerun:
329
+
330
+ ```bash
331
+ python3 scripts/inventory_folder_to_csv.py ./photos ./output --skip-vision
332
+ ```
333
+
334
+ This reuses `output/raw/*.json` and writes a new enriched:
335
+
336
+ ```text
337
+ output/inventory.csv
338
+ ```
339
+
340
+ ## Final CSV columns
341
+
342
+ By default, `inventory.csv` is deduplicated by normalized part number. Multiple images with the same IC become one BOM row with `sighting_count` and an `images` list.
343
+
344
+ ```text
345
+ normalized_part
346
+ candidate_parts
347
+ amount
348
+ sighting_count
349
+ description
350
+ datasheet_url
351
+ manufacturer
352
+ verified
353
+ vision_confidence
354
+ needs_review
355
+ images
356
+ observed_markings
357
+ raw_json
358
+ notes
359
+ ```
360
+
361
+ Example BOM row:
362
+
363
+ ```csv
364
+ SN74LS283N,SN74LS283N,8,2,74ls (4 bit) adder low power schottky ttl 5v DIP,https://www.ti.com/lit/ds/symlink/sn74ls283.pdf,Texas Instruments,true,high/low,true,"image_001.jpeg | image_002.jpeg","SN74LS283N | SN74S283N","output/raw/image_001.json | output/raw/image_002.json","Verified against TI datasheet"
365
+ ```
366
+
367
+ The script also writes `inventory_evidence.csv`, which keeps the non-deduplicated per-image rows used to build the BOM. It includes the same per-sighting `amount` estimate before aggregation.
368
+
369
+ `amount` is estimated from the vision result's IC count. `sighting_count` is the number of image-level sightings that were merged into the BOM row.
370
+
371
+ ## MCP server usage
372
+
373
+ Run the MCP server directly with:
374
+
375
+ ```bash
376
+ python3 vision_inventory_mcp.py
377
+ ```
378
+
379
+ The server uses MCP `stdio` transport, so it is meant to be launched by an MCP-compatible client or by the Pi extension.
380
+
381
+ ### MCP tools
382
+
383
+ | MCP tool | Purpose |
384
+ |---|---|
385
+ | `process_image` | Analyze one image and return visible inventory data. |
386
+ | `process_image_folder` | Analyze all supported images in a folder. |
387
+ | `save_inventory` | Save inventory output to JSON or CSV. |
388
+
389
+ ### Example MCP client configuration
390
+
391
+ ```json
392
+ {
393
+ "mcpServers": {
394
+ "vision-inventory": {
395
+ "command": "python3",
396
+ "args": ["/path/to/vision_inventory_mcp.py"],
397
+ "env": {
398
+ "CLOUDFLARE_ACCOUNT_ID": "your_cloudflare_account_id",
399
+ "CLOUDFLARE_AUTH_TOKEN": "your_cloudflare_workers_ai_token"
400
+ }
401
+ }
402
+ }
403
+ }
404
+ ```
405
+
406
+ ## Single-image output example
407
+
408
+ ```json
409
+ {
410
+ "image": "image_001.jpeg",
411
+ "items": [
412
+ {
413
+ "item_type": "IC",
414
+ "count_index": 1,
415
+ "package_marking": "SN74LS283N",
416
+ "marking_confidence": "medium",
417
+ "likely_part": "SN74LS283N",
418
+ "description": "consensus result; 4 visible ICs",
419
+ "position_hint": "multiple ICs",
420
+ "needs_review": true
421
+ }
422
+ ],
423
+ "warnings": [
424
+ "Multi-pass IC consensus verification applied."
425
+ ],
426
+ "ic_marking_observations": [
427
+ {
428
+ "position_hint": "top-right",
429
+ "package_marking": "SN74LS283N F 7936",
430
+ "marking_confidence": "high"
431
+ }
432
+ ],
433
+ "first_pass_items": []
434
+ }
435
+ ```
436
+
437
+ ## Error handling
438
+
439
+ The server returns structured errors when possible:
440
+
441
+ ```json
442
+ {
443
+ "error": true,
444
+ "message": "Missing CLOUDFLARE_ACCOUNT_ID environment variable."
445
+ }
446
+ ```
447
+
448
+ Handled cases include:
449
+
450
+ - Missing Cloudflare credentials.
451
+ - Invalid image path.
452
+ - Unsupported image extension.
453
+ - Failed image preprocessing.
454
+ - Cloudflare API errors.
455
+ - Invalid JSON from the model.
456
+ - Missing or invalid folder path.
457
+ - Save/write failures.
458
+
459
+ ## Important limitations
460
+
461
+ - Vision models can misread small or blurry IC markings.
462
+ - A higher-resolution or closer photo usually helps more than prompt changes.
463
+ - Full-board photos are useful for context; cropped IC close-ups are better for marking OCR.
464
+ - The consensus pass can enforce one shared IC result, but it can still choose the wrong consensus.
465
+ - Datasheet enrichment should be verified against official sources.
466
+ - The script does not deduplicate the same physical part across multiple images unless you handle that in the enrichment/review step.
467
+
468
+ ## Recommended operating procedure
469
+
470
+ 1. Install the Pi package/extension and run `/vision-inventory-setup` once.
471
+ 2. Take a clear photo of each IC group.
472
+ 3. Save photos into a dated folder.
473
+ 4. Run the full agent workflow:
474
+
475
+ ```text
476
+ /vision-inventory-agent-bom ./photos ./output
477
+ ```
478
+
479
+ 5. Review `inventory.csv`, especially rows with `needs_review=true` or `verified=false`.
480
+
481
+ Manual fallback remains available with `scripts/inventory_folder_to_csv.py` and `/vision-inventory-bom`.
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "vision-electronic-indexing-pi",
3
+ "version": "0.1.0",
4
+ "description": "Pi package for agent-assisted electronics/PCB image inventory with Cloudflare Workers AI vision and datasheet enrichment.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Pichi-Cell/vision-electronic-indexing-mcp.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/Pichi-Cell/vision-electronic-indexing-mcp/issues"
12
+ },
13
+ "homepage": "https://github.com/Pichi-Cell/vision-electronic-indexing-mcp#readme",
14
+ "keywords": [
15
+ "pi-package",
16
+ "pi",
17
+ "electronics",
18
+ "pcb",
19
+ "inventory",
20
+ "bom",
21
+ "vision",
22
+ "cloudflare-workers-ai"
23
+ ],
24
+ "files": [
25
+ "LICENSE",
26
+ "README.md",
27
+ "requirements.txt",
28
+ "vision_inventory_mcp.py",
29
+ "scripts/inventory_folder_to_csv.py",
30
+ ".pi/extensions/vision-inventory-mcp/README.md",
31
+ ".pi/extensions/vision-inventory-mcp/index.ts",
32
+ ".pi/skills/vision-inventory-workflow/SKILL.md",
33
+ ".pi/prompts/vision-inventory-agent-bom.md"
34
+ ],
35
+ "peerDependencies": {
36
+ "@earendil-works/pi-ai": "*",
37
+ "@earendil-works/pi-coding-agent": "*",
38
+ "typebox": "*"
39
+ },
40
+ "pi": {
41
+ "extensions": [".pi/extensions/vision-inventory-mcp/index.ts"],
42
+ "skills": [".pi/skills"],
43
+ "prompts": [".pi/prompts"]
44
+ }
45
+ }
@@ -0,0 +1,6 @@
1
+ mcp
2
+ requests
3
+ pillow
4
+ python-dotenv
5
+ # Optional: install for iPhone HEIC/HEIF image support
6
+ # pillow-heif