Capture dB levels from a WAV file - python

Python 2.7.3 (default, Sep 26 2013, 16:35:25) [GCC 4.7.2] on linux2
Linux Mint 14
Using Python, I am reading a wav and putting the data to a text file. The entire script is below. It is my first Python Script.
The values for the micsec can be anything from 000000 to 999999. There are frequently leading zero's which I am stripping with lstrip('0').
If the value is 000000, I end up with null. I'm using an extra step to change null to 0.
What is the best way to do this in a single step ?
Thanks for any input.
!/usr/bin/python
import contextlib, datetime, math, os, time, wave, glob
from scipy.io.wavfile import read
from numpy import log10, sqrt, mean
import numpy as np
path = "/the/path/*.wav"
for fname in glob.glob(path):
print "process_wav is processing - " + fname
outfname = fname + ".txt"
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(fname))
tm=0.000000
with contextlib.closing(wave.open(fname,'r')) as f:
channels = f.getnchannels()
sampwidth = f.getsampwidth()
comptype = f.getcomptype()
frames = f.getnframes()
rate = f.getframerate()
wav_duration = frames / float(rate)
frame_duration = round((wav_duration / frames),6)
samprate, wavdata = read(fname)
chunks = np.array_split(wavdata, frames)
hdr = (
"# audio file processed - " + fname +
"\n# this file name - " + outfname +
"\n# audio file mod time-" + str(mtime) +
"\n# channels - " + str(channels) +
"\n# sampwidth - " + str(sampwidth) +
"\n# comptype - " + str(comptype) +
"\n# frames - " + str(frames) +
"\n# rate - " + str(rate) +
"\n# wav_duration - " + str(wav_duration) +
"\n# frame_dutation - " + "{0:0.6f}".format(frame_duration) +
"\n# chunk|wave_file_name|audio_file_timestamp|chunk_second|chunk_microsecond|chunk_time_in_audio|frame_duration|dB\r\n"
)
out = open(outfname,'w')
out.write(hdr)
for i,chunk in enumerate(chunks):
try:
sec = int("{0:0.6f}".format(round(float(tm),6)).split('.')[0])
micsec = "{0:0.6f}".format(round(float(tm),6)).split('.')[1].lstrip('0')
#handle 000000 - there has to be a better way
micsec = int(float(micsec)) if len(micsec)>=1 else 0
cm = mtime + datetime.timedelta(seconds=sec,microseconds=micsec)
out.write("{}|{}|{}|{}|{}|{}|{}|{}\n".format(i, fname, mtime, sec, micsec, cm, frame_duration, 20*log10( sqrt(mean(chunk**2)))))
tm += frame_duration
except ValueError as ex:
print("value error" + str(ex))
out.write(str(i) + str(ex))
except Exception,e:
print("ERROR" + str(e))
out.write(str(i) + str(e))
out.close()

Related

How To Use Yolo txt Files For Detection?

