Detect with a USB webcam (OpenCV)¶
Without a HUSKYLENS2, a generic USB webcam plus OpenCV [1] is enough to emit person_detected and possible_littering events.
What you'll do: Detect a person and littering candidates from a USB webcam, emit events
Prerequisites: Prerequisites (install tools) + Quickstart (run the stack)
Keep running: the Quickstart (run the stack) stack (especially
mediator-owner). Keep the publisher from 1.1 stopped (it shares port 8080 with the Quickstart IPFS)What you need: PC + USB webcam
Time required: approx. 30 min
Where this page belongs
- This is section 1.2 of Part 1 (Hands-on). Pick just one of the three pages in 1.2.
- The "Next" link at the bottom of the page points to another option in the same group. Follow this page's Next steps section instead.
- "Phase 1" on this page is a development stage of the platform, not the learning order (Hands-on).
- Previous page: Quickstart (run the stack)
- Next page: section 1.3 of Hands-on (trade on the marketplace)
Goal¶
Detect people and possible littering in USB webcam images and generate event files, even without HUSKYLENS2. mediator-owner registers the generated files on the marketplace as merchandise.
The following diagram shows how a detection result becomes registered merchandise.
flowchart TB
subgraph OFF["On the PC (off-chain)"]
direction LR
DEV["USB webcam<br>or mock"]
BR["webcam_litter_bridge.py<br>(webcam-bridge)"]
FILE["event file<br>401_webcam_event_*.txt<br>(raw_data/output)"]
MO["mediator-owner"]
end
subgraph ON["Blockchain (on-chain)"]
direction LR
MC["merchandise contract<br>(Merchandise)"]
MK["marketplace<br>(IoTMarket)"]
end
UI["iot-market-ui<br>localhost:5173/merchandise/#lt;address#gt;"]
DEV -->|"frames"| BR
BR -->|"writes one event<br>per file"| FILE
FILE -->|"finds the new file"| MO
MO -->|"creates<br>(price, file hash)"| MC
MO -->|"registers the merchandise address"| MK
MC -.->|"shown on the merchandise page"| UI
Only merchandise information such as the price and the file hash is written to the chain. The event file itself stays with mediator-owner and is handed to the buyer after a purchase (covered in 1.3).
What this page helps you understand¶
- how to try a Phase 1 event-generation flow with only a USB webcam
- how
person_detectedandpossible_litteringare used differently - where to look in the generated event files
Common stumbling points¶
- camera index and camera ownership are common first-run failures
- lighting and framing strongly affect the heuristic output
- detections can be generated while the downstream pipeline still fails to pick them up
Required environment¶
- USB webcam, or a mock execution setup
webcam-bridgeavailable- the output folder
mediator-owner/raw_data/outputexists (the steps create it if missing) - in webcam mode, the camera device is recognized by the PC
- Python 3 (webcam mode also needs
ultralyticsandopencv-python. Install them in a virtual environment (venv) at the top of the course repository (cd ~/program/Blockchain_IoT_Marketplace && python3 -m venv .venv && source .venv/bin/activate && pip install ultralytics "opencv-python<5"). In a newly opened terminal, runsource ~/program/Blockchain_IoT_Marketplace/.venv/bin/activatefirst. On macOS there may be nopipcommand, and Homebrew Python rejectspip installoutside a venv)
Exercise Programs¶
This exercise generates a possible_littering mock event file for the USB webcam path.
The problem program focuses on the minimum event structure and on the meaning of fields such as camera_id, confidence, and event_type.
Shortest path¶
For a first pass, these four steps are enough.
- start
webcam_litter_bridge.pyinmockmode - confirm that
*_webcam_event_*.txtfiles are generated - look at the file content (event type and score)
- confirm that the
mediator-ownerterminal printsProduct deployed
Branches after that:
- If you only want to confirm the downstream pipeline:
mockmode is enough - If you want to inspect real camera detection: continue to
webcammode - If the camera cannot be opened: check camera index and device ownership first
Phase 1: Confirm the pipeline in mock mode¶
1. Mock first¶
Run in the webcam-bridge folder of the course repository (the first cd below moves there). Mock mode generates simulated events in place of a camera (no extra packages needed).
cd ~/program/Blockchain_IoT_Marketplace/webcam-bridge
mkdir -p ../mediator-owner/raw_data/output
python3 webcam_litter_bridge.py \
--mode mock \
--camera-id 401 \
--output-dir ../mediator-owner/raw_data/output \
--flush-seconds 5
One line is printed every 5 seconds, as below. Press Ctrl+C to stop.
[webcam-bridge] mock mode start
[webcam-bridge] wrote ../mediator-owner/raw_data/output/401_webcam_event_1791549330.txt
[webcam-bridge] wrote ../mediator-owner/raw_data/output/401_webcam_event_1791549335.txt
Look at the generated files in another terminal (you can leave the bridge running).
cat ~/program/Blockchain_IoT_Marketplace/mediator-owner/raw_data/output/401_webcam_event_*.txt | head -10
# Webcam Event Snapshot
camera_id: 401
event_type: possible_littering
event_time_utc: 2026-10-09T12:35:30.906857+00:00
event_score: 0.801
summary: Bottle remained on ground area for long duration
details:
{"event_type": "possible_littering", "event_time_utc": "...", "event_score": 0.801, "summary": "Bottle remained on ground area for long duration", "object": {...}}
Mock mode outputs either possible_littering or person_detected at random. A person_detected file has person_count instead of object, so it is fine if your first file differs from the example above. Each file is one event. event_type is the kind of event, event_score is its confidence, and details holds the underlying detection.
Confirm registration as merchandise¶
If mediator-owner is running as set up in Quickstart (run the stack), its terminal prints a line like the following each time an event file is written.
Product deployed: 0xc6ba8c3233ecf65b761049ef63466945c362edd2 for file 401_webcam_event_1791551565.txt
mediator-owner watches raw_data/output and registers each new event file on the chain as merchandise. 0x... is the item's address. Open the following URL in a browser to see its page.
http://localhost:5173/merchandise/<the address shown>
For purchasing the item and receiving the data, see §1.3 of Hands-on. If nothing is printed, check that the course repository is up to date (git pull) and that mediator-owner was restarted.
Once event files are generated in mock mode, the next step is to switch to the physical webcam and inspect the actual detections.
Phase 1: Inspect detections in webcam mode¶
2. USB webcam¶
Webcam mode uses an object-detection model (YOLO). Install ultralytics and opencv-python into the venv as described in Required environment. The first two lines of the command below move to the webcam-bridge folder and activate that venv. On the first run the model file (yolov8n.pt, a few MB) is downloaded automatically.
cd ~/program/Blockchain_IoT_Marketplace/webcam-bridge
source ../.venv/bin/activate
python3 webcam_litter_bridge.py \
--mode webcam \
--camera-index 0 \
--camera-id 401 \
--output-dir ../mediator-owner/raw_data/output \
--litter-classes bottle,cup \
--linger-seconds 8 \
--person-away-seconds 5
3. Detection logic (simple)¶
- A person is detected ->
person_detected - A
bottle/cupstays for a certain time and no person is nearby ->possible_littering
The following diagram shows when webcam mode emits each of the two events.
flowchart TB
CAM["camera image (one frame)"] --> YOLO["detect objects with YOLO"]
YOLO --> P{"Is a person<br>in the frame?"}
P -->|"yes"| PE["person_detected"]
YOLO --> L{"Has a bottle / cup stayed<br>in the same place<br>for 8 s or more?"}
L -->|"yes"| A{"Has no person been<br>nearby for 5 s or more?"}
A -->|"yes"| LE["possible_littering"]
L -->|"no"| W["not emitted yet"]
A -->|"no"| W
PE --> OUT["write an event file"]
LE --> OUT
The 8 s and 5 s are the values set with --linger-seconds and --person-away-seconds in the step 2 command. person_detected is emitted only when 12 s or more (the --person-event-interval default) have passed since the last one. possible_littering is emitted only once per object.
4. Check points¶
*_webcam_event_*.txtfiles are generated- The
event_typein the file isperson_detectedorpossible_littering - The
mediator-ownerterminal printsProduct deployed: 0x... for file 401_webcam_event_...txt
Success example¶
- mock mode generates event files without camera hardware
- webcam mode outputs
person_detectedorpossible_littering
5. Note¶
This detection is a simple rule of thumb (heuristic) for learning, not a strict judgment.
Troubleshooting¶
- Symptom: the camera cannot be opened
- Check: another application is not using the camera
- Check: another
--camera-indexwas tried - Check (macOS): System Settings → Privacy & Security → Camera allows your terminal app. Restart the terminal after allowing it (without the permission,
[webcam-bridge] error: failed to open webcam index=0is printed) - Symptom: the expected event does not appear
- Check: lighting and framing are appropriate
- Check: the pipeline works in mock mode first
- Symptom: no event file is created
- Check: the output folder (
../mediator-owner/raw_data/output) exists. If not, create it withmkdir -p ../mediator-owner/raw_data/output - Symptom: events are not registered as merchandise (
Product deployedis not printed) - Check:
mediator-owneris running - Check: the output path matches
mediator-owner/raw_data/output - Check: the course repository is up to date (after
git pull, restartmediator-owner)
Next steps¶
One page from 1.2 is enough. Next is section 1.3 of Hands-on, where you buy and receive the item you listed.
- 1.3 uses the Quickstart (run the stack) stack (terminals 1–7). Keep it running.
- In step 1 of 1.3 (listing), you output event files with the mock mode of this page.
- 1.3 does not use the publisher. Keep the publisher from 1.1 stopped, because it shares port 8080 with the Quickstart IPFS.
References¶
[1] OpenCV, "OpenCV," https://opencv.org/