Python_Virtual_Motion-JPEG_Camera_Simulator

English 简体中文 繁体中文 Tiếng Việt
Summary

The Python Virtual Motion-JPEG Camera Simulator offers a lightweight, extensible service for generating realistic MJPEG video streams over HTTP(s) via a Flask web interface. It supports diverse video sources, including physical cameras, external streams, image datasets, and screen or application window captures. The system comprises a Virtual Camera Client Library for stream generation, a Flask Camera Server mimicking Axis IP camera UIs with authentication and configuration, and a Multi-Camera View Monitor Dashboard for aggregating multiple feeds. This solution is ideal for developers building cyber ranges, digital twins, honeypots, or testing surveillance clients without requiring physical camera hardware.

專案設計目的:此專案旨在建立一個輕量級、可擴展的攝影機模擬服務程式,該程式透過 Flask 網頁介面,經由 HTTP(s) 公開 Motion-JPEG (MJPEG) 視訊串流,以便在網路靶場、紅/藍演練和研究中使用,在這些場景中,需要逼真的攝影機端點,但又沒有實體硬體。

模擬的視訊串流可以從五種不同類型的來源產生:

  • 本機實體攝影機(嵌入式網路攝影機或 USB 攝影機)。
  • 外部直播串流(RTSP/HTTP 來源)。
  • 靜態或旋轉影像資料集(從目錄載入的影像)。
  • 作業系統螢幕錄製(完整或部分螢幕截圖)。
  • 應用程式視窗擷取(僅限 Windows;擷取正在執行的應用程式視窗)。

此模擬器有意模仿 Axis IP cameras 的網頁 UI 和行為,因此也可以部署為可信的攝影機蜜罐,或作為測試和整合的直接替代品。

# Author:      Yuancheng Liu
# Created:     2025/10/15 
# version:     v_0.0.5
# Copyright:   Copyright (c) 2025 LiuYuancheng
# License:     GNU General Public License V3

簡介

IoT/IP 攝影機是現代 IT/OT 系統中的關鍵感測器之一,用於監視、安全監控和操作可見性。在數位分身和模擬環境中(其中 MU、PLC、RTU 甚至火車或跑道燈等實體世界實體都在軟體中建模),通常缺少逼真的攝影機端點。Python Virtual Motion-JPEG Camera Simulator 旨在填補這一空白,透過產生可信的 MJPEG 攝影機串流,這些串流可以整合到數位分身、網路靶場、監控儀表板和欺騙/蜜罐部署中。

此專案提供了一個輕量級、可攜式的模擬器,可以在 VM 或容器中執行,並透過 HTTP(s) 公開具有 Axis 風格網頁 UI 的 MJPEG 串流。系統工作流程圖如下所示:

Figure-01: System Workflow Diagram, version v_0.0.3 (2025)

視訊串流可以由即時裝置、預先錄製的資料集或主機上的擷取產生,因此模擬器可以呈現上下文相關的視訊 - 例如,當數位分身指示飛機正在最後進場時,在模擬跑道攝影機上顯示著陸飛機。

架構概觀

如下面的架構圖所示,該系統包含三個主要元件:

Figure-02: System Architecture Diagram, version v_0.0.3 (2025)

  • Virtual Camera Client Library:將各種視訊/影像來源轉換為具有可配置 FPS 和解析度的 Motion-JPEG (MJPEG) 串流。支援的來源:本機網路攝影機/USB 攝影機、RTSP/HTTP 串流、影像資料集、桌面螢幕截圖(完整/部分)和應用程式視窗擷取(僅限 Windows)。

  • Flask Camera Server:一個模仿 Axis 風格攝影機頁面的管理網頁應用程式。它公開攝影機配置、串流端點、使用者控制和日誌 - 使模擬器既可用作逼真的測試攝影機,又可用作令人信服的蜜罐 UI。

  • Multi-Camera View Monitor Dashboard:一個儀表板程式,將多個虛擬攝影機饋送聚合到一個多畫面視圖中,以便在指揮中心進行監控或投影。

系統設計

本節介紹系統三個主要元件的詳細設計和內部結構,這些元件在 Introduction[Architecture Overview] 節中介紹。

Virtual Camera Client Library 的設計

