I am using python 3 and with alsaaudio I read data from microphone but I have to work with each channel separately. So is there a way how to get data just from one channel? Or how to parse data from each channel separately?
import wave
import sys
import threading
import time
import os
import alsaaudio, audioop
# Input
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
inp.setchannels(2)
inp.setrate(8000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(80)
# Output file
output = wave.open("test2.wav",'wb')
output.setnchannels(2)
output.setsampwidth(2)
output.setframerate(8000)
while True:
l,frames = inp.read()
if l>0:
print(frames)
output.writeframes(frames)
Related
I have built a simple Dask application to use multiprocessing to loop through files and create summaries.The code is looping through all the zip files in the directory and creating a list of names while iterating through the files( Dummy task). I was not able to either print the name or append it in the list. what's the issue, i cant figure out.
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
plt.ioff()
import time
import os
from pathlib import Path
import glob
import webbrowser
from dask.distributed import Client
client = Client(n_workers=4, threads_per_worker=2) # In this example I have 8 cores and processes (can also use threads if desired)
webbrowser.open(client.dashboard_link)
print(client)
os.chdir("D:\spx\Complete data\item_000027392")
csv_file_list=[file for file in glob.glob("*.zip")]
total_file=len(csv_file_list)
data_date=[]
columns=['Date', 'straddle_price_open', 'straddle_price_close']
summary=pd.DataFrame(columns =columns)
def my_function(i):
df=pd.read_csv(Path("D:\spx\Complete data\item_000027392",csv_file_list[i]),skiprows=0)
date = csv_file_list
data_date.append(date)
print(date)
return date
futures = []
for i in range(0,total_file):
future = client.submit(my_function, i)
futures.append(future)
results = client.gather(futures)
client.close()
The idea is that I should be able to make operations on the data and print outputs and charts while using dask but for some reason i can't.
Here is the code:
Server:
import cv2
import numpy as np
import imagezmq
image_hub = imagezmq.ImageHub()
while True: # show streamed images until Ctrl-C
win_name = "Feed"
image = image_hub.recv_image()
img = cv2.imdecode(image, 1)
cv2.imshow(win_name, img)
cv2.waitKey(1)
image_hub.send_reply(b'OK')
Client:
import socket
import time
from imutils.video import VideoStream
import imagezmq
import cv2
import numpy as np
sender = imagezmq.ImageSender(connect_to='tcp://192.168.0.12:5555')
vs = VideoStream(src=0).start()
time.sleep(2.0)
while True: # send images as stream until Ctrl-C
frame = vs.read()
img_arr = np.array(bytearray(frame))
sender.send_image("Img",img_arr)
And i get the error:
img = cv2.imdecode(image, 1)
TypeError: Expected Ptr<cv::UMat> for argument 'buf'
on the server
I have checked online but could not find a suitable answer
Note: i am not that experienced in this.
according to imagezmq's documentation, one does not use any imdecode, and one does not use any bytearray() calls.
you just send numpy arrays back and forth.
further, I would advise against using imutils, in particular the VideoStream wrapper. use OpenCV's VideoCapture. check for isOpened() before trying to read. when reading with ok, frame = cap.read(), check if the read frame is ok, otherwise break the reading loop and discard the value in frame.
I am trying to generate keys using os.urandom() and base64 methods. Please see the below code. gen_keys() itself may not be very slow, but
the script overall run time is very slow. For example, gen_keys() takes
about 0.85 sec where as the overall script run time is is 2 minutes 6 seconds. I suspect this is some thing to do with module imports. Although I need all of the modules from my script.
Any thoughts on the real issue? Thanks.
I am using python3.4
#!/usr/bin/env python3
import netifaces
import os
import subprocess
import shlex
import requests
import time
import json
import psycopg2
import base64
def gen_keys():
start_time = time.time()
a_tok = os.urandom(40)
a_key = base64.urlsafe_b64encode(a_tok).rstrip(b'=').decode('ascii')
s_tok = os.urandom(64)
s_key = base64.urlsafe_b64encode(s_tok).rstrip(b'=').decode('ascii')
print("a_key: ", a_key)
print("s_key: ", s_key)
end_time = time.time()
print("time taken: ", end_time-start_time)
def Main():
gen_keys()
if __name__ == '__main__':
Main()
$~: time ./keys.py
a_key: 52R_5u4I1aZENTsCl-fuuHU1P4v0l-urw-_5_jCL9ctPYXGz8oFnsQ
s_key: HhJgnywrfgfplVjvtOciZAZ8E3IfeG64RCAMgW71Z8Tg112J11OHewgg0r4CWjK_SJRzYzfnN-igLJLRi1CkeA
time taken: 0.8523025512695312
real 2m6.536s
user 0m0.287s
sys 0m7.007s
$~:
In python I have written something like this;
script = """
import network
from machine import Pin, PWM
from time import sleep
"""
And I want to write something after it but without deleting the old ones. How do I do?
You can append a string to a string like following
>>> script = """
... import network
... from machine import Pin, PWM
... from time import sleep
... """
>>> script += "\nimport os"
You could put the script into a template and then fill in the values. This might be easier to manage than concatenating strings if your generated script is even moderately complex.
# script.template
import network
from machine import Pin, PWM
from time import sleep
${xyz}
# script-generator.py
from string import Template
with open('script.template') as f:
template = Template(f.read()
contents = template.substitute(xyz='xyz')
with open('main.py', 'w') as f:
f.write(contents)
Or you could use str.format() like this if a separate template file seems like overkill:
script = """\
import network
from machine import Pin, PWM
from time import sleep
{xyz}
"""
data = {'xyz': 'xyz'}
with open('main.py', 'w') as f:
f.write(script.format(**data))
odbname = data.jobname + '.odb'
mySession = session.openOdb(name = odbname)
myViewport = session.viewports["Viewport: 1"]
#plot stress
myViewport.setValues(displayedObject=mySession)
myViewport.odbDisplay.display.setValues(plotState=(CONTOURS_ON_DEF,))
myViewport.view.fitView()
session.viewports['Viewport: 1'].viewportAnnotationOptions.setValues(
legendFont='-*-verdana-medium-r-normal-*-*-120-*-*-p-*-*-*')
when i run this program am able to see the view iso i need to get the view in front view direction so can anyone tell me how to change to view using python coding
this are my import module
from abaqus import * # from the main library
from caeModules import * # import the modules
from abaqusConstants import * # constants we need
from math import fabs
from abaqus import backwardCompatibility
backwardCompatibility.setValues(reportDeprecated=False)
import section
import regionToolset
import displayGroupMdbToolset as dgm
import part
import material
import assembly
import step
import interaction
import load
import mesh
import optimization
import job
import sketch
import visualization
import xyPlot
import displayGroupOdbToolset as dgo
import connectorBehavior
For abaqus the easiest thing to do watch the replay output generated by abaqus cae. Open the model manually in CAE and change the view to what you would like to see. Abaqus writes a python replay file of all the actions the user takes in the CAE window. Navigate to your working folder and find the file named abaqus.rpy. The last lines in that will be the python commands to replicate your actions in CAE.