I would like to implement a status bar that gives the user feedback of the status of the script. I have build a script that will loop through a video directory and for every video that it detects it runs a process against it. While the initial code does work, the issue I am having is that when the code finishes using a trained tensorflow model, it fails to release the gpu memory reserved for the process. My solution to this is to run a separate script that has values passed through to it from the initial script. Any help would be appreciated.
Here is the code I referenced for building a multi-status bar output:
`
import multiprocessing
import random
from concurrent.futures import ProcessPoolExecutor
from time import sleep
#from rich import progress
from rich.progress import Progress
def long_running_fn(progress, task_id):
len_of_task = random.randint(3, 20) # take some random length of time
for n in range(0, len_of_task):
sleep(1) # sleep for a bit to simulate work
progress[task_id] = {"progress": n + 1, "total": len_of_task}
if __name__ == "__main__":
n_workers = 8 # set this to the number of cores you have on your machine
with Progress() as progress:
futures = [] # keep track of the jobs
with multiprocessing.Manager() as manager:
# this is the key - we share some state between our
# main process and our worker functions
_progress = manager.dict()
overall_progress_task = progress.add_task("[green]All jobs progress:")
with ProcessPoolExecutor(max_workers=n_workers) as executor:
for n in range(0, 20): # iterate over the jobs we need to run
# set visible false so we don't have a lot of bars all at once:
task_id = progress.add_task(f"task {n}", visible=False)
futures.append(executor.submit(long_running_fn, _progress, task_id))
# monitor the progress:
while (n_finished := sum([future.done() for future in futures])) < len(
futures
):
progress.update(
overall_progress_task, completed=n_finished, total=len(futures)
)
for task_id, update_data in _progress.items():
latest = update_data["progress"]
total = update_data["total"]
# update the progress bar for this task:
progress.update(
task_id,
completed=latest,
total=total,
visible=latest < total,
)
# raise any errors:
for future in futures:
future.result()
`
Which gives a result that look like this:
progress bars
In implementing this into my code, the progress bars work, but only until the gpu memory fills up than I am unable to keep the code running. If I run a two script variant without a progress bar everything works perfectly.
Here is the code that I currently have. I apologize in advance its a WIP.
`
def detect_frames(progress, task_id, gpu, filename, xl, size):
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu) #"1"
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
for gpu in gpus:
try:
tf.config.experimental.set_virtual_device_configuration(gpu,
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024*size)]) #1024 #starts mem limit at 1Gig, 5715 is for the R720
except RuntimeError as e:
print(e)
model = tensorflow.keras.models.load_model('SQ-D4096-V7.h5', compile=False)
labels = ['Corrupt','Good'] #needs to be in same order as text file
start_time = time.time()
f3 = 0
T = 0 #time
T2 = 0 #time2 for testing
cap = cv.VideoCapture(filename)
fps = cap.get(cv.CAP_PROP_FPS)
totalNoFrames = cap.get(cv.CAP_PROP_FRAME_COUNT)#/30
durationInSeconds = (totalNoFrames / fps)/fps
good = []
corrupt = []
bad = []
count = 0
while cap.isOpened():
ret, frame = cap.read()
count += 120
cap.set(cv.CAP_PROP_POS_FRAMES, count)
fnum = cap.get(cv.CAP_PROP_POS_FRAMES);
progress[task_id] = {"progress": fnum, "total": totalNoFrames}
rest of def ....
if __name__ == "__main__":
CWD = os.path.dirname(os.path.realpath(__file__))
nvidia_smi.nvmlInit()
try:
deviceCount = nvmlDeviceGetCount()
for i in range(deviceCount):
#gpus = len(i)
#print("Num GPU: ",gpus)
handle = nvmlDeviceGetHandleByIndex(i)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
card = nvmlDeviceGetName(handle)
print("Device", i, ":", nvmlDeviceGetName(handle))
print("Total memory: %sGB" %(info.total//(1.024*10**9)))
print("Free memory: %sGB" %(info.free//(1.024*10**9)))
print("Used memory: %sGB" %(info.used//(1.024*10**9)))
Num = (info.free//(1.024*10**9))-1
print("Max number of scripts at once: ",Num)
#print("Recommended Max number of scripts at once, per GPU: ",Num-1)
except NVMLError as error:
print(error)
numgpu = deviceCount
nvidia_smi.nvmlShutdown()
# Input area
#%%
#Where are the videos located?
#file = glob.glob("/Data/2_DeInter/*.avi") #for production
file = glob.glob("/Data/2_DeInter/Untitled_5.avi") #for Testing
#file = sys.argv[1]
#print(file)
#How many videos do you want to process per GPU?
#Num = 12
NumProc = 1
#nvidia_smi.nvmlInit()
if (str(card) == "b'NVIDIA GeForce GTX 1050 Ti'") and (NumProc > 2 ):
print("Unfortunately the 1050 Ti can only process two streams per card:")
print("Changing number of processes to 2!")
NumProc = 2
#if NumProc == 1:
# MemFree = ((Num//NumProc)-1)
#else:
# MemFree = (Num//NumProc)
print("NumProc: ",NumProc)
MemFree = 1/((Num*1.2)/NumProc)
print("Memory free:",MemFree)
size = (Num/NumProc)#MemFree #2 #1.2 #size of the tensorflow model
print('Memory size Used: ',size)
#print(size)
print("\n")
n_workers = NumProc#*numgpu #2
#%%
gpu = 0
x = 0
T = 0
nvidia_smi.nvmlInit()
with Progress() as progress:
futures = [] # keep track of the jobs
# with multiprocessing.Manager() as manager:
# # this is the key - we share some state between our
# # main process and our worker functions
# _progress = manager.dict()
# overall_progress_task = progress.add_task("[green]All jobs progress:")
with multiprocessing.Manager() as manager:
# this is the key - we share some state between our
# main process and our worker functions
_progress = manager.dict()
overall_progress_task = progress.add_task("[green]All jobs progress:")
#for n in range(len(file)):
with ProcessPoolExecutor(max_workers=n_workers) as executor:
#with ProcessPoolExecutor(max_workers=n_workers) as executor:
while True:
#for n in range(len(file)):
#task_id = progress.add_task(f"task {x}", visible=False)
task_id = progress.add_task(f"Processing video: {Path(file[x]).name}", visible=False)
#print("X: %s"%x)
#while True:
#for gpu in range(deviceCount):
#print("GPU: ",gpu)
print("X:",x)
# if x > NumProc: #clear the tf models
# tf.keras.backend.clear_session()
# tf.compat.v1.reset_default_graph()
# del model
# load.model()
#print("Mem Free: ",info.free//(size*10**9)-1)
#with Progress() as progress:
# futures = []
while info.free/(size*10**9) <= MemFree:
#print("Mem Free: ",info.free/(1.024*10**9))
#print("Mem Free: ",info.free/(size*10**9))
#print("No Free Memory!")
#print("Checking GPU: %s for space"%gpu)
#print("Update Progress Bar")
if (n_finished := sum([future.done() for future in futures])) < len(futures):
progress.update(overall_progress_task, completed=n_finished, total=len(futures))
for task_id, update_data in _progress.items():
latest = update_data["progress"]
total = update_data["total"]
# update the progress bar for this task:
progress.update(task_id,completed=latest,total=total,visible=latest < total,)
time.sleep(1)
gpu = gpu+1
if gpu == deviceCount:
gpu = 0
handle = nvmlDeviceGetHandleByIndex(gpu)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
if info.free/(size*10**9) > MemFree:
tf.config.experimental.set_virtual_device_configuration(gpu,
futures.append(executor.submit(detect_frames, _progress, task_id, gpu, file[x], x, size))
if (n_finished := sum([future.done() for future in futures])) < len(futures):
progress.update(overall_progress_task, completed=n_finished, total=len(futures))
for task_id, update_data in _progress.items():
latest = update_data["progress"]
total = update_data["total"]
# update the progress bar for this task:
progress.update(task_id,completed=latest,total=total,visible=latest < total,)
time.sleep(5)
#print("Update Progress Bar")
if (n_finished := sum([future.done() for future in futures])) < len(futures):
progress.update(overall_progress_task, completed=n_finished, total=len(futures))
for task_id, update_data in _progress.items():
latest = update_data["progress"]
total = update_data["total"]
# update the progress bar for this task:
progress.update(task_id,completed=latest,total=total,visible=latest < total,)
x = x+1
gpu = gpu+1
if gpu == deviceCount:
gpu = 0
#break
if x == len(file):
print("End of file list")
#print("Update Progress Bar")
while (n_finished := sum([future.done() for future in futures])) < len(futures):
progress.update(overall_progress_task, completed=n_finished, total=len(futures))
for task_id, update_data in _progress.items():
latest = update_data["progress"]
total = update_data["total"]
# update the progress bar for this task:
progress.update(task_id,completed=latest,total=total,visible=latest < total,)
break
handle = nvmlDeviceGetHandleByIndex(gpu)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
nvidia_smi.nvmlShutdown()
`
Related
import os
import gym
import neat
import neat.config
import configparser
import multiprocessing
import graphviz
import numpy as np
def eval_genome(genome, config):
# Creates 'FeedForward' Network with evolved genome and the requested configuration
network = neat.nn.FeedForwardNetwork.create(genome, config)
fits = []
runs_per_network = 10
for _ in range(runs_per_network):
observations = env.reset()
fitness = 0
for _ in range(500):
action = network.activate(observations) # The obsv is used as the input of the Neural Network
action = int(np.argmax(action)) # for the action, we take the one with the highest probability
observations, reward, done, _ = env.step(action) # We then take the action and get a reward
#reward
fitness += reward
if done:
fits.append(fitness)
break
fits.append(fitness)
# The genome's fitness is its worst performance across all runs per network
return min(fits)
# Evaluation of 100 episodes
def eval_winner_network(winner, config):
# Creates 'FeedForward' Network with evolved genome and the requested configuration
winner_network = neat.nn.FeedForwardNetwork.create(winner, config)
fits = []
for _ in range(100): #number of episodes
observations = env.reset()
fitness = 0
for _ in range(500):
action = winner_network.activate(observations)
action = int(np.argmax(action))
observations, reward, done, _ = env.step(action)
fitness += reward
if done:
fits.append(fitness)
break
fits.append(fitness)
print("Average fitness throughout 100 episodes is observed at: {}".format(np.mean(fits)))
if np.mean(fits) >= 500: #task solved if total reward equates to 500 over 100 consecutive trials
print(" + Task solved + ")
else:
print(" - Task unsolved - ")
# Final product for animation
def viz_winner_network(winner, config):
winner_network = neat.nn.FeedForwardNetwork.create(winner, config)
for _ in range(3):
observations = env.reset()
for _ in range(500): #episode length
env.render()
action = winner_network.activate(observations)
action = int(np.argmax(action))
observations, _, done, _ = env.step(action)
if done:
break
#load CartPole env
env = gym.make('CartPole-v1')
# Loads NEAT configuration file
local_dir = os.path.dirname('C:\\Users\\User')
config_path = os.path.join(local_dir, 'cartpole.config')
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
population = neat.Population(config)
# Adds statistics report
population.add_reporter(neat.StdOutReporter(True)) #Add a stdout reporter to show progress in terminal
stats = neat.StatisticsReporter()
population.add_reporter(stats)
# Run parallel execution over available processors
pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome)
it is at this point here, that running this next line here is when the output gets stuck on. I am unsure if this jupyter related or simply for the fact I have made a mistake somewhere along the way. I have been trying to debug this for 2 days with no avail.
# Runs NEAT and returns the best network
winner = population.run(pe.evaluate)
so, running this code and i am now left with ...
****** Running generation 0 ******
the file continues to run and has been for roughly 1 hour on quite a competent machine.
To start, this is a continuation of this question: Multithreading degrades GPU performance. However, since that question never got resolved due to everyone not being able to reproduce the results, I have created a new question with code here that reproduces the slower results outlined there.
To recap: when using cv2.VideoCapture with multi-threading, the inferencing time for Detectron2 is much slower compared to when multi-threading is disabled.
Some additional information is that I am operating on Windows and am using an RTX3070 so inferencing times may be slightly different for those trying to rerun this.
Here is the code:
import time
import cv2
from queue import Queue
from threading import Thread
from detectron2.config import get_cfg
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
class FileVideoStream:
def __init__(self, path, queueSize=15):
self.stream = cv2.VideoCapture(path)
self.stopped = False
self.Q = Queue(maxsize=queueSize)
def start(self):
t = Thread(target=self.update, args=())
t.daemon = True
t.start()
return self
def update(self):
while True:
if self.stopped:
self.stream.release()
return
if not self.Q.full():
(grabbed, frame) = self.stream.read()
if not grabbed:
self.stop()
return
self.Q.put(frame)
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file(
"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")
)
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # set threshold for this model
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"
)
cfg.MODEL.DEVICE = "cuda"
predictor = DefaultPredictor(cfg)
def threading_example():
print("Threading Example:")
fvs = FileVideoStream(r"DemoVideo.mp4")
fvs.start()
# allow time for thread to fill the queue
time.sleep(1)
for i in range(5):
img = fvs.Q.get()
start = time.time()
p = predictor(img)
end = time.time()
print(f"Frame {i} Prediction: {(end - start):.2f}s")
fvs.stopped = True
def non_threading_example():
print("Non-Threading Example:")
video = cv2.VideoCapture(r"DemoVideo.mp4")
for i in range(5):
_, img = video.read()
start = time.time()
p = predictor(img)
end = time.time()
print(f"Frame {i} Prediction: {(end - start):.2f}s")
non_threading_example()
threading_example()
This produces the following output:
Non-Threading Example:
Frame 0 Prediction: 1.41s
Frame 1 Prediction: 0.14s
Frame 2 Prediction: 0.14s
Frame 3 Prediction: 0.14s
Frame 4 Prediction: 0.14s
Threading Example:
Frame 0 Prediction: 10.55s
Frame 1 Prediction: 10.41s
Frame 2 Prediction: 10.77s
Frame 3 Prediction: 10.64s
Frame 4 Prediction: 10.27s
EDIT: I've added code to answer a comment about testing if the GPU on inferencing when inside a thread, which does not appear to be the case.
def infer_5(img):
for i in range(5):
start = time.time()
p = predictor(img)
end = time.time()
print(f"Frame {i}: {(end - start):.2f}s")
def system_load():
img = cv2.imread(
r"Image.jpg")
t = Thread(target=infer_5, args=(img,))
t.start()
Frame 0: 7.51s
Frame 1: 0.39s
Frame 2: 0.15s
Frame 3: 0.15s
Frame 4: 0.15s
I have implemented a python app that performs real time car_model and license plate recognition from a camera using yolo3 / opencv- gpu. Frames are captured in Threading having a descent fps.
I am trying to update it by getting streaming from 4 cameras showing the frames to a pysimplegui window. However I can't figure out how to make stream from the cameras run in parallel. It seems that each stream is waiting from the previous one to finish in order to stream a frame, so the overall fps, drops to 1/4.
if __name__ == '__main__':
Results.num_cam = 2
# initialize the video streams and allow them to warmup
img_size_x, img_size_y = 1280, 800
window = create_window(img_size_x, img_size_y,Results.num_cam)
print("[INFO] starting cameras...")
prev_frame_time = 0
prev_frame_time = 0
if Results.num_cam == 2:
webcam1 = VideoStream(src='rtsp://admin:VVZOZR#192.168.1.11:554/11').start()
webcam2 = VideoStream(src='rtsp://admin:VVZOZR#192.168.1.11:554/11').start()
else:
webcam1 = VideoStream(src='rtsp://admin:VVZOZR#192.168.1.11:554/11').start()
webcam2 = VideoStream(src='rtsp://admin:VVZOZR#192.168.1.11:554/11').start()
webcam3 = VideoStream(src='rtsp://admin:VVZOZR#192.168.1.11:554/11').start()
webcam4 = VideoStream(src='rtsp://admin:VVZOZR#192.168.1.11:554/11').start()
time.sleep(2.0)
Results.car_m, Results.car_l, _ = yolo_cars()
Results.pl_m, Results.pl_l, _ = yolo_plate()
# initialize the two motion detectors, along with the total
# number of frames read
results = Results()
total = 0
while True:
# initialize image elements objects
if Results.num_cam == 2:
cam_view = window['-FRAME1-']
cam1_view = window['-FRAME2-']
else:
cam_view = window['-FRAME1-']
cam1_view = window['-FRAME2-']
cam2_view = window['-FRAME3-']
cam3_view = window['-FRAME4-']
# initialize the list of frames that have been processed
frames = []
event, ts = window.read(timeout=0.05)
t1 = time.perf_counter()
if event == sg.WINDOW_CLOSED:
break
# loop over the frames and their respective motion detectors
if Results.num_cam == 2:
res_all = (results, results)
web_all = (webcam1, webcam2)
else:
res_all = (results, results, results, results)
web_all = (webcam1, webcam2, webcam3, webcam4)
for (stream, res) in zip(web_all, res_all):
# read the next frame from the video stream and resize
frame = stream.read()
res_inst = res.process_frame(frame,True)
if total < 32:
frames.append(res_inst.frame)
continue
# update the frames list
frames.append(res_inst.frame)
# increment the total number of frames read and grab the
# current timestamp
total += 1
if Results.num_cam == 2:
for (frame, name) in zip(frames, (cam_view, cam1_view)): # , 'Webcam3', 'Webcam4')):
name.update(frame)
else:
for (frame, name) in zip(frames, (cam_view, cam1_view, cam2_view, cam3_view)):
name.update(frame)
new_frame_time = time.time()
fps = 1 / (new_frame_time - prev_frame_time)
prev_frame_time = new_frame_time
fps = int(fps)
window['-FR-'].update(str(fps))
# do a bit of cleanup
print("[INFO] cleaning up...")
for web in web_all:
web.stop()`.
I've been trying to record audio using pyaudio untill silence is met in the input stream .but segmentation fault happens while running it .i don't think anything is wrong with pyaudio/portaudio installed in my raspberry pi because pyaudio works when i tried to run examples in pyaudio docs it works without any issue .i tried to debug it with pdb and
gdb these are the results :
Recording: Setting up
Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x7652a298 in ?? ()
from /usr/lib/python2.7/dist-packages/_portaudio.arm-linux- gnueabihf.so
(gdb) backtrace
#0 0x7652a298 in ?? ()
from /usr/lib/python2.7/dist-packages/_portaudio.arm-linux- gnueabihf.so
#1 0x764f47b0 in Pa_GetDeviceInfo ()
from /usr/lib/arm-linux-gnueabihf/libportaudio.so.2
#2 0x7effe2c4 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb)
pyaudio callback function
def _callback(self, in_data, frame_count, time_info, status): # pylint: disable=unused-argument
debug = logging.getLogger('alexapi').getEffectiveLevel() == logging.DEBUG
if not in_data:
self._queue.put(False)
return None, pyaudio.paAbort
do_VAD = True
if self._callback_data['force_record'] and not self._callback_data['force_record'][1]:
do_VAD = False
# do not count first 10 frames when doing VAD
if do_VAD and (self._callback_data['frames'] < self._callback_data['throwaway_frames']):
self._callback_data['frames'] += 1
# now do VAD
elif (self._callback_data['force_record'] and self._callback_data['force_record'][0]()) \
or (do_VAD and (self._callback_data['thresholdSilenceMet'] is False)
and ((time.time() - self._callback_data['start']) < self.MAX_RECORDING_LENGTH)):
if do_VAD:
if int(len(in_data) / 2) == self.VAD_PERIOD:
isSpeech = self._vad.is_speech(in_data, self.VAD_SAMPLERATE)
if not isSpeech:
self._callback_data['silenceRun'] += 1
else:
self._callback_data['silenceRun'] = 0
self._callback_data['numSilenceRuns'] += 1
# only count silence runs after the first one
# (allow user to speak for total of max recording length if they haven't said anything yet)
if (self._callback_data['numSilenceRuns'] != 0) \
and ((self._callback_data['silenceRun'] * self.VAD_FRAME_MS) > self.VAD_SILENCE_TIMEOUT):
self._callback_data['thresholdSilenceMet'] = True
else:
self._queue.put(False)
return None, pyaudio.paComplete
self._queue.put(in_data)
if debug:
self._callback_data['audio'] += in_data
return None, pyaudio.paContinue
pyaudio
def _callback(self, in_data, frame_count, time_info, status): # pylint: disable=unused-argument
debug = logging.getLogger('alexapi').getEffectiveLevel() == logging.DEBUG
if not in_data:
self._queue.put(False)
return None, pyaudio.paAbort
do_VAD = True
if self._callback_data['force_record'] and not self._callback_data['force_record'][1]:
do_VAD = False
# do not count first 10 frames when doing VAD
if do_VAD and (self._callback_data['frames'] < self._callback_data['throwaway_frames']):
self._callback_data['frames'] += 1
# now do VAD
elif (self._callback_data['force_record'] and self._callback_data['force_record'][0]()) \
or (do_VAD and (self._callback_data['thresholdSilenceMet'] is False)
and ((time.time() - self._callback_data['start']) < self.MAX_RECORDING_LENGTH)):
if do_VAD:
if int(len(in_data) / 2) == self.VAD_PERIOD:
isSpeech = self._vad.is_speech(in_data, self.VAD_SAMPLERATE)
if not isSpeech:
self._callback_data['silenceRun'] += 1
else:
self._callback_data['silenceRun'] = 0
self._callback_data['numSilenceRuns'] += 1
# only count silence runs after the first one
# (allow user to speak for total of max recording length if they haven't said anything yet)
if (self._callback_data['numSilenceRuns'] != 0) \
and ((self._callback_data['silenceRun'] * self.VAD_FRAME_MS) > self.VAD_SILENCE_TIMEOUT):
self._callback_data['thresholdSilenceMet'] = True
else:
self._queue.put(False)
return None, pyaudio.paComplete
self._queue.put(in_data)
if debug:
self._callback_data['audio'] += in_data
return None, pyaudio.paContinue
These are actually adaptation of the code that i found somewhere on the internet.i double checked my device index and sample rate there is nothing wrong with them
can someone help me sort it out ?
complete code is here
pdb result
> /usr/lib/python2.7/dist-packages/pyaudio.py(438)__init__()
-> arguments['stream_callback'] = stream_callback
(Pdb) step
> /usr/lib/python2.7/dist-packages/pyaudio.py(441)__init__()
-> self._stream = pa.open(**arguments)
(Pdb) step
Segmentation fault
root#raspberrypi:/home/pi/Desktop# python -m pdb rp3test.py
Idk may it's just a bug in pyaudio and everylibs that uses pyaudio such as python sounddevice . cause i tried it with sounddevice library . Finally made it work with this code
def silence_listener(throwaway_frames,filename = "recording.wav"):
# Reenable reading microphone raw data
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, alsa_card)
inp.setchannels(1)
inp.setrate(VAD_SAMPLERATE)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(VAD_PERIOD)
audio = ""
# Buffer as long as we haven't heard enough silence or the total size is within max size
thresholdSilenceMet = False
frames = 0
numSilenceRuns = 0
silenceRun = 0
start = time.time()
# do not count first 10 frames when doing VAD
while (frames < throwaway_frames): # VAD_THROWAWAY_FRAMES):
l, data = inp.read()
frames = frames + 1
if l:
audio += data
isSpeech = vad.is_speech(data, VAD_SAMPLERATE)
# now do VAD
while (thresholdSilenceMet == False) and ((time.time() - start) < MAX_RECORDING_LENGTH):
l, data = inp.read()
if l:
audio += data
if (l == VAD_PERIOD):
isSpeech = vad.is_speech(data, VAD_SAMPLERATE)
if (isSpeech == False):
silenceRun = silenceRun + 1
#print "0"
else:
silenceRun = 0
numSilenceRuns = numSilenceRuns + 1
#print "1"
# only count silence runs after the first one
# (allow user to speak for total of max recording length if they haven't said anything yet)
if (numSilenceRuns != 0) and ((silenceRun * VAD_FRAME_MS) > VAD_SILENCE_TIMEOUT):
thresholdSilenceMet = True
if debug: print ("End recording")
rf = open(filename, 'w')
rf.write(audio)
rf.close()
inp.close()
return
I have a problem... I've already tried some ways but it didn;t work. I have to do a real time data aquisition and plotting them in an interface... If you can suggest me a way to do that... The program below makes one data aquisition in variable "data"(matrix), but I have to do it continuously and plotting them the same time... Thank you!
# Print library info:
print_library_info()
# Search for devices:
libtiepie.device_list.update()
# Try to open an oscilloscope with block measurement support:
scp = None
for item in libtiepie.device_list:
if item.can_open(libtiepie.DEVICETYPE_OSCILLOSCOPE):
scp = item.open_oscilloscope()
if scp.measure_modes & libtiepie.MM_BLOCK:
break
else:
scp = None
if scp:
try:
fig = plt.figure()
ax = fig.add_subplot(111)
k=0
while k<20:
# Set measure mode:
scp.measure_mode = libtiepie.MM_BLOCK
# Set sample frequency:
scp.sample_frequency = 5e6 # 1 MHz
# Set record length:
scp.record_length = 1000 # 15000 samples
# Set pre sample ratio:
scp.pre_sample_ratio = 0 # 0 %
# For all channels:
for ch in scp.channels:
# Enable channel to measure it:
ch.enabled = True
# Set range:
ch.range = 8 # 8 V
# Set coupling:
ch.coupling = libtiepie.CK_ACV # DC Volt
# Set trigger timeout:
scp.trigger_time_out = 100e-3 # 100 ms
# Disable all channel trigger sources:
for ch in scp.channels:
ch.trigger.enabled = False
# Setup channel trigger:
ch = scp.channels[0] # Ch 1
# Enable trigger source:
ch.trigger.enabled = True
# Kind:
ch.trigger.kind = libtiepie.TK_RISINGEDGE # Rising edge
# Level:
ch.trigger.levels[0] = 0.5 # 50 %
# Hysteresis:
ch.trigger.hystereses[0] = 0.05 # 5 %
# Print oscilloscope info:
#print_device_info(scp)
# Start measurement:
scp.start()
# Wait for measurement to complete:
while not scp.is_data_ready:
time.sleep(0.01) # 10 ms delay, to save CPU time
# Get data:
data = scp.get_data()
except Exception as e:
print('Exception: ' + e.message)
sys.exit(1)
# Close oscilloscope:
del scp
else:
print('No oscilloscope available with block measurement support!')
sys.exit(1)
What about something like this (I assumed you were plotting your data against time):
import joystick as jk
import time
class test(jk.Joystick):
_infinite_loop = jk.deco_infinite_loop()
_callit = jk.deco_callit()
#_callit('before', 'init')
def _init_data(self, *args, **kwargs):
self.t = np.array([])
self.data = np.array([])
# do the hardware initialization here
#.............
self.range = 8
self.record_length = 1000
#_callit('after', 'init')
def _build_frames(self, *args, **kwargs):
self.mygraph = self.add_frame(
Graph(name="Graph", size=(500, 500),
pos=(50, 50), fmt="go-", xnpts=self.record_length,
freq_up=10, bgcol="w", xylim=(0,10,0,self.range)))
#_callit('before', 'start')
def _set_t0(self):
# initialize t0 at start-up
self._t0 = time.time()
#_infinite_loop(wait_time=0.2)
def _get_data(self):
self.t = self.mygraph.add_datapoint(self.t, time.time())
# new data acquisition here
new_data = scp.get_data()
self.data = self.graph.add_datapoint(self.data, new_data)
self.mygraph.set_xydata(self.t-self._t0, self.data)
and to start the reading/plotting:
t = test()
t.start()