Ändern Sie die Bilder in einem Fenster über Echtzeit -MIDI -NachrichtenPython

Python-Programme
Anonymous
 Ändern Sie die Bilder in einem Fenster über Echtzeit -MIDI -Nachrichten

Post by Anonymous »

Ich versuche, ein Programm zu erstellen, das in Bildern aus einem Dire aus einer Liste in einer Liste in Bildern gelesen wird. Verwenden Sie den Note -Wert und den passenden Index aus der Liste von Bildern < /li>
< /ul>
< />
< /ul>
Ich habe viele Beispiele dafür gesehen, dass ich die oben genannten separat tun kann, aber ich bin festgefahren, wie man sie alle in einem kombinieren kann. Ich war nicht in der Lage, Beispiele für Bindungen an MIDI -Nachrichten zu finden - nur für Tasten- und Tastendrucke. Nach dem Schließen des Fensters kann ich Midi -Notizen in der Streaming -Ausgabe sehen, aber kein Fenster für Bilder ist sichtbar. Ein Bild wird im Fenster nie angezeigt.

Code: Select all

import matplotlib
import os, sys
import mido
import cv2

def get_images(path: str) -> list:
"""
Recursively crawl through dir and load in all images
"""
images = []
image_files = glob.glob(os.path.join(path, '*'))
print(image_files)

for i in image_files:
images.append(cv2.imread(i))

return images

def get_midi_port():
# Get a list of available input port names
input_ports = mido.get_input_names()

# Get first port that is not a Through type
for p in input_ports:
if "Midi Through" not in p:
print(p)
port = p
return p

if not input_ports or not p:
raise Exception("No MIDI input ports found.")

class MIDI_Images():
def __init__(
self,
path="."
):
self.path = path
self.loaded_images = get_images(self.path)
self.curr = 0
if auto_start:
self.fig, self.ax = plt.subplots()
self.display()
plt.show(block=False) ## opens window but no image
plt.pause(0.1)
### plt.show() ## shows image but code doesn't continue for MIDI notes

### Ingest MIDI messages
try:
p = get_midi_port()

with mido.open_input(p) as inport:
inport.poll()
print(f"Listening for MIDI messages on port: {inport.name}")
while True:
for msg in inport.iter_pending():
print(msg)
self.curr = msg.note - 24 ## lowest note on my controller is 24
self.update_image()

except Exception as e:
print(f"Error: {e}")

def update_image(self):
"""
Load in next image
"""

# advance to next image
try:
self.im.set_array(self.loaded_images[self.curr])
self.display()
## self.fig.canvas.draw_idle()  ## waits for image window to close if uncommented
except IndexError:
print("Sorry no image in index: ", n)

def display(self):
"""
Orchestrating function to run
"""

image = self.loaded_images[self.curr]

self.im = self.ax.imshow(image)

### Examples of key bindings working just fine
# self.fig.canvas.mpl_connect("key_press_event", self.next_image)

if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser(description='')

parser.add_argument('--imagedir', action="store", dest='imagedir', default='')

args = parser.parse_args()

MIDI_Images(args.imagedir)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post