VTK textactor not showing - python

I'm using VTK to show textactor with info 'ID' added to the actors. When my mouse is over an actor, the MouseEvent will call an 'add_text' function. The function was called and printed the correct 'ID' but there's no textactor adding to the window, is there any problem with my program?
class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("MouseMoveEvent", self.MouseMove)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
self.new = vtkTextActor()
self.old = vtkTextActor()
self.on_actor = False
def add_text(self,renderer,ID):
self.new.SetInput(ID)
txtprop = self.new.GetTextProperty()
txtprop.SetFontSize(36)
self.new.SetDisplayPosition(0,0)
# renderer.RemoveActor(self.old)
self.old = self.new
if self.on_actor:
renderer.AddActor(self.new)
print("add_text called , ID : ",ID)
else:
pass
# renderer.RemoveActor(self.old)
# renderer.RemoveActor(self.new)
def MouseMove(self,obj,event):
Mousepos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(Mousepos[0], Mousepos[1], 0, self.GetDefaultRenderer())
self.NewPickedActor = picker.GetActor()
if self.NewPickedActor:
if not self.on_actor:
self.on_actor = True
print("on actor")
info = self.NewPickedActor.GetProperty().GetInformation()
info = str(info)
pattern_1 = re.compile(r"ID.*\d+")
pattern_2 = re.compile(r"\d+")
string = pattern_1.findall(info)[0]
ID = pattern_2.findall(string)[0]
# print("ID : ",ID)
self.add_text(self.GetDefaultRenderer() , ID)
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
self.NewPickedActor.GetProperty().SetDiffuse(1.0)
self.NewPickedActor.GetProperty().SetSpecular(0.0)
self.LastPickedActor = self.NewPickedActor
else:
pass
else:
print("not on actor")
self.on_actor = False
Update , the problem is solved, source code :
class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.AddObserver("LeftButtonReleaseEvent", self.leftButtonReleaseEvent)
self.AddObserver("MouseMoveEvent", self.MouseMove)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
self.new = vtkTextActor()
self.old = vtkTextActor()
self.on_actor = False
self.picker = vtk.vtkPropPicker()
def add_text(self,renderer,ID):
self.new.SetInput(ID)
txtprop = self.new.GetTextProperty()
txtprop.SetFontSize(72)
self.new.SetDisplayPosition(0,0)
self.old = self.new
if self.on_actor:
renderer.AddActor(self.new)
# print("ID : ",ID)
def MouseMove(self,obj,event):
Mousepos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(Mousepos[0], Mousepos[1], 0, self.GetDefaultRenderer())
self.NewPickedActor = picker.GetActor()
if self.NewPickedActor:
self.on_actor = True
# print("on actor")
info = self.NewPickedActor.GetProperty().GetInformation()
info = str(info)
pattern_1 = re.compile(r"ID.*\d+")
pattern_2 = re.compile(r"\d+")
string = pattern_1.findall(info)[0]
ID = pattern_2.findall(string)[0]
# print("ID : ",ID)
self.add_text(self.GetDefaultRenderer() , ID)
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
self.NewPickedActor.GetProperty().SetDiffuse(1.0)
self.NewPickedActor.GetProperty().SetSpecular(0.0)
self.LastPickedActor = self.NewPickedActor
self.OnLeftButtonDown()
self.OnLeftButtonUp()
else:
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
self.on_actor = False
self.GetDefaultRenderer().RemoveActor(self.new)
self.GetDefaultRenderer().RemoveActor(self.old)
self.OnLeftButtonDown()
self.OnLeftButtonUp()
def leftButtonPressEvent(self, obj, event):
self.RemoveObservers("MouseMoveEvent")
self.OnLeftButtonDown()
def leftButtonReleaseEvent(self, obj, event):
self.AddObserver("MouseMoveEvent",self.MouseMove)
self.OnLeftButtonUp()

Related

Let the GUI continue working after crashing without having to reset device

