Scratch_Vision_Game / README2.md
prthm11's picture
Upload README2.md
c37925f verified
# Scratch Vision Game - Technical Documentation
## Overview
The Scratch Vision Game is an AI-powered system that converts visual Scratch programming blocks from images/PDFs into functional Scratch 3.0 projects (.sb3 files). The system uses computer vision, OCR, and large language models to analyze, interpret, and reconstruct Scratch programs from visual inputs.
## System Architecture
### Core Components
1. **Image Processing Pipeline** (`app.py`)
- PDF extraction and image preprocessing
- Multi-modal image enhancement using OpenCV
- OCR text extraction with Tesseract
- Visual similarity matching using multiple algorithms
2. **Block Recognition System** (`utils/block_relation_builder.py`)
- Scratch block catalog management
- Pseudocode to JSON conversion
- Block relationship building and validation
- Project structure generation
3. **AI Processing Layer**
- LLM-based code interpretation using Groq/LLaMA
- Multi-modal vision models for image captioning
- Semantic understanding of Scratch programming concepts
## Process Flow & System Tree Structure
### Complete User Journey Tree
```
USER INPUT (PDF File via Web Interface)
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ /process_pdf [POST] - Flask Route Handler
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ ๐Ÿ” PDF Validation & Security
โ”‚ โ”‚ โ”œโ”€โ”€ secure_filename() - Sanitize filename
โ”‚ โ”‚ โ”œโ”€โ”€ tempfile.mkdtemp() - Create temp directory
โ”‚ โ”‚ โ””โ”€โ”€ pdf_file.save() - Save to temp location
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ PDF Processing Pipeline
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŽฏ extract_images_from_pdf()
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ partition_pdf() - Unstructured library extraction
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ strategy="hi_res"
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ extract_image_block_types=["Image"]
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ extract_image_block_to_payload=True
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ’พ Save extracted.json
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ /outputs/EXTRACTED_JSON/{pdf_name}/extracted.json
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ”„ For Each Extracted Image:
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ Image Processing Branch
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ base64.b64decode() - Decode image data
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Image.open() - PIL image creation
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ image.save() - Save as PNG
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ /outputs/DETECTED_IMAGE/{pdf_name}/Sprite_{i}.png
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿค– AI Analysis Branch (Parallel)
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ Description Generation
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ LangGraph Agent (Groq LLaMA)
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Prompt: "Give a brief Captioning."
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ response["messages"][-1].content
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿท๏ธ Name Generation
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ LangGraph Agent (Groq LLaMA)
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Prompt: "give a short name caption"
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ response["messages"][-1].content
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“‹ Metadata Assembly
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ extracted_sprites.json
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "Sprite {count}": {
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "name": AI_generated_name
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "base64": image_data
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "file-path": pdf_directory
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ "description": AI_description
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ }
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ ๐ŸŽฎ Project Generation Pipeline
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ ๐Ÿ” similarity_matching()
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š Embedding Generation Branch
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŽฏ Query Processing
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ base64.b64decode() - Decode sprite images
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ tempfile.mkdtemp() - Create temp workspace
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Image.save() - Save temp sprite files
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿง  CLIP Embeddings
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ OpenCLIPEmbeddings() - Initialize embedder
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ clip_embd.embed_image() - Generate embeddings
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ sprite_features = np.array()
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“ˆ Similarity Computation
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Load: /outputs/embeddings.json
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ np.matmul(sprite_matrix, img_matrix.T)
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ np.argmax(similarity, axis=1)
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŽจ Asset Matching & Collection
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿง™โ€โ™‚๏ธ Sprite Assets Branch
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Match: /blocks/sprites/{matched_folder}/
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Load: sprite.json
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Copy: All files except matched image & sprite.json
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Append to: project_data[]
โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐ŸŒ„ Backdrop Assets Branch (Parallel)
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Match: /blocks/Backdrops/{matched_folder}/
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Load: project.json
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Copy: All files except matched image & project.json
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Extract: Stage targets โ†’ backdrop_data[]
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ—๏ธ Project Assembly
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“‹ JSON Structure Creation
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ final_project = {
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "targets": []
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "monitors": []
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ "extensions": []
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ "meta": {...}
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ }
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿง™โ€โ™‚๏ธ Sprite Integration
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ For sprite in project_data:
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ if not sprite.get("isStage"):
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ final_project["targets"].append(sprite)
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŒ„ Stage/Backdrop Integration
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ if backdrop_data:
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Merge: all_costumes.extend()
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Merge: sounds from first backdrop
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Create: Stage target with merged assets
โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ’พ Final Output
โ”‚ โ”‚ โ”œโ”€โ”€ /outputs/project_{uuid}/project.json
โ”‚ โ”‚ โ””โ”€โ”€ Return: project_json_path
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ค Response Generation
โ”‚ โ””โ”€โ”€ JSON Response:
โ”‚ โ”œโ”€โ”€ "message": "โœ… PDF processed successfully"
โ”‚ โ”œโ”€โ”€ "output_json": extracted_sprites_path
โ”‚ โ”œโ”€โ”€ "sprites": sprite_metadata
โ”‚ โ”œโ”€โ”€ "project_output_json": final_project_path
โ”‚ โ””โ”€โ”€ "test_url": download_link
โ”‚
โ””โ”€โ”€ ๐Ÿ“ฅ /download_sb3/{project_id} [GET] - Download Endpoint
โ”œโ”€โ”€ Locate: /game_samples/{project_id}.sb3
โ”œโ”€โ”€ Validate: File existence
โ””โ”€โ”€ send_from_directory() - Serve .sb3 file
```
### Parallel Processing Branches
```
๐Ÿ”„ CONCURRENT OPERATIONS DURING PDF PROCESSING:
โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ Image Processing Thread
โ”‚ โ”œโ”€โ”€ OpenCV Enhancement Pipeline
โ”‚ โ”‚ โ”œโ”€โ”€ upscale_image_cv() - 2x cubic interpolation
โ”‚ โ”‚ โ”œโ”€โ”€ reduce_noise_cv() - Non-local means denoising
โ”‚ โ”‚ โ”œโ”€โ”€ sharpen_cv() - Kernel-based sharpening
โ”‚ โ”‚ โ””โ”€โ”€ enhance_contrast_cv() - Contrast enhancement
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ Multi-Algorithm Similarity Matching
โ”‚ โ”œโ”€โ”€ DINOv2 Embeddings (Semantic)
โ”‚ โ”œโ”€โ”€ PHash (Perceptual Hashing)
โ”‚ โ””โ”€โ”€ Image Signatures (Goldberg Algorithm)
โ”œโ”€โ”€ ๐Ÿค– AI Processing Thread
โ”‚ โ”œโ”€โ”€ SmolVLM Vision Model
โ”‚ โ”‚ โ”œโ”€โ”€ Image Captioning
โ”‚ โ”‚ โ””โ”€โ”€ Name Generation
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ Groq LLaMA Language Model
โ”‚ โ”œโ”€โ”€ OCR Text Refinement
โ”‚ โ”œโ”€โ”€ Pseudocode Generation
โ”‚ โ””โ”€โ”€ JSON Structure Validation
โ””โ”€โ”€ ๐Ÿ’พ I/O Operations Thread
โ”œโ”€โ”€ File System Operations
โ”‚ โ”œโ”€โ”€ Directory Creation
โ”‚ โ”œโ”€โ”€ Image Saving/Loading
โ”‚ โ””โ”€โ”€ JSON Serialization
โ”‚
โ””โ”€โ”€ Asset Management
โ”œโ”€โ”€ Reference Asset Loading
โ”œโ”€โ”€ Project Asset Copying
โ””โ”€โ”€ Final Project Assembly
```
### Data Flow Diagram
```
๐Ÿ“Š DATA TRANSFORMATION PIPELINE:
PDF Bytes โ†’ Images โ†’ Enhanced Images โ†’ Embeddings โ†’ Similarities โ†’ Assets โ†’ .sb3
โ†“ โ†“ โ†“ โ†“ โ†“ โ†“ โ†“
[Binary] [PIL.Image] [np.ndarray] [np.float32] [indices] [JSON] [ZIP]
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
โ”œโ”€ OCR โ”€โ”€โ”€โ”€โ”€โ”ผโ”€ AI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€ Models โ”€โ”€โ”€โ”€โ”ผโ”€ Search โ”€โ”€โ”€โ”ผโ”€ Match โ”€โ”€โ”ผโ”€ Buildโ”ค
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
โ””โ”€ Text โ”€โ”€โ”€โ”€โ”ดโ”€ Metadata โ”€โ”ดโ”€ Features โ”€โ”€โ”ดโ”€ Ranking โ”€โ”€โ”ดโ”€ Select โ”€โ”ดโ”€ Pack โ”˜
```
### Key Processing Functions
**Input Processing:**
- `extract_images_from_pdf()` - Extracts images from PDF using unstructured library
- `process_image_cv2_from_pil()` - Enhances images using OpenCV (upscaling, denoising, sharpening)
### 2. Visual Similarity Matching
```
Query Image โ†’ Multi-Algorithm Matching โ†’ Asset Selection โ†’ Project Assembly
```
**Algorithms Used:**
- **DINOv2 Embeddings**: Deep learning-based semantic similarity
- **Perceptual Hashing (PHash)**: Structural image comparison
- **Image Signatures**: Goldberg algorithm for visual fingerprinting
**Implementation:**
```python
def run_query_search_flow(query_b64, embeddings_dict, hash_dict, signature_obj_map):
# 1. Preprocess query image
enhanced_query_pil = process_image_cv2_from_pil(query_from_b64, scale=2)
# 2. Generate embeddings
query_emb = get_dinov2_embedding_from_pil(prepped)
query_phash = phash.encode_image(image_array=query_hash_arr)
query_sig = gis.generate_signature(query_sig_path)
# 3. Compute similarities
emb_sim = cosine_similarity(query_emb, stored_emb)
ph_sim = 1.0 - (hamming_distance / MAX_PHASH_BITS)
im_sim = 1.0 - gis.normalized_distance(stored_sig, query_sig)
# 4. Combine scores
combined = (emb_clamped + ph_sim + im_sim) / 3.0
```
### 3. Code Block Recognition
```
OCR Text โ†’ LLM Processing โ†’ Pseudocode โ†’ Block Mapping โ†’ JSON Generation
```
**LLM System Prompt:**
```python
SYSTEM_PROMPT = """Your task is to process OCR-extracted text from images of Scratch 3.0 code blocks and produce precisely formatted pseudocode JSON.
### Core Role
- Treat this as an OCR refinement task: the input may contain typos or spacing issues.
- Intelligently correct OCR mistakes to align with valid Scratch 3.0 block syntax.
### Universal Rules
1. Code Detection: If no Scratch blocks are detected, the `pseudocode` value must be "No Code-blocks".
2. Script Ownership: Determine the target from "Script for:". If it matches a `Stage_costumes` name, set `name_variable` to "Stage".
3. Pseudocode Structure: The pseudocode must be a single JSON string with `\n` for newlines.
"""
```
### 4. Project Generation
```
Pseudocode โ†’ Block Definitions โ†’ Relationship Building โ†’ .sb3 Assembly
```
## Libraries and Dependencies
### Core Libraries
#### Computer Vision & Image Processing
- **OpenCV** (`cv2`): Image enhancement, filtering, and preprocessing
- **PIL/Pillow**: Image manipulation and format conversion
- **imagededup**: Perceptual hashing for duplicate detection
- **image-match**: Visual similarity using Goldberg signatures
#### Machine Learning & AI
- **transformers**: Hugging Face models (DINOv2, SmolVLM)
- **torch**: PyTorch for deep learning inference
- **sentence-transformers**: Text and image embeddings
- **faiss-cpu**: Fast similarity search and clustering
- **open_clip_torch**: OpenAI CLIP embeddings
#### Language Models
- **langchain**: LLM orchestration and chaining
- **langchain-groq**: Groq API integration
- **langgraph**: Graph-based agent workflows
#### Document Processing
- **unstructured**: PDF parsing and content extraction
- **pdf2image**: PDF to image conversion
- **pytesseract**: OCR text extraction
- **PyPDF2**: PDF manipulation
#### Web Framework
- **Flask**: Web application framework
- **Flask-SocketIO**: Real-time communication
- **gunicorn**: WSGI HTTP server
### Model Specifications
#### Vision Models
```python
# DINOv2 for semantic image understanding
DINOV2_MODEL = "facebook/dinov2-small"
dinov2_processor = AutoImageProcessor.from_pretrained(DINOV2_MODEL)
dinov2_model = AutoModel.from_pretrained(DINOV2_MODEL)
# SmolVLM for image captioning
smolvlm256m_processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-256M-Instruct")
smolvlm256m_model = AutoModelForVision2Seq.from_pretrained("HuggingFaceTB/SmolVLM-256M-Instruct")
```
#### Language Model
```python
# Groq LLaMA for code interpretation
llm = ChatGroq(
model="meta-llama/llama-4-scout-17b-16e-instruct",
temperature=0,
max_tokens=None,
)
```
## Technical Approaches
### 1. Multi-Modal Image Enhancement
**OpenCV Pipeline:**
```python
def process_image_cv2_from_pil(pil_img, scale=2):
bgr = pil_to_bgr_np(pil_img)
bgr = upscale_image_cv(bgr, scale=scale) # Cubic interpolation
bgr = reduce_noise_cv(bgr) # Non-local means denoising
bgr = sharpen_cv(bgr) # Kernel-based sharpening
bgr = enhance_contrast_cv(bgr) # Contrast enhancement
return bgr_np_to_pil(bgr)
```
### 2. Hybrid Similarity Scoring
**Multi-Algorithm Consensus:**
```python
def choose_top_candidates(embedding_results, phash_results, imgmatch_results):
# Method A: Normalized weighted average
weighted_scores[p] = (w_emb * emb_norm[p] + w_ph * ph_norm[p] + w_im * im_norm[p])
# Method B: Rank-sum (Borda count)
rank_sum[p] = rank_emb[p] + rank_ph[p] + rank_im[p]
# Method C: Harmonic mean (penalizes missing values)
harm = 3.0 / ((1.0/a) + (1.0/b) + (1.0/c))
```
### 3. Block Relationship Building
**Scratch Block Catalog System:**
```python
def generate_blocks_from_opcodes(opcode_counts, all_block_definitions):
"""
Generates Scratch blocks with proper parent-child relationships
- Hat blocks: topLevel=True, parent=None
- Stack blocks: Linked via 'next' field
- C-blocks: Contains SUBSTACK inputs
- Shadow blocks: Linked as input values
"""
```
### 4. Project Assembly Pipeline
**JSON Structure Generation:**
```python
final_project = {
"targets": [], # Sprites and Stage
"monitors": [], # Variable/list monitors
"extensions": [], # Scratch extensions
"meta": {
"semver": "3.0.0",
"vm": "11.3.0",
"agent": "OpenAI ScratchVision Agent"
}
}
```
## File System Architecture
### Project Directory Structure
```
๐Ÿ“ scratch-vision-game/
โ”œโ”€โ”€ ๐Ÿ app.py # Main Flask application (PRIMARY)
โ”œโ”€โ”€ ๐Ÿ“‹ requirements.txt # Python dependencies
โ”œโ”€โ”€ ๐Ÿณ Dockerfile # Container configuration
โ”œโ”€โ”€ ๐Ÿ“– README.md # Basic project info
โ”œโ”€โ”€ ๐Ÿ“– README2.md # Technical documentation
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ utils/ # Core processing utilities
โ”‚ โ””โ”€โ”€ ๐Ÿ”ง block_relation_builder.py # Scratch block logic & JSON generation
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ blocks/ # Scratch block definitions & assets
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š blocks.json # Main block catalog
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š boolean_blocks.json # Boolean/condition blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š cap_blocks.json # Terminal blocks (stop, delete clone)
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š c_blocks.json # Control flow blocks (if, repeat, forever)
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š control_blocks.json # Control category blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š data_blocks.json # Variables and lists blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š event_blocks.json # Event/trigger blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š hat_blocks.json # Script starter blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š looks_blocks.json # Appearance blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š motion_blocks.json # Movement blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š operator_blocks.json # Math and logic operators
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š reporter_blocks.json # Value reporter blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š sensing_blocks.json # Sensor blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š sound_blocks.json # Audio blocks
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š stack_blocks.json # Sequential action blocks
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ sprites/ # Reference sprite assets
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ {sprite_name}/
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ {sprite_image}.png
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š sprite.json # Sprite definition
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐ŸŽต {sounds}.wav
โ”‚ โ”‚ โ””โ”€โ”€ ...
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ Backdrops/ # Reference backdrop assets
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ {backdrop_name}/
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ {backdrop_image}.png
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š project.json # Stage definition
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐ŸŽต {sounds}.wav
โ”‚ โ”‚ โ””โ”€โ”€ ...
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ ๐Ÿ“ sound/ # Audio assets library
โ”‚ โ””โ”€โ”€ ๐ŸŽต *.wav
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ templates/ # Flask HTML templates
โ”‚ โ””โ”€โ”€ ๐ŸŒ *.html
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ static/ # Web static assets
โ”‚ โ”œโ”€โ”€ ๐ŸŽจ css/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“œ js/
โ”‚ โ””โ”€โ”€ ๐Ÿ–ผ๏ธ images/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ game_samples/ # Pre-built .sb3 files
โ”‚ โ””โ”€โ”€ ๐ŸŽฎ *.sb3
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ generated_projects/ # Runtime generated projects
โ”‚ โ””โ”€โ”€ ๐Ÿ“ project_{uuid}/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š project.json
โ”‚ โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ *.png
โ”‚ โ””โ”€โ”€ ๐ŸŽต *.wav
โ”‚
โ””โ”€โ”€ ๐Ÿ“ outputs/ # Processing outputs (Runtime)
โ”œโ”€โ”€ ๐Ÿ“ DETECTED_IMAGE/ # Extracted & processed images
โ”‚ โ””โ”€โ”€ ๐Ÿ“ {pdf_name}/
โ”‚ โ””โ”€โ”€ ๐Ÿ–ผ๏ธ Sprite_*.png
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ SCANNED_IMAGE/ # Original scanned images
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ EXTRACTED_JSON/ # Intermediate JSON data
โ”‚ โ””โ”€โ”€ ๐Ÿ“ {pdf_name}/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“Š extracted.json # Raw PDF extraction
โ”‚ โ””โ”€โ”€ ๐Ÿ“Š extracted_sprites.json # AI-processed sprites
โ”‚
โ””โ”€โ”€ ๐Ÿ“Š embeddings.json # Pre-computed embeddings cache
```
### Runtime Directory Creation Flow
```
๐Ÿ—๏ธ DYNAMIC DIRECTORY CREATION:
User Upload โ†’ PDF Processing โ†’ Directory Structure
โ”‚ โ”‚ โ”‚
โ”œโ”€ temp_dir โ”€โ”€โ”€โ”ผโ”€ pdf_filename โ”€โ”€โ”€โ”€โ”€โ”ผโ”€ /outputs/DETECTED_IMAGE/{pdf_name}/
โ”‚ โ”‚ โ”œโ”€ /outputs/EXTRACTED_JSON/{pdf_name}/
โ”‚ โ”‚ โ””โ”€ /generated_projects/project_{uuid}/
โ”‚ โ”‚
โ””โ”€ secure_filename() โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Sanitized paths
```
### Data Persistence Locations
```
๐Ÿ’พ PERSISTENT DATA STORAGE:
โ”œโ”€โ”€ ๐Ÿ”„ Input Processing
โ”‚ โ”œโ”€โ”€ /tmp/{random}/ - Temporary PDF storage
โ”‚ โ”œโ”€โ”€ /outputs/DETECTED_IMAGE/ - Extracted sprite images
โ”‚ โ”œโ”€โ”€ /outputs/EXTRACTED_JSON/ - Processing metadata
โ”‚ โ””โ”€โ”€ /outputs/embeddings.json - Similarity search cache
โ”‚
โ”œโ”€โ”€ ๐ŸŽฏ Asset Matching
โ”‚ โ”œโ”€โ”€ /blocks/sprites/ - Reference sprite library
โ”‚ โ”œโ”€โ”€ /blocks/Backdrops/ - Reference backdrop library
โ”‚ โ””โ”€โ”€ /blocks/*.json - Block definition catalogs
โ”‚
โ””โ”€โ”€ ๐ŸŽฎ Final Output
โ”œโ”€โ”€ /generated_projects/project_{uuid}/ - Assembled project
โ”œโ”€โ”€ /game_samples/{project_id}.sb3 - Downloadable Scratch file
โ””โ”€โ”€ /logs/app.log - Application logs
```
## API Endpoints
### `/process_pdf` (POST)
Processes uploaded PDF files containing Scratch code blocks.
**Request:**
```
Content-Type: multipart/form-data
pdf_file: <PDF file>
```
**Response:**
```json
{
"message": "โœ… PDF processed successfully",
"output_json": "path/to/extracted.json",
"sprites": {...},
"project_output_json": "path/to/project.json"
}
```
### `/download_sb3/<project_id>` (GET)
Downloads generated Scratch 3.0 project files.
## Processing Timeline & Performance
### Execution Timeline Tree
```
โฑ๏ธ PROCESSING TIMELINE (Typical PDF with 5 images):
๐Ÿ“ค User Upload (0.0s)
โ”‚
โ”œโ”€โ”€ ๐Ÿ” PDF Validation (0.1s)
โ”‚ โ””โ”€โ”€ File security & temp storage
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ PDF Extraction (2-5s)
โ”‚ โ”œโ”€โ”€ partition_pdf() - Unstructured processing
โ”‚ โ”œโ”€โ”€ Image extraction & base64 encoding
โ”‚ โ””โ”€โ”€ extracted.json creation
โ”‚
โ”œโ”€โ”€ ๐Ÿค– AI Processing (10-15s per image)
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ Description Generation (5-7s)
โ”‚ โ”‚ โ”œโ”€โ”€ LangGraph agent initialization
โ”‚ โ”‚ โ”œโ”€โ”€ Groq API call
โ”‚ โ”‚ โ””โ”€โ”€ Response processing
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ ๐Ÿท๏ธ Name Generation (5-7s)
โ”‚ โ”‚ โ”œโ”€โ”€ Second LangGraph agent call
โ”‚ โ”‚ โ”œโ”€โ”€ Groq API call
โ”‚ โ”‚ โ””โ”€โ”€ Response processing
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ ๐Ÿ“‹ Metadata Assembly (0.1s)
โ”‚ โ””โ”€โ”€ JSON structure creation
โ”‚
โ”œโ”€โ”€ ๐Ÿ” Similarity Matching (3-8s)
โ”‚ โ”œโ”€โ”€ ๐ŸŽฏ Image Decoding (0.5s)
โ”‚ โ”œโ”€โ”€ ๐Ÿง  CLIP Embeddings (2-3s)
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ˆ Similarity Computation (0.5s)
โ”‚ โ””โ”€โ”€ ๐ŸŽจ Asset Matching (2-4s)
โ”‚
โ”œโ”€โ”€ ๐Ÿ—๏ธ Project Assembly (1-2s)
โ”‚ โ”œโ”€โ”€ JSON merging
โ”‚ โ”œโ”€โ”€ Asset copying
โ”‚ โ””โ”€โ”€ Final project creation
โ”‚
โ””โ”€โ”€ ๐Ÿ“ค Response Generation (0.1s)
โ””โ”€โ”€ JSON response formatting
TOTAL: ~60-90 seconds for 5-image PDF
```
### Performance Bottlenecks & Optimizations
```
๐Ÿš€ PERFORMANCE OPTIMIZATION STRATEGIES:
โ”œโ”€โ”€ ๐Ÿง  Model Loading (Startup Cost)
โ”‚ โ”œโ”€โ”€ โœ… Pre-loaded global models
โ”‚ โ”‚ โ”œโ”€โ”€ DINOv2: ~2GB VRAM
โ”‚ โ”‚ โ”œโ”€โ”€ SmolVLM: ~1GB VRAM
โ”‚ โ”‚ โ””โ”€โ”€ CLIP: ~500MB VRAM
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ โœ… GPU Acceleration (when available)
โ”‚ โ”‚ โ””โ”€โ”€ torch.device("cuda" if torch.cuda.is_available() else "cpu")
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ โœ… CPU Optimization
โ”‚ โ””โ”€โ”€ torch.set_num_threads(4)
โ”‚
โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ Image Processing Pipeline
โ”‚ โ”œโ”€โ”€ โœ… Efficient NumPy Operations
โ”‚ โ”‚ โ”œโ”€โ”€ Vectorized computations
โ”‚ โ”‚ โ”œโ”€โ”€ In-place operations where possible
โ”‚ โ”‚ โ””โ”€โ”€ Memory-mapped file access
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ โœ… OpenCV Optimizations
โ”‚ โ”‚ โ”œโ”€โ”€ Multi-threaded operations
โ”‚ โ”‚ โ”œโ”€โ”€ SIMD instructions
โ”‚ โ”‚ โ””โ”€โ”€ Optimized algorithms
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ โœ… Memory Management
โ”‚ โ”œโ”€โ”€ Garbage collection hints
โ”‚ โ”œโ”€โ”€ Temporary file cleanup
โ”‚ โ””โ”€โ”€ Buffer reuse
โ”‚
โ”œโ”€โ”€ ๐Ÿ” Similarity Search Acceleration
โ”‚ โ”œโ”€โ”€ โœ… Pre-computed Embeddings Cache
โ”‚ โ”‚ โ””โ”€โ”€ /outputs/embeddings.json (persistent)
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ โœ… Normalized Embeddings
โ”‚ โ”‚ โ”œโ”€โ”€ Cosine similarity via dot product
โ”‚ โ”‚ โ””โ”€โ”€ L2 normalization preprocessing
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ โœ… Parallel Algorithm Execution
โ”‚ โ”œโ”€โ”€ DINOv2, PHash, ImageMatch concurrent
โ”‚ โ””โ”€โ”€ Multi-threaded similarity computation
โ”‚
โ””โ”€โ”€ ๐ŸŒ API & I/O Optimizations
โ”œโ”€โ”€ โœ… Async File Operations
โ”œโ”€โ”€ โœ… Streaming Responses
โ”œโ”€โ”€ โœ… Connection Pooling
โ””โ”€โ”€ โœ… Compression (gzip)
```
### Memory Usage Profile
```
๐Ÿ’พ MEMORY CONSUMPTION BREAKDOWN:
โ”œโ”€โ”€ ๐Ÿง  AI Models (Peak: ~4GB)
โ”‚ โ”œโ”€โ”€ DINOv2 Model: ~2GB
โ”‚ โ”œโ”€โ”€ SmolVLM Model: ~1GB
โ”‚ โ”œโ”€โ”€ CLIP Embeddings: ~500MB
โ”‚ โ””โ”€โ”€ Groq API Client: ~100MB
โ”‚
โ”œโ”€โ”€ ๐Ÿ–ผ๏ธ Image Processing (Peak: ~500MB per image)
โ”‚ โ”œโ”€โ”€ Original PIL Images: ~50MB each
โ”‚ โ”œโ”€โ”€ Enhanced Images: ~100MB each
โ”‚ โ”œโ”€โ”€ OpenCV Buffers: ~200MB each
โ”‚ โ””โ”€โ”€ Embedding Vectors: ~2KB each
โ”‚
โ”œโ”€โ”€ ๐Ÿ“Š Data Structures (Peak: ~200MB)
โ”‚ โ”œโ”€โ”€ Block Definitions: ~50MB
โ”‚ โ”œโ”€โ”€ Asset Metadata: ~100MB
โ”‚ โ”œโ”€โ”€ Similarity Matrices: ~50MB
โ”‚ โ””โ”€โ”€ JSON Structures: ~10MB
โ”‚
โ””โ”€โ”€ ๐ŸŒ Web Framework (Baseline: ~100MB)
โ”œโ”€โ”€ Flask Application: ~50MB
โ”œโ”€โ”€ Request Buffers: ~30MB
โ””โ”€โ”€ Response Caching: ~20MB
TOTAL PEAK: ~5GB (with GPU models loaded)
TOTAL BASELINE: ~1GB (CPU-only, no active processing)
```
### Performance Optimizations
### 1. Model Caching
- Pre-loaded models with global variables
- GPU acceleration when available
- Batch processing for multiple images
### 2. Image Processing
- Efficient numpy operations
- OpenCV optimizations
- Memory management for large images
### 3. Similarity Search
- FAISS indexing for fast nearest neighbor search
- Normalized embeddings for cosine similarity
- Parallel processing of multiple algorithms
## Error Handling
### 1. Graceful Degradation
```python
def process_image_cv2_from_pil(pil_img, scale=2):
try:
# OpenCV enhancement pipeline
return enhanced_image
except Exception as e:
print(f"Enhancement failed: {e}")
return original_image # Fallback to original
```
### 2. JSON Validation
```python
agent_json_resolver = create_react_agent(
model=llm,
prompt=SYSTEM_PROMPT_JSON_CORRECTOR
)
```
## Deployment
### Docker Configuration
```dockerfile
FROM python:3.11-slim
# System dependencies: tesseract-ocr, poppler-utils, libgl1
# Python dependencies: requirements.txt
# Environment: Flask production mode
EXPOSE 7860
CMD ["python", "app.py"]
```
### Environment Variables
- `GROQ_API_KEY`: API key for Groq language model
- `TRANSFORMERS_CACHE`: Model cache directory
- `HF_HOME`: Hugging Face cache directory
## Future Enhancements
1. **Real-time Processing**: WebSocket integration for live feedback
2. **Advanced OCR**: Custom trained models for Scratch block recognition
3. **Multi-language Support**: International Scratch block recognition
4. **Collaborative Features**: Multi-user project editing
5. **Performance Monitoring**: Detailed analytics and optimization metrics
## Contributing
The system is designed with modularity in mind:
- Add new block definitions in `blocks/` directory
- Extend similarity algorithms in the matching pipeline
- Enhance OCR accuracy with custom preprocessing
- Improve LLM prompts for better code interpretation
## License
Apache 2.0 License - See project repository for full details.