# povzayd/p-per architecture

> Architecture diagram of the GitHub repository povzayd/p-per, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/povzayd/p-per
- Repository: https://github.com/povzayd/p-per

## Overview

#### Purpose and Entry Points
This is a terminal-oriented forensic media recovery pipeline: recover JPEG byte ranges from binary evidence, optionally triage them with ML, then reconstruct and annotate a video. Primary executables are `pcap2img.py`, `pcap2imgv2.py`, and `videogen.py`. Included sample evidence includes `security-footage-1648933966395.pcap` and `security-footage-1648933966395.mp4`.

#### 1. Lightweight JPEG Carving
`pcap2img.py` is the interactive, lower-resource extractor. It prompts for an input file, scans its raw bytes for JPEG SOI/EOI signatures (`FFD8` / `FFD9`), and writes sequential `frame_XXXX.jpg` files into an input-derived directory. It is the simple recovery path for dumps, captures, or media containers, with no protocol-level PCAP parsing or deep JPEG validation.

#### 2. Scalable Extraction and CLI Boundary
`pcap2imgv2.py` is the automated/high-volume entry point. Its argparse interface accepts required input plus output-directory, archive, and classification options. It processes evidence in chunks to constrain memory use, recovers JPEG ranges, records extraction metrics, and can package the resulting frame directory as ZIP. This creates the main handoff boundary to downstream analysis: a directory or archive of recovered JPEG frames.

#### 3. Optional ImageNet Classification
The `--classify` branch in `pcap2imgv2.py` loads pretrained MobileNetV2 through PyTorch/torchvision and applies it to successfully recovered frames. Classification is optional because it adds substantial CPU/GPU and model-runtime cost and may fail or mislabel damaged/non-natural forensic imagery. Labels are triage metadata rather than extraction criteria.

#### 4. Frame-Sequence Ingestion
`videogen.py` consumes either a frame directory or ZIP archive, orders/loads image frames, derives timestamps from metadata where available, normalizes frames to the selected output resolution, and emits an MP4. Its input contract aligns directly with both extractor outputs, allowing recovery and reconstruction to be invoked as separate stages.

#### 5. Privacy and Evidence Annotation
During video generation, `videogen.py` can detect faces using OpenCV Haar cascades, either draw detection boxes or blur detected regions. It can also invoke Tesseract OCR to extract visible text and log frame metadata/OCR output to CSV. OCR is explicitly bypassable for faster rendering; face detection and anonymization remain opt-in transformations on recovered evidence.

#### 6. Motion Analysis and Heatmaps
`videogen.py` computes motion from consecutive grayscale frames via differencing, thresholding, dilation, contour detection, and an accumulated motion matrix. With motion highlighting enabled, detected regions are overlaid on video frames and an OpenCV color-mapped heatmap image is saved; preview is optional. This analysis depends on temporal ordering and treats minor frame changes as potential motion.

#### Runtime and External Dependencies
The repository is Python-based, with dependencies declared in `requirements.txt`. Extraction uses CLI/progress/logging libraries; advanced classification requires PyTorch and torchvision and may use CUDA if installed. Video reconstruction depends on OpenCV, Pillow, NumPy, and system-accessible Tesseract via pytesseract. Outputs are local files: JPEG frames, optional ZIPs, MP4 video, CSV logs, and heatmap JPEGs.