My current GUI is to show target tracking from a TI processor to PC via UART. Sometimes, GUI crashes due to UART performance. But everytime I want to reset GUI, I also have to reset my TI device. So I want to modify python code so as to only re-open GUI, enter COM port without having to reset device whenever GUI crashes.
Currently, each time GUI crashes, even though I re-opened GUI but didn't reset device (power off & on), I can not continue tracking.
I put python code for GUI below.In this GUI python program, we have 2 blanks & button to enter UART COM port, 2 buttons to select & send configuration to device. Then it can begin tracking.
I'm considering to modify the part of sendCfg(self), but still not sure how to make it work as my purpose due to my lack of experience in Python & GUI design. Please help me with sample code for this task.
class Window(QDialog):
def __init__(self, parent=None, size=[]):
super(Window, self).__init__(parent)
#when running Gree Demo
self.Gree = 1
# set window toolbar options, and title. #deb_gp
self.setWindowFlags(
Qt.Window |
Qt.CustomizeWindowHint |
Qt.WindowTitleHint |
Qt.WindowMinimizeButtonHint |
Qt.WindowMaximizeButtonHint |
Qt.WindowCloseButtonHint
)
self.setWindowTitle("mmWave People Counting")
print('Python is ', struct.calcsize("P")*8, ' bit')
print('Python version: ', sys.version_info)
gridlay = QGridLayout()
gridlay.addWidget(self.comBox, 0,0,1,1)
gridlay.addWidget(self.statBox, 1,0,1,1)
gridlay.addWidget(self.configBox,2,0,1,1)
gridlay.addWidget(self.plotControlBox,3,0,1,1)
gridlay.addWidget(self.boxTab,4,0,1,1)
gridlay.addWidget(self.spBox,5,0,1,1)
gridlay.addWidget(self.graphTabs,0,1,6,1)
gridlay.addWidget(self.gw, 0, 2, 6, 1)
#gridlay.addWidget(self.demoData, 0,3,1,2)
#gridlay.addWidget(self.hPlot,1,3,4,2)
gridlay.setColumnStretch(0,1)
gridlay.setColumnStretch(1,3)
self.setLayout(gridlay)
#
# left side pane layout
#
def setConnectionLayout(self):
self.comBox = QGroupBox('Connect to Com Ports')
self.uartCom = QLineEdit('') #deb_gp
self.dataCom = QLineEdit('') #deb_gp
self.uartLabel = QLabel('UART COM:')
self.dataLabel = QLabel('DATA COM:')
self.connectStatus = QLabel('Not Connected')
self.connectButton = QPushButton('Connect')
self.connectButton.clicked.connect(self.connectCom)
self.configLabel = QLabel('Config Type:')
self.configType = QComboBox()
self.configType.addItems(["3D People Counting", "SDK Out of Box Demo", "Long Range People Detection", "Indoor False Detection Mitigation", "(Legacy) 2D People Counting", "(Legacy): Overhead People Counting"])
self.comLayout = QGridLayout()
self.comLayout.addWidget(self.uartLabel,0,0)
self.comLayout.addWidget(self.uartCom,0,1)
self.comLayout.addWidget(self.dataLabel,1,0)
self.comLayout.addWidget(self.dataCom,1,1)
self.comLayout.addWidget(self.configLabel,2,0)
self.comLayout.addWidget(self.configType,2,1)
self.comLayout.addWidget(self.connectButton,3,0)
self.comLayout.addWidget(self.connectStatus,3,1)
self.comBox.setLayout(self.comLayout)
def setConfigLayout(self):
self.configBox = QGroupBox('Configuration')
self.selectConfig = QPushButton('Select Configuration')
self.sendConfig = QPushButton('Send Configuration')
self.selectConfig.clicked.connect(self.selectCfg)
self.sendConfig.clicked.connect(self.sendCfg)
self.configTable = QTableWidget(5,2)
#set parameter names
self.configTable.setItem(0,0,QTableWidgetItem('Radar Parameter'))
self.configTable.setItem(0,1,QTableWidgetItem('Value'))
self.configTable.setItem(1,0,QTableWidgetItem('Max Range'))
self.configTable.setItem(2,0,QTableWidgetItem('Range Resolution'))
self.configTable.setItem(3,0,QTableWidgetItem('Max Velocity'))
self.configTable.setItem(4,0,QTableWidgetItem('Velcoity Resolution'))
self.configLayout = QVBoxLayout()
self.configLayout.addWidget(self.selectConfig)
self.configLayout.addWidget(self.sendConfig)
self.configLayout.addWidget(self.configTable)
#self.configLayout.addStretch(1)
self.configBox.setLayout(self.configLayout)
def updateGraph(self, parsedData):
updateStart = int(round(time.time()*1000))
self.useFilter = 0
classifierOutput = []
pointCloud = parsedData[0]
targets = parsedData[1]
indexes = parsedData[2]
numPoints = parsedData[3]
numTargets = parsedData[4]
self.frameNum = parsedData[5]
fail = parsedData[6]
classifierOutput = parsedData[7]
fallDetEn = 0
indicesIn = []
if (fail != 1):
#left side
pointstr = 'Points: '+str(numPoints)
targetstr = 'Targets: '+str(numTargets)
self.numPointsDisplay.setText(pointstr)
self.numTargetsDisplay.setText(targetstr)
#right side fall detection
peopleStr = 'Number of Detected People: '+str(numTargets)
if (numTargets == 0):
fdestr = 'Fall Detection Disabled - No People Detected'
elif (numTargets == 1):
fdestr = 'Fall Detection Enabled'
fallDetEn = 1
elif (numTargets > 1):
fdestr = 'Fall Detected Disabled - Too Many People'
#self.numDetPeople.setText(peopleStr)
#self.fallDetEnabled.setText(fdestr)
if (len(targets) < 13):
targets = []
classifierOutput = []
if (fail):
return
#remove static points
if (self.configType.currentText() == '3D People Counting'):
if (not self.staticclutter.isChecked()):
statics = np.where(pointCloud[3,:] == 0)
try:
firstZ = statics[0][0]
numPoints = firstZ
pointCloud = pointCloud[:,:firstZ]
indexes = indexes[:,:self.previousFirstZ]
self.previousFirstZ = firstZ
except:
firstZ = -1
#point cloud persistence
fNum = self.frameNum%10
if (numPoints):
self.previousCloud[:5,:numPoints,fNum] = pointCloud[:5,:numPoints]
self.previousCloud[5,:len(indexes),fNum] = indexes
self.previousPointCount[fNum]=numPoints
#plotting 3D - get correct point cloud (persistent points and synchronize the frame)
if (self.configType.currentText() == 'SDK3xPeopleCount'):
pointIn = pointCloud
else:
totalPoints = 0
persistentFrames = int(self.persistentFramesInput.currentText())
#allocate new array for all the points
for i in range(1,persistentFrames+1):
totalPoints += self.previousPointCount[fNum-i]
pointIn = np.zeros((5,int(totalPoints)))
indicesIn = np.ones((1, int(totalPoints)))*255
totalPoints = 0
#fill array
for i in range(1,persistentFrames+1):
prevCount = int(self.previousPointCount[fNum-i])
pointIn[:,totalPoints:totalPoints+prevCount] = self.previousCloud[:5,:prevCount,fNum-i]
if (numTargets > 0):
indicesIn[0,totalPoints:totalPoints+prevCount] = self.previousCloud[5,:prevCount,fNum-i]
totalPoints+=prevCount
if (self.graphFin):
self.plotstart = int(round(time.time()*1000))
self.graphFin = 0
if (self.threeD):
try:
indicesIn = indicesIn[0,:]
except:
indicesIn = []
self.get_thread = updateQTTargetThread3D(pointIn, targets, indicesIn, self.scatter, self.pcplot, numTargets, self.ellipsoids, self.coordStr, classifierOutput, self.zRange, self.gw, self.plotByIndex.isChecked(), self.plotTracks.isChecked(), self.bbox,self.boundaryBoxes[0]['checkEnable'].isChecked())
self.get_thread.done.connect(self.graphDone)
self.get_thread.start(priority=QThread.HighestPriority-1)
else:
npc = pointIn[0:2,:]
print (np.shape(npc))
self.legacyThread = update2DQTGraphThread(npc, targets, numTargets, indexes, numPoints, self.trailData, self.activeTrail, self.trails, self.scatter2D, self.gatingScatter)
self.legacyThread.done.connect(self.graphDone)
self.legacyThread.start(priority=QThread.HighestPriority-1)
else:
return
#pointIn = self.previousCloud[:,:int(self.previousPointCount[fNum-1]),fNum-1]
#state tracking
if (numTargets > 0):
self.lastFrameHadTargets = True
else:
self.lastFrameHadTargets = False
if (numTargets):
self.lastTID = targets[0,:]
else:
self.lastTID = []
def graphDone(self):
plotend = int(round(time.time()*1000))
plotime = plotend - self.plotstart
try:
if (self.frameNum > 1):
self.averagePlot = (plotime*1/self.frameNum) + (self.averagePlot*(self.frameNum-1)/(self.frameNum))
else:
self.averagePlot = plotime
except:
self.averagePlot = plotime
self.graphFin = 1
pltstr = 'Average Plot time: '+str(plotime)[:5] + ' ms'
fnstr = 'Frame: '+str(self.frameNum)
self.frameNumDisplay.setText(fnstr)
self.plotTimeDisplay.setText(pltstr)
def resetFallText(self):
self.fallAlert.setText('Standing')
self.fallPic.setPixmap(self.standingPicture)
self.fallResetTimerOn = 0
def updateFallThresh(self):
try:
newThresh = float(self.fallThreshInput.text())
self.fallThresh = newThresh
self.fallThreshMarker.setPos(self.fallThresh)
except:
print('No numberical threshold')
def connectCom(self):
#get parser
self.parser = uartParserSDK(type=self.configType.currentText())
self.parser.frameTime = self.frameTime
print('Parser type: ',self.configType.currentText())
#init threads and timers
self.uart_thread = parseUartThread(self.parser)
if (self.configType.currentText() != 'Replay'):
self.uart_thread.fin.connect(self.parseData)
self.uart_thread.fin.connect(self.updateGraph)
self.parseTimer = QTimer()
self.parseTimer.setSingleShot(False)
self.parseTimer.timeout.connect(self.parseData)
try:
uart = "COM"+ self.uartCom.text() #deb_gp
data = "COM"+ self.dataCom.text() #deb_gp
#TODO: find the serial ports automatically.
self.parser.connectComPorts(uart, data)
self.connectStatus.setText('Connected') #deb_gp
self.connectButton.setText('Disconnect') #deb_gp
#TODO: create the disconnect button action
except:
self.connectStatus.setText('Unable to Connect')
if (self.configType.currentText() == "Replay"):
self.connectStatus.setText('Replay')
if (self.configType.currentText() == "Long Range People Detection"):
self.frameTime = 400
#
# Select and parse the configuration file
# TODO select the cfgfile automatically based on the profile.
def selectCfg(self):
try:
self.parseCfg(self.selectFile())
except:
print('No cfg file selected!')
def selectFile(self):
fd = QFileDialog()
filt = "cfg(*.cfg)"
filename = fd.getOpenFileName(directory='./../chirp_configs',filter=filt) #deb_gp - added folder name
return filename[0]
def parseCfg(self, fname):
cfg_file = open(fname, 'r')
self.cfg = cfg_file.readlines()
counter = 0
chirpCount = 0
for line in self.cfg:
args = line.split()
if (len(args) > 0):
if (args[0] == 'cfarCfg'):
zy = 4
#self.cfarConfig = {args[10], args[11], '1'}
elif (args[0] == 'AllocationParam'):
zy=3
#self.allocConfig = tuple(args[1:6])
elif (args[0] == 'GatingParam'):
zy=2
#self.gatingConfig = tuple(args[1:4])
elif (args[0] == 'SceneryParam' or args[0] == 'boundaryBox'):
self.boundaryLine = counter
self.profile['leftX'] = float(args[1])
self.profile['rightX'] = float(args[2])
self.profile['nearY'] = float(args[3])
self.profile['farY'] = float(args[4])
self.setBoundaryTextVals(self.profile)
self.boundaryBoxes[0]['checkEnable'].setChecked(True)
elif (args[0] == 'staticBoundaryBox'):
self.staticLine = counter
elif (args[0] == 'profileCfg'):
self.profile['startFreq'] = float(args[2])
self.profile['idle'] = float(args[3])
self.profile['adcStart'] = float(args[4])
self.profile['rampEnd'] = float(args[5])
self.profile['slope'] = float(args[8])
self.profile['samples'] = float(args[10])
self.profile['sampleRate'] = float(args[11])
print(self.profile)
elif (args[0] == 'frameCfg'):
self.profile['numLoops'] = float(args[3])
self.profile['numTx'] = float(args[2])+1
elif (args[0] == 'chirpCfg'):
chirpCount += 1
elif (args[0] == 'sensorPosition'):
self.profile['sensorHeight'] = float(args[1])
self.profile['az_tilt'] = float(args[2])
self.profile['elev_tilt'] = float(args[3])
counter += 1
maxRange = self.profile['sampleRate']*1e3*0.9*3e8/(2*self.profile['slope']*1e12)
#update boundary box
self.drawBoundaryGrid(maxRange)
#update chirp table values
bw = self.profile['samples']/(self.profile['sampleRate']*1e3)*self.profile['slope']*1e12
rangeRes = 3e8/(2*bw)
Tc = (self.profile['idle']*1e-6 + self.profile['rampEnd']*1e-6)*chirpCount
lda = 3e8/(self.profile['startFreq']*1e9)
maxVelocity = lda/(4*Tc)
velocityRes = lda/(2*Tc*self.profile['numLoops']*self.profile['numTx'])
self.configTable.setItem(1,1,QTableWidgetItem(str(maxRange)[:5]))
self.configTable.setItem(2,1,QTableWidgetItem(str(rangeRes)[:5]))
self.configTable.setItem(3,1,QTableWidgetItem(str(maxVelocity)[:5]))
self.configTable.setItem(4,1,QTableWidgetItem(str(velocityRes)[:5]))
#update sensor position
self.az_tilt.setText(str(self.profile['az_tilt']))
self.elev_tilt.setText(str(self.profile['elev_tilt']))
self.s_height.setText(str(self.profile['sensorHeight']))
def sendCfg(self):
try:
if (self.configType.currentText() != "Replay"):
self.parser.sendCfg(self.cfg)
self.configSent = 1
self.parseTimer.start(self.frameTime)
except:
print ('No cfg file selected!')
# Needed ?? deb_gp
# def setParser(self, uParser):
# self.parser = uParser
def parseData(self):
self.uart_thread.start(priority=QThread.HighestPriority)
def whoVisible(self):
if (self.threeD):
self.threeD = 0
else:
self.threeD = 1
print('3d: ', self.threeD)
if __name__ == '__main__':
if (compileGui):
appctxt = ApplicationContext()
app = QApplication(sys.argv)
screen = app.primaryScreen()
size = screen.size()
main = Window(size=size)
main.show()
exit_code = appctxt.app.exec_()
sys.exit(exit_code)
else:
app = QApplication(sys.argv)
screen = app.primaryScreen()
size = screen.size()
main = Window(size=size)
main.show()
sys.exit(app.exec_())

