Sep-07-2023, 11:14 AM
(This post was last modified: Sep-07-2023, 02:02 PM by deanhystad.)
Hello. Greeting to you all. I'm totally new, today i wrote my very first bunch of code. I got a problem with sounds within my project. As i managed to do a simple app that helps my to classify my WMS data, im helpless with audio. Audio is kind of not related to my csv specified cells, while i organised it well.
CSV contains:
header1;header2;header3 etc
soundlink1;soundlink2;soundlink3
data1;data2;data3
When i input my data (exact EAN product code), app tells my where to put this product. This works well.
Now, i wanted to add some sounds, because i dont need to look on screen to know where to put this ean. Sound are like "put it on shelf 1", "shelf" 2 etc .
Problem is, that even if my EANs seems to work well with header, my audio is playing randomly. While text says "Put it on shelf 3", audio plays "put it on shelf 1". It doesnt relate to exact file that should be played. What's wrong in that case? Would some1 be so nice and help me with that?
PY file:
CSV contains:
header1;header2;header3 etc
soundlink1;soundlink2;soundlink3
data1;data2;data3
When i input my data (exact EAN product code), app tells my where to put this product. This works well.
Now, i wanted to add some sounds, because i dont need to look on screen to know where to put this ean. Sound are like "put it on shelf 1", "shelf" 2 etc .
Problem is, that even if my EANs seems to work well with header, my audio is playing randomly. While text says "Put it on shelf 3", audio plays "put it on shelf 1". It doesnt relate to exact file that should be played. What's wrong in that case? Would some1 be so nice and help me with that?
PY file:
from flask import Flask, render_template, request, jsonify import os import pygame app = Flask(__name__) # Pygame pygame.init() # CSV path csv_path = os.path.join(os.path.dirname(__file__), 'dane.csv') # get data from CSV segments = {} sound_paths = [] with open(csv_path, 'r') as file: lines = file.readlines() headers = lines[0].strip().split(';') sound_paths = lines[1].strip().split(';') data = lines[2:] for i, header in enumerate(headers): segments[header] = [] for line in data: values = line.strip().split(';') segments[header].append(values[i]) @app.route('/') def index(): return render_template('index.html') @app.route('/check_segment', methods=['POST']) def check_segment(): input_string = request.form['input_string'] # Index itering to choose segment for segment_name, values in segments.items(): if input_string in values: indices = [i for i, v in enumerate(values) if v == input_string] if len(indices) == 1: # if there's exact match index = indices[0] sound_path = sound_paths[index] # play sound try: pygame.mixer.music.load(os.path.join('static', sound_path)) pygame.mixer.music.play() return jsonify({'result': segment_name}) except Exception as e: print(f"Error while playing sound {e}") return jsonify({'result': 'Error while playing sound.'}) return jsonify({'result': 'There's no matching segment.'}) if __name__ == '__main__': app.run(debug=False)
deanhystad write Sep-07-2023, 02:02 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.