import cv2
import json
def save_yolo_format(image_file, json_file, txt_file):
with open(json_file) as f:
chars = json.loads(f.read())
bgr = cv2.imread(image_file)
result = ""
for char in chars:
x_center = (char["x"] + char["width"] // 2) / bgr.shape[1]
y_center = (char["y"] + char["height"] // 2) / bgr.shape[0]
width = char["width"] / bgr.shape[1]
height = char["height"] / bgr.shape[0]
label = char["char_id"]
result += str(label) + " " + str(x_center) + " " + str(y_center) + " " + str(width) + " " + str(height) + "\n"
with open(txt_file, "w+") as f:
f.write(result)
save_yolo_format("dataset_root/images/1.jpg",
"dataset_root/labels/1.json",
"dataset_root/labels/1.txt")
**I Convert Json Files To Yolo Txt File **
After Run Code My Result Is...
One Of My Text File Value:
7 0.05197505197505198 0.5234375 0.07276507276507277 0.26953125
5 0.13097713097713098 0.51953125 0.08523908523908524 0.25390625
26 0.23700623700623702 0.51953125-0.12058212058212059 0.32421875
2 0.3471933471933472 0.5234375 0.08316008316008316 0.28125
How can I use the files above for detection?

How to fix: KeyError: '__file__' in sitecustomize.py (anaconda3) when using run file (F5)

The script below works fine when running it with (F9). When running it with (F5) it still works (all operations are done fine), but produces the following error:
File "", line 1, in runfile('S:/Fakultaet/MFZ/NWFZ/AGdeHoz/Philipp/Python/Analysis_script.py',wdir='S:/Fakultaet/MFZ/NWFZ/AGdeHoz/Philipp/Python')
File "c:\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 709, in runfile namespace.pop('file')
KeyError: 'file'
I would like to know what is happening and how to fix it.
I read something about a nesting problem, so I took out the part where I import my own function and also the part where I use it. The error still persists. So it has at least nothing to do with my own function.
from IPython import get_ipython
get_ipython().magic('%reset -f')
import time
tic = time.time()
import sys
import os
import pickle
import CSC_getdata_timestamps
General_folder = 'Data' #where processed data will be saved
filename_extension = '_Sleep_auditory'; #file extension name
Database_name = 'mydata.obj' #name of database
ExpDates = 'ExpDates.obj' #name of object containing all experimental
dates
root = r"S:\Fakultaet\MFZ\NWFZ\AGdeHoz\Philipp" #root directory (data
location)
AnalyType = "LFP" #"SP" Local field potential or Spike analysis
spr = 30000 #sample rate
Cname = "CH"
file_md = open(r"S:\Fakultaet\MFZ\NWFZ\AGdeHoz\Philipp\Data\Database\\" +
Database_name, "rb")
md = pickle.load(file_md)
file_ExpDates =
open(r"S:\Fakultaet\MFZ\NWFZ\AGdeHoz\Philipp\Data\Database\\" +
ExpDates, "rb")
ExpDates = pickle.load(file_ExpDates)
for mi, expdates in enumerate(ExpDates):
day = expdates
mouse = md[expdates]['mouse']
files = md[expdates]['rectime']
filesm = md[expdates]['FRAm'] + md[expdates]['REMm'] + md[expdates]
['SWSm'] + md[expdates]['awakem']
rect = md[expdates]['rect']
filest = [md[expdates]['FRAt'], md[expdates]['REMt'], md[expdates]
['SWSt'], md[expdates]['awaket']]
channels = md[expdates]['channels']
for f, files_m in enumerate(filesm):
try:
if len(files) == 1:
recname = files
else:
tfiles = []
tfilesm = []
for i, file in enumerate(files):
tfiles.append(int(files[i][0:2]) * 60 + int(files[i]
[3:5]))
tfilesm = int(files_m[f][0:2]) * 60 + int(files_m[f]
[3:5])
recidx = [i for i in range(len(tfiles)) if tfiles[i] <
tfilesm]
recname = files[recidx[-1]]
TS0 = (int(rect[0:2]) * 3600 + int(rect[3:5]) * 60 +
int(rect[6:8])) * spr
TS1 = TS0 + (int(filest[f][0][0:2]) * 3600 + int(filest[f][0]
[3:5]) * 60 + int(filest[f][0][6:8])) * spr
TS2 = TS0 + TS1 + (int(filest[f][1][0:2]) * 3600 +
int(filest[f][1][3:5]) * 60 + int(filest[f][1][6:8])) * spr
TS = [TS1, TS2]
savename = files_m[0:2] + "-" + files_m[3:5]
if AnalyType == "LFP":
if not os.path.exists(root + "\Data\Raw" +
filename_extension + "\LFP\\" + day):
os.makedirs(root + "\Data\Raw" + filename_extension +
"\LFP\\" + day)
else:
print("Directory already exists.")
elif AnalyType == "SP":
if not os.path.exists(root + "\Data\Raw" +
filename_extension + "\SP\\" + day):
os.makedirs(root + "\Data\Raw" + filename_extension +
"\SP\\" + day)
else:
print("Directory already exists.")
else:
sys.exit("Invalid type of analysis. Check variable
AnalyType.")
varlist, trseq, trlength, stlength, ntrials, datacscini,
samplefreq, tscsc, tstrig =
CSC_getdata_timestamps.CSC_getdata_timestamps(root, day,
savename, recname, channels, AnalyType, TS)
print(trseq)
except Exception as e:
print('Error: ' + str(e))
print('mi = {}, f = {}'.format(mi, f))
toc = round(time.time() - tic, 2)
print("elapsed time:", toc, "seconds")
The whole script is to combine data from a (1) written database, with a (2) dataframe which includes specifications of a sound played during a (3) recording.
forgive me if it is written very unpythonic, I just recently switched from Matlab.
The script is for a application in Neuroscience.
(Spyder maintainer here) This error is fixed in our latest version (3.3.4). Please update by opening the Anaconda prompt and running there
conda install spyder=3.3.4

Python 3.7: How to get the Windows user Login Time?

I am trying to get the System User's Login Time using Python 3.7. I have tried win32net and platform module for Python but, functions are not defined in platform module and Win32net is not compatible with Python 3 and more. I have tried following code:
import platform
platform.uname()
import platform
os_name = platform.uname()[0].lower()
if os_name == "windows":
get_win_login_time()
elif os_name.endswith("nix"):
get_nix_login_time()
Try These ( install win32com.client and subprocess modules first ):
import win32com.client, time
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_NetworkLoginProfile")
def Convert_to_human_time(dtmDate):
strDateTime = ""
if dtmDate[4] == 0:
strDateTime = dtmDate[5] + '/'
else:
strDateTime = dtmDate[4] + dtmDate[5] + '/'
if dtmDate[6] == 0:
strDateTime = strDateTime + dtmDate[7] + '/'
else:
strDateTime = strDateTime + dtmDate[6] + dtmDate[7] + '/'
strDateTime = strDateTime + dtmDate[0] + dtmDate[1] + dtmDate[2] + dtmDate[3] + " " + dtmDate[8] + dtmDate[9] + ":" + dtmDate[10] + dtmDate[11] +':' + dtmDate[12] + dtmDate[13]
return strDateTime
for objItem in colItems:
if objItem.Name is not None:
print("Name: " + str(objItem.Name))
if objItem.LastLogon is not None:
print("Last Logon (Normal Format): " + str(objItem.LastLogon))
print("Last Logon (Human Readable Format): " + Convert_to_human_time(objItem.LastLogon))
if objItem.LastLogoff is not None:
print("Last Logoff (Normal Format): " + str(objItem.LastLogoff))
print("Last Logoff (Human Readable Format): " + Convert_to_human_time(objItem.LastLogoff))
if objItem.LogonHours is not None:
print("Logon Hours: " + str(objItem.LogonHours))
if objItem.LogonServer is not None:
print("Logon Server: " + str(objItem.LogonServer))
if objItem.NumberOfLogons is not None:
print("Number Of Logons: " + str(objItem.NumberOfLogons))
Another way :
from subprocess import check_output
import sys
get_result = check_output("wmic netlogin get name, fullname, lastlogon", shell=True, stderr=False)
print(get_result)
clean_result = str(get_result).lstrip("b'").rstrip("'").replace("\\r\\r\\n", "\n").replace('\n\n', '\n').split('\n')[2:-1]
for items in clean_result:
print(items.lstrip().rstrip())
Good Luck ...

Python Timer2 call function every n milliseconds on Pi

I'm using timer2 in Python to save data from an IMU sensor into a file every 10 miliseconds (100Hz). I tell to timer2 to run the function every 10 ms with the method "apply_interval".
However, the timestamp show that the function is called irregularely and too many times (1 per ms)
I need to save the data at the same precise interval in order to perform machine learning.
I will be happy if you could fix it with me.
Here is the code :
from numpy import linalg as LA
from conf import *
import RTIMU
from ledFunctions import *
import timer2
class IMU():
def retrieve_data(self):
t = time.time()
timestamp = t - self.last_time
self.last_time = t
accel = self.data["accel"]
gyro = self.data["gyro"]
mag = self.data["compass"]
f = open(self.nameFile, "a")
f.write(str(accel[0]) + ";" + str(accel[1]) + ";" + str(accel[2]) + ";" +
str(gyro[0]) + ";" + str(gyro[1]) + ";" + str(gyro[2]) + ";" +
str(mag[0]) + ";" + str(mag[1]) + ";" + str(mag[2]) + ";" + str(timestamp) + "\n")
f.flush()
f.close()
def __init__(self):
self.last_time = time.time()
print("Using settings file " + SETTINGS_FILE + ".ini")
if not os.path.exists(SETTINGS_FILE + ".ini"):
print("Settings file does not exist, will be created")
self.s = RTIMU.Settings(SETTINGS_FILE)
# Create IMU object
self.imu = RTIMU.RTIMU(self.s)
print("IMU Name: " + self.imu.IMUName())
# Init IMU
if (not self.imu.IMUInit()):
print("IMU Init Failed")
sys.exit(1)
else:
print("IMU Init Succeeded")
# This is a good time to set any fusion parameters
self.imu.setSlerpPower(0.02)
self.imu.setGyroEnable(True)
self.imu.setAccelEnable(True)
self.imu.setCompassEnable(True)
# Check if save directory exists
self.imuDir = ifDirExists(imuDirectoryName)
nameFileNum = self.imuDir + "/nb"
# Check if nb file exists
if not (os.path.isfile(nameFileNum)):
fnb = open(nameFileNum, "w")
fnb.write(str(0))
fnb.flush()
fnb.close()
nb = 0
print("nb file does not exist, create it")
else:
fnb = open(nameFileNum, "r")
nb = int(fnb.readline().strip())
fnb.close()
print("nb file exists")
# Create new file with name = nb + 1
nb = nb + 1
self.nameFile = self.imuDir + "/" + str(nb)
try:
f = open(self.nameFile, "a")
# Increment the value in nb file
fnb = open(nameFileNum, "w")
fnb.write(str(nb))
fnb.flush()
fnb.close()
self.lastDisplay = time.time()
self.poll = self.imu.IMUGetPollInterval()
self.deltat = 0
self.now = self.lastDisplay = time.time()
except IOError as e:
print "I/O Error({0}): {1}".format(e.errno, e.strerror)
# Retrieve data when available
if self.imu.IMURead():
self.data = self.imu.getIMUData()
timer2.apply_interval(100, self.retrieve_data)
def loop(self):
# Retrieve data when available
if self.imu.IMURead():
self.data = self.imu.getIMUData()
Thx :)

Script Loop through files in directory

I have the following code which creates the txt file I require from a shp.file with the data I need. I have a folder called profiles containing a few number of shape files named (profil1.shp, profil2.shp, profil3.shp etc.). I was wondering how to create a loop so that the script creates for each file a txt file with the same name (eg. for profil1.shp create profil1.txt, profil2.shp create profil2.txt and so on).
import ogr, os, sys, osr
os.chdir('..\profiles')
file = open('profil1.txt', 'w')
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.Open('profil1.shp', 0)
if datasource is None:
print 'Could not open file'
sys.exit(1)
layer = datasource.GetLayer()
feature = layer.GetNextFeature()
while feature:
id = feature.GetFieldAsString('ID')
Distanta = feature.GetFieldAsString('DIST')
Z = feature.GetFieldAsString('Z')
geom = feature.GetGeometryRef()
x = str(geom.GetX())
y = str(geom.GetY())
file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " " + y + " " + " " + "[Z]" + Z + " " + "\n")
feature.Destroy()
feature = layer.GetNextFeature()
datasource.Destroy()
file.close()
edit: the code is returning a Could not open file.Photo of the folder containing the files and their respective names. Safe to assume I am doing something wrong.
import ogr, os, sys, osr,os.path
os.chdir = ('C:\Users\Andrei\Desktop\profil3')
l = os.listdir('C:\Users\Andrei\Desktop\profil3')
for i in l:
if i.endswith('.shp'):
s1 = s.split('.')[0] + '.txt'
file = open(s1, 'w')
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.Open(i, 0)
if datasource is None:
print 'Could not open file'
sys.exit(1)
layer = datasource.GetLayer()
feature = layer.GetNextFeature()
while feature:
id = feature.GetFieldAsString('ID')
Distanta = feature.GetFieldAsString('DIST')
Z = feature.GetFieldAsString('Z')
geom = feature.GetGeometryRef()
x = str(geom.GetX())
y = str(geom.GetY())
file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " " + y + " " + " " + "[Z]" + Z + " " + "\n")
feature.Destroy()
feature = layer.GetNextFeature()
datasource.Destroy()
file.close()
You can use os.listdir() to list the files and folders in the current directory.
This returns a list of all files in the current directory (or the directory given to it as parameter , if no parameter is specified it checks the current directory) .
Then you can check for files with the name ending with .shp using string.endswith() function and then use that to create your new files.
Example of a small portion -
import os , os.path
l = os.listdir()
for i in l:
if i.endswith('.shp'):
s1 = s.split('.')[0] + '.txt'
At the end s1 would contain the file with extension as .txt .
Then you can do your logic on this file, and keep on doing like this.
Full code would look something like -
import ogr, os, sys, osr,os.path
os.chdir('..\profiles')
l = os.listdir()
for i in l:
if i.endswith('.shp'):
s1 = s.split('.')[0] + '.txt'
file = open(s1, 'w')
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.Open(i, 0)
if datasource is None:
print 'Could not open file'
sys.exit(1)
layer = datasource.GetLayer()
feature = layer.GetNextFeature()
while feature:
id = feature.GetFieldAsString('ID')
Distanta = feature.GetFieldAsString('DIST')
Z = feature.GetFieldAsString('Z')
geom = feature.GetGeometryRef()
x = str(geom.GetX())
y = str(geom.GetY())
file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " " + y + " " + " " + "[Z]" + Z + " " + "\n")
feature.Destroy()
feature = layer.GetNextFeature()
datasource.Destroy()
file.close()
A better way of openning files, etc is using with statement. Look up its tutorial here.

Categories

Resources