PyQT table update crash easyly

i first use PyQT4 .
i'm create a QTableWidget to show runing message...
when my program run, it ill crash Within ten minutes.
i try diable my TableUpdate function , and it's don't crash again.
there is my code please help me
class table_work(QThread):
TableDataSignal = pyqtSignal()
def __init__(self,main_self):
# QThread.__init__(self)
super(table_work, self).__init__(main_self)
self.main_self = main_self
self.table_update_list = list()
#pyqtSlot(dict)
def update_table_thread_o(self,work):
try:
row_pos = work['row_position']
data = work['data']
table_key_sort = work['key_sort']
this_table = work['table']
k = 0
for table_key in table_key_sort:
this_table.setItem(row_pos, k, QTableWidgetItem(unicode(data[table_key])))
k += 1
del work
except:
pass
def update_table_thread(self):
main_self = self.main_self
table_work_list = self.table_update_list
while 1:
for work in self.table_update_list:
row_pos = work['row_position']
data = work['data']
table_key_sort = work['key_sort']
this_table = work['table']
k = 0
for table_key in table_key_sort:
this_table.setItem(row_pos, k, QTableWidgetItem(unicode(data[table_key])))
k += 1
time.sleep(0.5)
def run(self):
self.update_table_thread()
this's update table message
def update_table(self,address,change_obj=None,tabe_name='auto_card'):
sample_dict = dict()
table_key_sort = list()
now_table_sort = 0
if tabe_name == "auto_bot":
this_table = self.auto_bot_procc_table
table_data_list = self.auto_bot_procc_table_list
now_table_sort = self.auto_bot_now_table_sort
sample_dict['address'] = address
sample_dict['money'] = 0
sample_dict['run_time'] = 0
sample_dict['item_cd'] = u"60分鐘後"
sample_dict['stat'] = "Ready..."
sample_dict['sort'] = now_table_sort
table_key_sort.append('address')
table_key_sort.append('money')
table_key_sort.append('run_time')
table_key_sort.append('item_cd')
table_key_sort.append('stat')
if tabe_name == "auto_card":
this_table = self.process_table
table_data_list = self.now_procc_table_list
now_table_sort = self.now_table_sort
sample_dict['address'] = address
sample_dict['done_num'] = 0
sample_dict['pre_item'] = ""
sample_dict['procc'] = "Ready"
sample_dict['mission_procc'] = u"待命.."
sample_dict['mission_num'] = 0
sample_dict['mission_line'] = 0
sample_dict['update_time'] = db.get_time()
sample_dict['sort'] = now_table_sort
sample_dict['option'] = ""
table_key_sort.append('address')
table_key_sort.append('done_num')
table_key_sort.append('pre_item')
table_key_sort.append('mission_procc')
table_key_sort.append('procc')
table_key_sort.append('mission_num')
table_key_sort.append('mission_line')
table_key_sort.append('update_time')
if address not in table_data_list:
this_table.insertRow(sample_dict['sort'])
table_data_list[address] = sample_dict
sample_dict['sort'] = self.auto_bot_now_table_sort
self.auto_bot_now_table_sort += 1
acc_data = table_data_list[address]
if change_obj != None:
key = change_obj['key']
val = change_obj['val']
if key in acc_data:
acc_data[key] = val
acc_data['update_time'] = db.get_time()
rowPosition = acc_data['sort']
temp = dict()
temp['row_position'] = rowPosition
temp['data'] = acc_data
temp['key_sort'] = table_key_sort
temp['table'] = this_table
self.TableDataSignal.emit(temp)
del temp
Some time i get a ANS.
i'm a PYQT newbie , After this period of various projects experience.
I understand if you don't use Main Thread to Change UI, Always use sign/emit
even your code is worked,but always use sign/emit, Otherwise there will be a series of disasters.
you just like
class sample(QtCore.QThread):
table_data_change = QtCore.pyqtSignal(dict)
def __init__(self,main_win):
self.main = main_win
self.table_data_change.connect(self.main.change_fn)
def test(self):
data = dict()
data['btn'] = .....
data['val'] = .....
self.table_data_change.emit(data)
Save your time !