Virtual Camera Client Library 負責將各種視訊來源轉換為可以提供給瀏覽器或其他應用程式的連續 MJPEG 串流。此模組的核心是基底類別 camClient,它定義了由五個主要步驟組成的串流管道:

步驟 1:從來源擷取畫面

  • 每個子類別都實現 camClient 介面函數 getOneFrame(),以從其指定的視訊來源檢索一個 OpenCV (cv2) 影像畫面。此函數確保所有來源類型都具有一致的畫面物件。

步驟 2:影像 JPEG 編碼

  • 每個擷取的畫面都被壓縮成 JPEG 格式,以減少頻寬並實現高效串流:

  • _, buffer = cv2.imencode('.jpg', frame)

步驟 3:傳回 HTTP 串流

  • Flask 產生器持續產生每個編碼的 JPEG 畫面到 HTTP 回應串流:

  • yield (b'--frame\r\n'
           b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')

步驟 4:Multipart MJPEG 回應

  • HTTP 回應使用 MIME 類型 multipart/x-mixed-replace 發送,允許瀏覽器將其解釋為連續的 JPEG 影像串流:

  • mimetype='multipart/x-mixed-replace; boundary=frame'

步驟 5:瀏覽器呈現直播串流

  • 在瀏覽器的 元素中,依序編碼每個傳入的 JPEG 畫面並顯示它們 - 在視覺上建立視訊:

類別結構和來源繼承

camClient 基底類別由幾個專門的子類別擴展,這些子類別處理不同的輸入來源:

Figure-04: Class Structure Diagram, version v_0.0.3 (2025)

每個子類別管理其自己的擷取邏輯,同時為 Flask 伺服器和儀表板維護統一的串流介面。

Flask Camera Server 的設計

Flask Camera Server 是一個網頁主機,提供使用者介面、視訊直播檢視、配置選項和用於視訊串流的 API 端點。它還支援安全存取控制,並與網路靶場或數位分身環境中的實體世界模擬資料連結。Flask 伺服器的操作流程/結構如下所示:

Figure-04: Flask Server Workflow Diagram, version v_0.0.3 (2025)

四個模組的功能詳細資訊包括:

  • Video Source Manager Module:封裝模組匯入並與 Virtual Camera Client Library 整合,以管理從多個視訊來源擷取畫面。

  • User and Access Management Module:處理身份驗證和授權,連結到憑證資料庫以管理使用者登入和 MJPEG 擷取 API 存取權限。

  • Data Manager Module:資料管理員模組與網路靶場的實體世界模擬器介接,以將操作狀態(例如,飛機位置、火車移動)對應到適當的攝影機視訊顯示饋送畫面。

  • Flash Web Service Module:主要網頁服務模組開啟一個可配置的埠,以處理所有 http/https 請求。

基於 Flask 的網頁伺服器為使用者提供四個主要頁面,如下所示:

[1] Camera Home Page

當使用者存取攝影機模擬器 IP 位址時的預設登陸頁面,提示使用者在使用有效憑證登入後才能存取攝影機系統:

Figure-05: IP Camera Home Page Screenshot, version v_0.0.3 (2025)

[2] Camera Video Live View Page

使用者使用正確的憑證登入後,他們可以存取顯示目前直播 MJPEG 串流的頁面。使用者可以從頁面即時調整畫面解析度和 FPS:

Figure-06: Camera Video Live View Page Screenshot, version v_0.0.3 (2025)

[3] User Configuration Page

允許不同類型的使用者管理和變更存取憑證。目前版本提供 2 種類型的使用者:

  • Normal users 只能檢視直播串流並變更自己的密碼。

  • Admin users 可以新增/刪除使用者、重設/檢查密碼,以及管理 MJPEG API 權杖。

Figure-07: User Configuration Page Screenshot, version v_0.0.3 (2025)

[4] Access Token Configuration Page

用於為外部應用程式產生和管理 MJPEG API 存取權杖。對於其他使用 motion-JPEG API 擷取畫面的程式,在呼叫 API url 時需要有效的存取權杖。只有管理員使用者才能存取 API 權杖配置頁面,如下所示:

Figure-08: Access Token Configuration Page Screenshot, version v_0.0.3 (2025)

有 2 種類型的 API 存取權杖:

  • Fixed Token:固定權杖已在攝影機本機資料庫中預先配置,並且沒有使用限制。如果我們重新啟動攝影機模擬器程式,固定權杖將不會遺失。

  • Temporary Token:臨時權杖儲存在攝影機的記憶體中,每次管理員按下「產生隨機權杖」按鈕時,攝影機模擬器都會產生一個 16 個字元的臨時權杖。管理員還可以設定臨時權杖的有效期限。當攝影機模擬器重新啟動時,所有臨時權杖都將遺失。

使用者存取規則和可用功能如下表所示:

功能\使用者 管理員使用者 一般使用者
存取使用者管理頁面
變更自己的密碼
變更和檢查其他使用者的密碼
新增和移除一般使用者
建立新的管理員使用者
移除現有的管理員使用者
存取 motion-JPEG 權杖管理頁面
檢視固定和臨時 API 權杖
產生、修改和刪除臨時 API 權杖

Multi-Camera View Monitor Dashboard 的設計

Multi-Camera View Monitor Dashboard 將多個虛擬攝影機串流聚合到一個統一的監控介面中。它透過 HTTP API 呼叫擷取 MJPEG 視訊饋送,並將它們顯示在可配置的多畫面佈局中。攝影機模擬器和儀表板的網路拓撲和配置如下所示:

Figure-09: Monitor Dashboard Network Diagram, version v_0.0.3 (2025)

顯示儀表板功能包括:

  • 支援同時監控多個 MJPEG 饋送。

  • 可配置的網格佈局(例如,2x2、3x3)用於自訂顯示設定。

  • 可調整的串流 FPS 和解析度,用於效能調整。

  • 允許多個儀表板訂閱相同的攝影機串流

儀表板的攝影機存取配置檔案

每個攝影機饋送都在 JSON 配置檔案中定義。以下是將新攝影機新增到儀表板的範例:

    "Desktop1": {
        "name": "Desktop screenshot 1 virtual Camera",
        "url": "http://127.0.0.1:5000/cgi-bin/mjpg/",
        "token": "motionJPEG",
        "size": [
            640,
            480
        ]
    },    

儀表板支援彈性的佈局配置、可調整的 FPS,並且可以在單個視窗或多個顯示器上顯示多個攝影機串流。

系統配置和使用

開發環境:Python 3.7.4+

專案檔案清單和模組功能說明

Virtual Camera Client Library

程式檔案 執行環境 說明
src/lib/virtualCamera.py python 3 虛擬攝影機 lib 模組
src/lib/virtualCameraTest.py + templates python 3 + HTML 虛擬攝影機 lib 測試案例模組

Flask Camera Server

程式檔案 執行環境 說明
src/VirtualCam/static/* CSS, JS,Image 所有網頁 CSS、JavaScript 和影像
src/VirtualCam/templates/* HTML 網頁的所有 html 頁面。
src/VirtualCam/Config_templat.txt   使用者建立自己的配置檔案的配置範本檔案。
src/VirtualCam/users_template.json JSON 使用者憑證資料庫檔案範本。
src/VirtualCam/webCamGlobal.py python 3 全域參數模組。
src/VirtualCam/webCamDataMgr.py python 3 可選模組,用於連結到網路靶場實體世界模擬模組。
src/VirtualCam/webCamAuth.py python 3 Flask 使用者授權模組。
src/VirtualCam/webCamApp.py python 3 主要 Flask 網頁主機執行模組。

Multi-Camera View Monitor Dashboard

程式檔案 執行環境 說明
src/MultiCamViewDashboard/camDashboardConfig_template.txt   儀表板配置範本檔案。
src/MultiCamViewDashboard/camDashboardDataMgr.py python 3 從所有攝影機擷取 MJPEG 影像的子執行緒模組
src/MultiCamViewDashboard/camDashboardGlobal.py python 3 全域參數模組。
src/MultiCamViewDashboard/camDashboardPanel.py python 3 儀表板顯示面板模組。
src/MultiCamViewDashboard/cameraConfig_template.json JSON 攝影機存取配置檔案範本。
src/MultiCamViewDashboard/camDashboardRun.py python 3 儀表板主要執行模組。

系統預先設定

在配置系統之前,請安裝下表中其他 lib/軟體:

Lib 模組 版本 安裝 Lib 連結
Flask 1.1.2 pip install Flask https://flask.palletsprojects.com/en/stable/
Flask_Login 0.6.2 pip install Flask-Login https://pypi.org/project/Flask-Login/
numpy 1.21.6 pip install numpy https://pypi.org/project/numpy/
opencv_python 4.5.1.48 pip install opencv-python https://pypi.org/project/opencv-python/
PyAutoGUI 0.9.53 pip install PyAutoGUI https://pyautogui.readthedocs.io/en/latest/
pywin32 305 pip install pywin32 https://pypi.org/project/pywin32/
requests 2.28.1 pip install requests https://pypi.org/project/requests/
win32gui 221.6 pip install win32gui https://pypi.org/project/win32gui/
wxPython 4.1.0 pip install wxPython https://pypi.org/project/wxPython/

Virtual Camera \ 嘅使用方法

Work Folder : src/VirtualCam

Step 1: 設定 web configuration file。

將 configuration file template Config_template.txt 重新命名為 Config.txt,並按照以下所示設定參數:

# This is the config file template for the module 
# Setup the parameter with below format (every line follows : format, the
# key can not be changed):
#-----------------------------------------------------------------------------
# Camera mode flag, 
CAM_MD:2
#-----------------------------------------------------------------------------
# Camera Admin user config and user record file. 
USERS_RCD:users.json
#-----------------------------------------------------------------------------
# Define physical world simulator IP
RW_IP:127.0.0.1
# Define physical world simulator connection port
RW_PORT:3001
RW_REFRESH_TIME:1
# Physical world reconnection time 
RW_RECONN_TIME:10
#-----------------------------------------------------------------------------
# Camera video source parameter:
# Simulated camera report to RW ID:
CAM_ID:RW_CAM_REAL
# Physical camera ID
CAM_IDX:0
CAM_FPS:6
# Simulated camera data set parameters:
CAM_DATA_DIR:takeoff
CAM_DATA_PREFIX:takeoff-
CAM_DATA_START_IDX:6
CAM_DATA_END_IDX:53
#-----------------------------------------------------------------------------
# Init the Flask app parameters
FLASK_SER_PORT:5000
FLASK_DEBUG_MD:False
FLASK_MULTI_TH:True
FLASK_FIXED_TOKEN:motionJPEG

Step 2: 設定 video source

在 configuration file 中設定 camera mode flag 參數:

  • CAM_MD:1 - From real camera

  • CAM_MD:2 - From image data set

  • CAM_MD:3 - From desktop screen recording

  • CAM_MD:4 - From Windows application.

webCamApp.py 中,設定 video source,如下所示:

if gv.gCamMode == 0:
    gv.iCamMgr = cam.camClientReal(gv.gCamSrc, fps=gv.gCamFps)
elif gv.gCamMode == 1:
    gv.iCamMgr = cam.camClientSimu(gv.gCamDir, gv.gCamFilePrefix, fps=gv.gCamFps)
    gv.iCamMgr.setShowTimestamp(True)
    gv.iCamMgr.setTestMode(56)
elif gv.gCamMode == 2:
    gv.iCamMgr = cam.camClientScreen(fps=gv.gCamFps)
else:
    windowName = "templates - File Explorer"
    #windowName = "2D Airport CAT-II Runway Light System Simulation"
    #windowName = "Microsoft Edge"
    gv.iCamMgr = cam.camClientWinApp(windowName)

要查找整個應用程式視窗名稱,請將滑鼠移至 Windows 工作列,然後按一下頂部彈出的小視窗以顯示視窗名稱,如下所示:

從 Windows App 擷取時,應用程式不能最小化。

Step 3: 設定 user authorization file

users_template.json 重新命名為 users.json,並新增 user name 和 password,如下所示:

    "admin": {
        "username": "admin",
        "password": "admin",
        "usertype": "admin"
    },

Step4: 執行 virtual camera program

執行 flask web host 並開啟 URL:http://:

python webCamApp.py

要使用 GET request 呼叫 motion JPEG API,請從 token page 複製 fix 或 temporary token,如下所示:

然後使用 http GET request 取得 url http://:/cgi-bin/mjpg/ 上 virtual camera 的 frame,如下所示:

response = requests.get(http://:/cgi-bin/mjpg/, timeout=1)

Camera View Dashboard \ 嘅使用方法

Step 1: 設定 dashboard configuration file。

將 configuration file template camDashboardConfig_template.txt 重新命名為 `camDashboardConfig.txt,並按照以下所示設定參數:

# This is the config file template for the module 
# Setup the parameter with below format (every line follows : format, the
# key can not be changed):
#-----------------------------------------------------------------------------
# Test mode:
# - True: run the UI with out connect to the cameras.
# - False: connect to the cameras and fetch the vide stream.
#TEST_MD:True
TEST_MD:False
#-----------------------------------------------------------------------------
# Define all the HMI UI config parameters
# define UI title name 
UI_TITLE: Multi-Camera View Monitor Dashboard
# Define update clock interval
CLK_INT:0.5
#-----------------------------------------------------------------------------
# camera connection configuration file.
CAM_CONFIG_FILE:cameraConfig.json

Step2: 設定 cameras connection configuration file

將 configuration file template cameraConfig_template.json 重新命名為 cameraConfig.txt,從相關 camera 嘅 token page 複製 access token (Fixed/Temporary),並將您想要存取嘅 camera 新增到檔案中,如下所示:

    "Desktop1": {
        "name": "Desktop screenshot 1 virtual Camera",
        "url": "http://127.0.0.1:5000/cgi-bin/mjpg/",
        "token": "motionJPEG",
        "size": [
            640,
            480
        ]
    },   

Step3: 執行 dashboard

確保所有 virtual camera 都在執行中,並使用 cmd 執行 dashboard:

python camDashboardRun

然後 dashboard 將顯示如下:

未設定或未連接嘅 camera 將顯示「no video」。

Program Typical Use Cases

該系統設計用於構建 OT cyber range 並支援 cyber exercise,可能嘅 use case 包括:

  • 為 blue/red team exercise 和 cyber range 提供 instrumented camera endpoint。

  • 部署 believable camera honeypot 以捕獲 attacker 行為並構建 dataset。

  • 將 camera feed 整合到 digital-twin scenario 中(例如,顯示基於 simulated operation 嘅 state-dependent video)。

  • 在沒有 physical camera 嘅情況下測試和驗證 surveillance client、analytics pipeline 和 dashboard。

  • 從 virtual machine 和 host application 聚合各種 video source,以進行 demonstration、QA 或 ML dataset generation。

我們還將展示兩個 use case,說明如何使用此 project 在 aviation runway cyber range 中模擬 4 個不同嘅 camera,並在 cyber exercise 中監控 railway cyber range HMI。

Usage Case as Cyber Range Surveillance System

該系統用於 Aviation runway light management system cyber range 中,以模擬 tower operator 嘅 surveillance camera 監控系統,以顯示如下所示嘅四個 camera。這四個 camera 包括:

  1. Airport runway landing area surveillance camera

  2. Airport runway takeoff area surveillance camera

  3. Tower operational room surveillance camera

  4. Cyber range physical world simulator view display camera

Figure-10: Cyber Range Surveillance System Dashboard, version v_0.0.3 (2025)

Usage Case as Cyber Exercise Main Projection Screen

該系統用於監控在 Land Based Railway IT-OT System Cyber Security Cyber Range System 嘅不同 VM 上運行嘅所有 UI program,並在 cyber exercise 中投影到大電視上,如下所示。4 個受監控嘅 program 包括:

  1. Railway cyber range physical world simulator

  2. HQ railway track signaling system monitor HMI

  3. HQ railway trains monitor and control system HMI

  4. HQ railway management HMI

Figure-11: Cyber Exercise Main Projection Screen, version v_0.0.3 (2025)

GitHub Link: 

https://github.com/LiuYuancheng/Python_Virtual_MotionJPEG_Camera

感謝您花時間查看文章詳細資訊,如果您有任何問題和建議或發現任何 program bug,請隨時給我留言。如果您能提供一些評論並分享任何改進建議,我們將不勝感激,以便我們能夠改進我們嘅工作~

Last edit by LiuYuancheng ([email protected]) at 03/11/2025, if you have any problem please free to message me.

  RELATED

No related programming articles found. Browse all programming tutorials and articles.

  COMMENTS

0

No comment for this article.