Main entry points: [`videogen.py`](https://github.com/povzayd/p-per/blob/main/videogen.py) (Frame normalization and MP4), [`pcap2imgv2.py`](https://github.com/povzayd/p-per/blob/main/pcap2imgv2.py) (Scalable extractor), [`pcap2img.py`](https://github.com/povzayd/p-per/blob/main/pcap2img.py) (Interactive carver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_recovery["JPEG Recovery"]
  node_evidence["Binary evidence<br/>input media"]
  node_light_carver["Interactive carver<br/>Python CLI<br/>[pcap2img.py]"]
  node_scalable_cli["Scalable extractor<br/>Python CLI<br/>[pcap2imgv2.py]"]
  node_jpeg_carving{{"JPEG range recovery<br/>signature scanner"}}
  node_frames["Recovered frames<br/>JPEG directory"]
  node_frame_archive["Frame archive<br/>ZIP output"]
end

subgraph group_triage["Optional Triage"]
  node_classification{{"MobileNetV2 classifier<br/>optional ML branch<br/>[pcap2imgv2.py]"}}
  node_triage_labels["Triage labels<br/>metadata"]
end

subgraph group_video["Video Reconstruction"]
  node_video_cli["Video generator<br/>Python CLI<br/>[videogen.py]"]
  node_frame_ingestion{{"Frame ordering and timestamps<br/>ingestion stage<br/>[videogen.py]"}}
  node_video_render{{"Frame normalization and MP4<br/>rendering stage<br/>[videogen.py]"}}
  node_privacy{{"Face annotation or blur<br/>optional OpenCV stage<br/>[videogen.py]"}}
  node_ocr{{"OCR and CSV logging<br/>optional Tesseract stage<br/>[videogen.py]"}}
  node_motion{{"Motion analysis<br/>temporal analysis<br/>[videogen.py]"}}
  node_mp4["Reconstructed video<br/>MP4 output"]
  node_csv["OCR metadata log<br/>CSV output"]
  node_heatmap["Motion heatmap<br/>JPEG output"]
end

node_evidence -->|"interactive input"| node_light_carver
node_evidence -->|"CLI input"| node_scalable_cli
node_light_carver -->|"raw scan"| node_jpeg_carving
node_scalable_cli -->|"chunked scan"| node_jpeg_carving
node_jpeg_carving -->|"write JPEGs"| node_frames
node_scalable_cli -.->|"optional ZIP"| node_frame_archive
node_frames -.->|"--classify"| node_classification
node_classification -->|"ImageNet labels"| node_triage_labels
node_frames -->|"frame directory"| node_video_cli
node_frame_archive -->|"ZIP input"| node_video_cli
node_video_cli -->|"load sequence"| node_frame_ingestion
node_frame_ingestion -.->|"ordered frames"| node_privacy
node_privacy -.->|"annotated frames"| node_ocr
node_ocr -.->|"render path"| node_motion
node_frame_ingestion -->|"normalized sequence"| node_video_render
node_privacy -.->|"optional transform"| node_video_render
node_motion -.->|"optional overlays"| node_video_render
node_video_render -->|"write MP4"| node_mp4
node_ocr -.->|"write CSV"| node_csv
node_motion -.->|"save heatmap"| node_heatmap

click node_light_carver "https://github.com/povzayd/p-per/blob/main/pcap2img.py"
click node_scalable_cli "https://github.com/povzayd/p-per/blob/main/pcap2imgv2.py"
click node_classification "https://github.com/povzayd/p-per/blob/main/pcap2imgv2.py"
click node_video_cli "https://github.com/povzayd/p-per/blob/main/videogen.py"
click node_frame_ingestion "https://github.com/povzayd/p-per/blob/main/videogen.py"
click node_video_render "https://github.com/povzayd/p-per/blob/main/videogen.py"
click node_privacy "https://github.com/povzayd/p-per/blob/main/videogen.py"
click node_ocr "https://github.com/povzayd/p-per/blob/main/videogen.py"
click node_motion "https://github.com/povzayd/p-per/blob/main/videogen.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_evidence,node_light_carver,node_scalable_cli,node_jpeg_carving,node_frames,node_frame_archive toneBlue
class node_classification,node_triage_labels toneAmber
class node_video_cli,node_frame_ingestion,node_video_render,node_privacy,node_ocr,node_motion,node_mp4,node_csv,node_heatmap toneMint
```

## Components

### JPEG Recovery

- **Binary evidence**: input media · PCAPs, dumps, or media containers scanned as raw bytes.
- [Interactive carver](https://github.com/povzayd/p-per/blob/main/pcap2img.py): Python CLI · Low-resource manual recovery path. · `pcap2img.py`
- [Scalable extractor](https://github.com/povzayd/p-per/blob/main/pcap2imgv2.py): Python CLI · Chunked argparse-driven extraction with metrics. · `pcap2imgv2.py`
- **JPEG range recovery**: signature scanner · Finds SOI/EOI byte ranges.
- **Recovered frames**: JPEG directory · Sequential frame_XXXX.jpg output and downstream handoff.
- **Frame archive**: ZIP output · Optional packaged frame directory.

### Optional Triage

- [MobileNetV2 classifier](https://github.com/povzayd/p-per/blob/main/pcap2imgv2.py): optional ML branch · PyTorch/torchvision ImageNet triage for recovered frames. · `pcap2imgv2.py`
- **Triage labels**: metadata · Labels do not determine extraction.

### Video Reconstruction

- [Video generator](https://github.com/povzayd/p-per/blob/main/videogen.py): Python CLI · Consumes frame directories or ZIP archives. · `videogen.py`
- [Frame ordering and timestamps](https://github.com/povzayd/p-per/blob/main/videogen.py): ingestion stage · Loads frames and derives metadata timestamps where available. · `videogen.py`
- [Frame normalization and MP4](https://github.com/povzayd/p-per/blob/main/videogen.py): rendering stage · Resizes frames to output resolution and emits video. · `videogen.py`
- [Face annotation or blur](https://github.com/povzayd/p-per/blob/main/videogen.py): optional OpenCV stage · Haar detection can draw boxes or anonymize faces. · `videogen.py`
- [OCR and CSV logging](https://github.com/povzayd/p-per/blob/main/videogen.py): optional Tesseract stage · Extracts visible text and frame metadata. · `videogen.py`
- [Motion analysis](https://github.com/povzayd/p-per/blob/main/videogen.py): temporal analysis · Differences consecutive grayscale frames and finds regions. · `videogen.py`
- **Reconstructed video**: MP4 output
- **OCR metadata log**: CSV output
- **Motion heatmap**: JPEG output · Optional accumulated motion visualization.

## Connections

- Binary evidence → Interactive carver: interactive input
- Binary evidence → Scalable extractor: CLI input
- Interactive carver → JPEG range recovery: raw scan
- Scalable extractor → JPEG range recovery: chunked scan
- JPEG range recovery → Recovered frames: write JPEGs
- Scalable extractor → Frame archive: optional ZIP
- Recovered frames → MobileNetV2 classifier: --classify
- MobileNetV2 classifier → Triage labels: ImageNet labels
- Recovered frames → Video generator: frame directory
- Frame archive → Video generator: ZIP input
- Video generator → Frame ordering and timestamps: load sequence
- Frame ordering and timestamps → Face annotation or blur: ordered frames
- Face annotation or blur → OCR and CSV logging: annotated frames
- OCR and CSV logging → Motion analysis: render path
- Frame ordering and timestamps → Frame normalization and MP4: normalized sequence
- Face annotation or blur → Frame normalization and MP4: optional transform
- Motion analysis → Frame normalization and MP4: optional overlays
- Frame normalization and MP4 → Reconstructed video: write MP4
- OCR and CSV logging → OCR metadata log: write CSV
- Motion analysis → Motion heatmap: save heatmap