function, not defined by using def

I am very new to Python, however I am in need of using Python for my project, I have been learning on the go. There is a point I don't understand with the piece of code below:
result['data'] = pool.map(image_processor, im_info)
So, I figured out pool.map(somefunc,[list of params]) is akin to parfor of Matlab. What I don't understand is the part relating image_processor. I am assuming it should be a function, however it is not defined as one using def. The code is working perfectly fine and I tried to trace it with pdb and it seems to process the images as intended in batches, for caffe input. This might be a very trivial question, I just am new to Python. So what is the order of execution that image_processor just works and how can I make changes in this function (I will be passing multiple parameters, I just need to know where I can make those changes as the function is not defined using a def) Thx.
#Data layer for video. Change flow_frames and RGB_frames to be the path to the flow and RGB frames.
import sys
sys.path.append('../../python')
import caffe
import io
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
import time
import pdb
import glob
import pickle as pkl
import random
import h5py
from multiprocessing import Pool
from threading import Thread
import skimage.io
import copy
flow_frames = 'flow_images/'
RGB_frames = 'frames/'
test_frames = 16
train_frames = 16
test_buffer = 3
train_buffer = 24
def processImageCrop(im_info, transformer, flow):
im_path = im_info[0]
im_crop = im_info[1]
im_reshape = im_info[2]
im_flip = im_info[3]
data_in = caffe.io.load_image(im_path)
if (data_in.shape[0] < im_reshape[0]) | (data_in.shape[1] < im_reshape[1]):
data_in = caffe.io.resize_image(data_in, im_reshape)
if im_flip:
data_in = caffe.io.flip_image(data_in, 1, flow)
data_in = data_in[im_crop[0]:im_crop[2], im_crop[1]:im_crop[3], :]
processed_image = transformer.preprocess('data_in',data_in)
return processed_image
class ImageProcessorCrop(object):
def __init__(self, transformer, flow):
self.transformer = transformer
self.flow = flow
def __call__(self, im_info):
return processImageCrop(im_info, self.transformer, self.flow)
class sequenceGeneratorVideo(object):
def __init__(self, buffer_size, clip_length, num_videos, video_dict, video_order):
self.buffer_size = buffer_size
self.clip_length = clip_length
self.N = self.buffer_size*self.clip_length
self.num_videos = num_videos
self.video_dict = video_dict
self.video_order = video_order
self.idx = 0
def __call__(self):
label_r = []
im_paths = []
im_crop = []
im_reshape = []
im_flip = []
if self.idx + self.buffer_size >= self.num_videos:
idx_list = range(self.idx, self.num_videos)
idx_list.extend(range(0, self.buffer_size-(self.num_videos-self.idx)))
else:
idx_list = range(self.idx, self.idx+self.buffer_size)
for i in idx_list:
key = self.video_order[i]
label = self.video_dict[key]['label']
video_reshape = self.video_dict[key]['reshape']
video_crop = self.video_dict[key]['crop']
label_r.extend([label]*self.clip_length)
im_reshape.extend([(video_reshape)]*self.clip_length)
r0 = int(random.random()*(video_reshape[0] - video_crop[0]))
r1 = int(random.random()*(video_reshape[1] - video_crop[1]))
im_crop.extend([(r0, r1, r0+video_crop[0], r1+video_crop[1])]*self.clip_length)
f = random.randint(0,1)
im_flip.extend([f]*self.clip_length)
rand_frame = int(random.random()*(self.video_dict[key]['num_frames']-self.clip_length)+1+1)
frames = []
for i in range(rand_frame,rand_frame+self.clip_length):
frames.append(self.video_dict[key]['frames'] %i)
im_paths.extend(frames)
im_info = zip(im_paths,im_crop, im_reshape, im_flip)
self.idx += self.buffer_size
if self.idx >= self.num_videos:
self.idx = self.idx - self.num_videos
return label_r, im_info
def advance_batch(result, sequence_generator, image_processor, pool):
label_r, im_info = sequence_generator()
#tmp = image_processor(im_info[0])
result['data'] = pool.map(image_processor, im_info)
result['label'] = label_r
cm = np.ones(len(label_r))
cm[0::16] = 0
result['clip_markers'] = cm
class BatchAdvancer():
def __init__(self, result, sequence_generator, image_processor, pool):
self.result = result
self.sequence_generator = sequence_generator
self.image_processor = image_processor
self.pool = pool
def __call__(self):
return advance_batch(self.result, self.sequence_generator, self.image_processor, self.pool)
class videoRead(caffe.Layer):
def initialize(self):
self.train_or_test = 'test'
self.flow = False
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_testVideos.txt'
def setup(self, bottom, top):
random.seed(10)
self.initialize()
f = open(self.video_list, 'r')
f_lines = f.readlines()
f.close()
video_dict = {}
current_line = 0
self.video_order = []
for ix, line in enumerate(f_lines):
video = line.split(' ')[0].split('/')[1]
l = int(line.split(' ')[1])
frames = glob.glob('%s%s/*.jpg' %(self.path_to_images, video))
num_frames = len(frames)
video_dict[video] = {}
video_dict[video]['frames'] = frames[0].split('.')[0] + '.%04d.jpg'
video_dict[video]['reshape'] = (240,320)
video_dict[video]['crop'] = (227, 227)
video_dict[video]['num_frames'] = num_frames
video_dict[video]['label'] = l
self.video_order.append(video)
self.video_dict = video_dict
self.num_videos = len(video_dict.keys())
#set up data transformer
shape = (self.N, self.channels, self.height, self.width)
self.transformer = caffe.io.Transformer({'data_in': shape})
self.transformer.set_raw_scale('data_in', 255)
if self.flow:
image_mean = [128, 128, 128]
self.transformer.set_is_flow('data_in', True)
else:
image_mean = [103.939, 116.779, 128.68]
self.transformer.set_is_flow('data_in', False)
channel_mean = np.zeros((3,227,227))
for channel_index, mean_val in enumerate(image_mean):
channel_mean[channel_index, ...] = mean_val
self.transformer.set_mean('data_in', channel_mean)
self.transformer.set_channel_swap('data_in', (2, 1, 0))
self.transformer.set_transpose('data_in', (2, 0, 1))
self.thread_result = {}
self.thread = None
pool_size = 24
self.image_processor = ImageProcessorCrop(self.transformer, self.flow)
self.sequence_generator = sequenceGeneratorVideo(self.buffer_size, self.frames, self.num_videos, self.video_dict, self.video_order)
self.pool = Pool(processes=pool_size)
self.batch_advancer = BatchAdvancer(self.thread_result, self.sequence_generator, self.image_processor, self.pool)
self.dispatch_worker()
self.top_names = ['data', 'label','clip_markers']
print 'Outputs:', self.top_names
if len(top) != len(self.top_names):
raise Exception('Incorrect number of outputs (expected %d, got %d)' %
(len(self.top_names), len(top)))
self.join_worker()
for top_index, name in enumerate(self.top_names):
if name == 'data':
shape = (self.N, self.channels, self.height, self.width)
elif name == 'label':
shape = (self.N,)
elif name == 'clip_markers':
shape = (self.N,)
top[top_index].reshape(*shape)
def reshape(self, bottom, top):
pass
def forward(self, bottom, top):
if self.thread is not None:
self.join_worker()
#rearrange the data: The LSTM takes inputs as [video0_frame0, video1_frame0,...] but the data is currently arranged as [video0_frame0, video0_frame1, ...]
new_result_data = [None]*len(self.thread_result['data'])
new_result_label = [None]*len(self.thread_result['label'])
new_result_cm = [None]*len(self.thread_result['clip_markers'])
for i in range(self.frames):
for ii in range(self.buffer_size):
old_idx = ii*self.frames + i
new_idx = i*self.buffer_size + ii
new_result_data[new_idx] = self.thread_result['data'][old_idx]
new_result_label[new_idx] = self.thread_result['label'][old_idx]
new_result_cm[new_idx] = self.thread_result['clip_markers'][old_idx]
for top_index, name in zip(range(len(top)), self.top_names):
if name == 'data':
for i in range(self.N):
top[top_index].data[i, ...] = new_result_data[i]
elif name == 'label':
top[top_index].data[...] = new_result_label
elif name == 'clip_markers':
top[top_index].data[...] = new_result_cm
self.dispatch_worker()
def dispatch_worker(self):
assert self.thread is None
self.thread = Thread(target=self.batch_advancer)
self.thread.start()
def join_worker(self):
assert self.thread is not None
self.thread.join()
self.thread = None
def backward(self, top, propagate_down, bottom):
pass
class videoReadTrain_flow(videoRead):
def initialize(self):
self.train_or_test = 'train'
self.flow = True
self.buffer_size = train_buffer #num videos processed per batch
self.frames = train_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = flow_frames
self.video_list = 'ucf101_split1_trainVideos.txt'
class videoReadTest_flow(videoRead):
def initialize(self):
self.train_or_test = 'test'
self.flow = True
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = flow_frames
self.video_list = 'ucf101_split1_testVideos.txt'
class videoReadTrain_RGB(videoRead):
def initialize(self):
self.train_or_test = 'train'
self.flow = False
self.buffer_size = train_buffer #num videos processed per batch
self.frames = train_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_trainVideos.txt'
class videoReadTest_RGB(videoRead):
def initialize(self):
self.train_or_test = 'test'
self.flow = False
self.buffer_size = test_buffer #num videos processed per batch
self.frames = test_frames #length of processed clip
self.N = self.buffer_size*self.frames
self.idx = 0
self.channels = 3
self.height = 227
self.width = 227
self.path_to_images = RGB_frames
self.video_list = 'ucf101_split1_testVideos.txt'
The code is from https://github.com/LisaAnne/lisa-caffe-public/blob/lstm_video_deploy/examples/LRCN_activity_recognition/sequence_input_layer.py
See this line:
self.image_processor = ImageProcessorCrop(self.transformer, self.flow)
The ImageProcessorCrop class has a __call__ method, which allows these objects to be used as if they're functions. When you call them, it calls that method.

