1. Controlling MXW through Python
Python is used in MXW trough a plugin interface. Specified program paths are searched for plugins which are registered, and then loaded through the user interface.
As of version 7.2., plugins are available in the playlist. There are two paths searched during program startup, the program folder and the user folder:
- (Program folder)/plugins/playlist/python/
- ~.StageDesigner/plugins/playlist/python/ (Unix)
- ~.StageDesigner/plugins/playlist/python/ (Windows)
Each plugin resides in a folder. Inside this folder, two files have to be present,
- mxw_plugin.ini
- mxw_main.py
The file mxw_plugin.ini defines the registration of the plugin. The following fields are valid:
Tag | Use | Values/Example |
---|---|---|
plugin_version | Plugin Version | 1 |
plugin_script_language | Plugin Script Language | Python |
plugin_action_level | Plugin Activity Switch | disabled = not visible |
plugin_menu_parent | Top level parent in playlist menu | "AI", "IO" .. |
plugin_menu_name | Playlist menu entry | "Face detection" |
plugin_grid_name | Default name in grid (may be completed via script) | "Face detect" |
plugin_grid_bg_color | Default color in grid (may be changed via script) | 0.95 0.05 0.45 1.00 (RGBA with range 0..1) |
plugin_tooltip | Tool tip in grid and panel | "This plugin triggers the playlist when it finds a Face" |
An example mxw_plugin.ini looks like this:
[mxw_plugin] ; must be here
plugin_version = 1 ; must be V1 (as of V7.2)
plugin_script_language = Python ; must be Python (as of V7.2)
plugin_menu_parent = AI
plugin_menu_name = NeuronalNet(Dlib) Face Detect Plugin
plugin_grid_name = Face Detect Plugin
plugin_grid_bg_color = 0.950000 0.050000 0.450000 1.000000 ; set plugin color.
plugin_tooltip = This plugin triggers the playlist when it finds a Face using a neuronal net (Dlib)
The plugins are defined in a mxw_main.py file. This file is loaded, and
import tempfile
import pickle, codecs # load / store
import mxw, mxw_imgui # for mxw interaction, mxw ui interaction
import cv2 # image processing
import numpy as np # math
# example per-instance-storage: create a dictionary
# and use it with 'item_id' as key (this integer is set by host application before every function call)
class video_writer:
capture_device=""
instance_storage = {}
videosize = (640,480)
# -----------------------------------------------------------------------------------
def onCreate():
v = video_writer()
dev = mxw.media().get_capture_device_names()
v.capture_device = dev[1]
instance_storage[item_id] = v
return
# save and load: you can serialize into a string
def onSave():
serialized = codecs.encode(pickle.dumps(instance_storage[item_id]), "base64").decode()
return serialized
def onLoad( serialized ):
instance_storage[item_id] = pickle.loads(codecs.decode(serialized.encode(), "base64"))
return
def onAction():
v = instance_storage[item_id]
v.f = tempfile.NamedTemporaryFile(suffix='.avi')
v.f.close()
fourcc = cv2.VideoWriter_fourcc('M','P','4','V')
v.out = cv2.VideoWriter(v.f.name, fourcc, mxw.fps, videosize)
m = mxw.media(v.capture_device)
if(m.isvalid()):
m.reference(True)
return
def onPostAction():
v = instance_storage[item_id]
v.out.release()
mxw.preload(1).set_media(v.f.name)
m = mxw.media(v.capture_device)
if(m.isvalid()):
m.reference(False)
return
def onNewFrameInPlayoutCue():
v = instance_storage[item_id]
m = mxw.media(v.capture_device)
if(m.isvalid()):
img = m.get_image_sample_cvmat(videosize[0],videosize[1])
img = np.array(img, copy=False)
img = cv2.flip(img, 0)
v.out.write(img)
return
# render in panel for settings etc
def onRenderPanel():
v = instance_storage[item_id]
mxw_imgui.text_unformatted("This plugin records a camera")
if(hasattr(v,'f')):
mxw_imgui.text_unformatted(v.f.name)
if(hasattr(v,'out') and v.out.isOpened()):
mxw_imgui.text_unformatted("Recording")
else:
mxw_imgui.text_unformatted("Not recording")
dev = mxw.media().get_capture_device_names()
a = mxw_imgui.combo("Capture Device", dev.index(v.capture_device), dev)
if(a[0]):
print(str(a[1]))
v.capture_device = dev[a[1]]
return