pyqtgraph plot widget is not update

I have two working stream.
1 - receiving data from com port and sends signal.
def __packet_parser(self, *args):
while self.__thred_is_runing:
data = self.__connection.read_all()
if data != b'':
self.change_data.emit(self.__readline())
self.__callback(self.__readline())
2 - draw graph.
def set_value_by_plot_name(self, value, name='default'):
self.__plots[name].setData(np.append(self.__plots[name].getData()[1][1:], value))
def __test(self, value):
tmp = value.decode('windows-1252')
data = tmp.split('\t')
if len(data) == 10:
self.__gr.set_value_by_plot_name(int(data[1]))
def main(self):
self.__window = Window()
self.__gr = Graphics()
w = self.__gr.get_widget()
self.__window.add_widget(w)
connect = COMPortConnection('COM7', 38400)
connect.change_data.connect(self.__test)
connect.open()
self.__window.show()
few seconds everything works fine, and then ceases to be updated.
What problem?
Data is updated, picture is not.
I had a similar problem, i solved it by passing by an intermediate method i'm not sure why:
class GraphWindowController:
# init ...
def start_graph(self):
# ... process
self.view.run_graph(self.graph_nodes)
stream_signals_and_slots = []
my_steam_signal.connect(self.add_value_to_graph)
def add_value_to_graph(self, values):
self.view.update(values)
class GraphWindow(QtGui.QFrame, Ui_GraphWindow):
def __init__(self, parent=None):
"""Initializer."""
super(GraphWindow, self).__init__(parent)
self.setupUi(self)
self.connect_signals()
self.plot_widget = None
self.y_data = []
self.x_data = []
self.curve = []
self.display_range = None
self.velocity = None
self.values_to_display = None
self.count_value_to_display = 0
self.legend = None
self.build_gui()
# Doing stuff ...
def build_gui(self):
self.plot_widget = pg.PlotWidget()
layout = QtGui.QGridLayout()
self.PlotLayout.setLayout(layout)
layout.addWidget(self.plot_widget, 0,1)
def configure_graph(self, display_range, velocity):
self.display_range = display_range
self.velocity = velocity
self.plot_widget.setLabels(left='axis 1')
self.legend = pg.LegendItem((100, 60), offset=(70, 30))
self.legend.setParentItem(self.plot_widget.graphicsItem())
self.plot_widget.showGrid(True, True, 251)
self.plot_widget.showButtons()
self.plot_widget.setRange(rect=None, xRange=[0, self.display_range], yRange=[
1300, -300], padding=0.09, update=True, disableAutoRange=True)
def run_graph(self, values_to_display):
self.values_to_display = values_to_display
self.curve_number = len(self.values_to_display)
for i in range(self.curve_number):
self.y_data.append([])
colors_to_use = self.enumerate_colors(
self.curve_number)
# Blablabla doing stuff ...
def update(self, my_values):
value = my_values
for i in range(self.curve_number):
if len(self.y_data[i]) >= int((self.display_range * 1000) / self.velocity):
del self.y_data[i][0]
else:
my_var = len(self.x_data) * (self.velocity / 1000.0)
self.x_data.append(my_var)
self.y_data[i].append(int(value[i]))
my_data_to_display = list(
zip(self.x_data, self.y_data[i]))
my_array = np.array(my_data_to_display)
self.curve[i].setData(my_array)

"WindowsError: access violation 000001" on DispatchMessageW in EventLoop

I research Python internals and write GUI Generic windows use Windows API.
# -*- coding: utf-8 -*-
import time
import threading
import logging
import uuid
import ctypes
import ctypes.wintypes
import const
INT = ctypes.wintypes.c_int
LPVOID = ctypes.wintypes.c_void_p
HCURSOR = ctypes.wintypes.HANDLE
LRESULT = ctypes.wintypes.LPARAM
COLORREF = ctypes.wintypes.DWORD
PVOID = ctypes.wintypes.c_void_p
WCHAR = ctypes.wintypes.c_wchar
BCHAR = ctypes.wintypes.c_wchar
LPRECT = ctypes.wintypes.POINTER(ctypes.wintypes.RECT)
LPPOINT = ctypes.wintypes.POINTER(ctypes.wintypes.POINT)
LPMSG = ctypes.wintypes.POINTER(ctypes.wintypes.MSG)
UINT_PTR = ctypes.wintypes.HANDLE
LONG_PTR = ctypes.wintypes.HANDLE
#WNDPROC = ctypes.wintypes.WINFUNCTYPE(ctypes.c_long, ctypes.wintypes.HWND, ctypes.wintypes.UINT, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM)
WNDPROC = ctypes.wintypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int)
SW_SHOWNORMAL = 1
SW_SHOW = 5
CS_HREDRAW = 2
CS_VREDRAW = 1
CW_USEDEFAULT = 0x80000000
WM_DESTROY = 2
class WNDCLASSEX(ctypes.wintypes.Structure):
_fields_ = [("cbSize", ctypes.wintypes.UINT),
("style", ctypes.wintypes.UINT),
("lpfnWndProc", WNDPROC),
("cbClsExtra", ctypes.wintypes.INT),
("cbWndExtra", ctypes.wintypes.INT),
("hInstance", ctypes.wintypes.HANDLE),
("hIcon", ctypes.wintypes.HANDLE),
("hCursor", ctypes.wintypes.HANDLE),
("hBrush", ctypes.wintypes.HANDLE),
("lpszMenuName", ctypes.wintypes.LPCWSTR),
("lpszClassName", ctypes.wintypes.LPCWSTR),
("hIconSm", ctypes.wintypes.HANDLE)]
class Window(object):
def __init__(self, app, wclassName=None, wname=u"Custom Title"):
self.__app = app
#
self.__log = logging.getLogger("win_ui.window")
#
self.__hInst = app.getInstance()
self.__hWnd = None
#
if wclassName is None:
wclassName = u"NewWindowClass-{uid}".format(uid=uuid.uuid4().hex)
#
self.__wclassName = wclassName
self.__wname = wname
#
#self.__event_dispatcher = EventDispatcher(app=app)
#self.__event_dispatcher.start()
#
self.__has_exit = threading.Condition()
#
self._create()
def wnd_proc(self, hWnd, Msg, wParam, lParam):
self.__log.debug("WndProc: hWnd={hWnd!r}, Msg={Msg!r}, wParam={wParam!r}, lParam={lParam!r}".format(hWnd=hWnd, Msg=Msg, wParam=wParam, lParam=lParam))
#self.__event_dispatcher.push((hWnd, Msg, wParam, lParam))
#
result = None
#
self.__has_exit.acquire()
self.__has_exit.wait(0.1)
#
if Msg == WM_DESTROY:
self.__app.loadLibrary("user32").PostQuitMessage(0)
result = 0
#
if result is None:
result = self.__app.loadLibrary("user32").DefWindowProcW(hWnd, Msg, wParam, lParam)
#
self.__has_exit.release()
#
return result
def _create(self):
WndProc = WNDPROC(self.wnd_proc)
hBrush = self.__app.loadLibrary("gdi32").GetStockObject(const.LTGRAY_BRUSH)
print hBrush
wndClass = WNDCLASSEX()
wndClass.cbSize = ctypes.sizeof(WNDCLASSEX)
wndClass.style = CS_HREDRAW | CS_VREDRAW
wndClass.lpfnWndProc = WndProc
wndClass.cbClsExtra = 0
wndClass.cbWndExtra = 0
wndClass.hInstance = self.__hInst
wndClass.hIcon = 0 #self.__app.loadIcon()
wndClass.hCursor = 0 #self.__app.loadCursor()
wndClass.hBrush = hBrush
wndClass.lpszMenuName = None
wndClass.lpszClassName = ctypes.c_wchar_p(self.__wclassName)
wndClass.hIconSm = 0 #self.__app.loadCursor()
self.__log.debug("wndClass.cbSize = {cbSize!r}".format(cbSize=wndClass.cbSize))
self.__log.debug("wndClass.lpfnWndProc = {lpfnWndProc!r}".format(lpfnWndProc=wndClass.lpfnWndProc))
self.__log.debug("wndClass.hInstance = {hInst!r}".format(hInst=wndClass.hInstance))
self.__log.debug("wndClass.lpszClassName = {lpszClassName!r}".format(lpszClassName=wndClass.lpszClassName))
result = self.__app.loadLibrary("user32").RegisterClassExW(ctypes.byref(wndClass))
self.__log.debug("0x0{result:x} = RegisterClassExW(...)".format(result=result))
#
self.__x = CW_USEDEFAULT
self.__y = CW_USEDEFAULT
self.__width = 300
self.__height = 300
#
exStyle = 0
#exStyle = const.WS_EX_APPWINDOW
style = const.WS_OVERLAPPEDWINDOW
#style = const.WS_OVERLAPPEDWINDOW | const.WS_CAPTION | const.WS_VISIBLE
wclassName = ctypes.c_wchar_p(self.__wclassName)
wname = ctypes.c_wchar_p(self.__wname)
print wname
#
self.__app.loadLibrary("user32").CreateWindowExW.restype = ctypes.wintypes.HWND
self.__app.loadLibrary("user32").CreateWindowExW.argtypes = [ctypes.wintypes.DWORD, ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.wintypes.DWORD, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.wintypes.HWND, ctypes.wintypes.HMENU, ctypes.wintypes.HINSTANCE, ctypes.wintypes.LPVOID]
#
self.__hWnd = self.__app.loadLibrary("user32").CreateWindowExW(exStyle, wclassName, wname, style, self.__x, self.__y, self.__width, self.__height, 0, None, self.__hInst, 0)
self.__log.debug("0x0{hWnd!r} = CreateWindowExW(...)".format(hWnd=self.__hWnd))
if not self.__hWnd:
print self.__app.getLastError()
raise WindowsError("Failed to create window")
self.__log.debug("ShowWindow(hWnd=0x{hWnd:x})".format(hWnd=self.__hWnd))
result = self.__app.loadLibrary("user32").ShowWindow(self.__hWnd, SW_SHOW)
self.__log.debug('0x0{result:x} = ShowWindow(...)'.format(result=result))
self.__log.debug('UpdateWindow(hWnd=0x{hWnd:x})'.format(hWnd=self.__hWnd))
result = self.__app.loadLibrary("user32").UpdateWindow(self.__hWnd)
self.__log.debug('0x0{result:x} = UpdateWindow(...)'.format(result=result))
GUI generic window create but cause exception "WidnowsError: access violation 000001" on DispatchMessageW in EventLoop.
# -*- coding: utf-8 -*-
import logging
import uuid
import ctypes
import ctypes.wintypes
import const
class Application(object):
def __init__(self):
self.__log = logging.getLogger("win_ui.application")
self.__libs = {}
#
self._event_thread = self.loadLibrary("kernel32").GetCurrentThreadId()
def loadLibrary(self, name):
if name in self.__libs:
result = self.__libs[name]
else:
module = getattr(ctypes.windll, name)
result = self.__libs[name] = module
return result
def getInstance(self, hModule=None):
self.__log.debug('GetModuleHandleW(hModule={hModule!r})'.format(hModule=hModule))
result = self.loadLibrary("kernel32").GetModuleHandleW(None)
self.__log.debug('0x0{result:x} = GetModuleHandleW(...)'.format(result=result))
return result
def loadIcon(self, iconName=None):
if iconName is None:
iconName = const.IDI_APPLICATION
hInst = self.getInstance()
result = self.loadLibrary("user32").LoadIconW(hInst, iconName)
return result
def loadCursor(self, v=None):
if v is None:
v = const.IDI_APPLICATION
hInst = self.getInstance()
result = self.loadLibrary("user32").LoadCursorW(hInst, v)
return result
def getLastError(self):
self.__log.debug('GetLastError()')
result = self.loadLibrary("kernel32").GetLastError()
#print result
#rs = self.loadLibrary("kernel32").FormatMessageW(result)
#print rs
self.__log.debug('0x0{result:x} = GetLastError()'.format(result=result))
return result
def check(self):
if self._event_thread != self.loadLibrary("kernel32").GetCurrentThreadId():
raise RuntimeError("Thread?")
def run(self):
self.check()
msg = ctypes.wintypes.MSG()
lpmsg = ctypes.pointer(msg)
self.__log.debug('Entering message loop')
#
user32 = self.loadLibrary("user32")
while user32.GetMessageW(lpmsg, 0, 0, 0) != 0:
#while self.loadLibrary("user32").PeekMessageW(ctypes.byref(msg), 0, 0, 0, const.PM_REMOVE):
user32.TranslateMessage(lpmsg)
print lpmsg
user32.DispatchMessageW(lpmsg)
#
self.__log.debug('done.')
When I refactoring and calls RegisterWindowEx and CreateWindowEx on separate Python method CreateWindowEx start return 0x0 and GetLastError retrun 0x0. What problem there?

Categories

Resources