233 lines
8.0 KiB
Python
233 lines
8.0 KiB
Python
from shutil import copyfile
|
|
import threading
|
|
|
|
|
|
class ImageIntensity(DeviceBase, Readable):
|
|
def __init__(self, image):
|
|
DeviceBase.__init__(self, image.name + " intensity")
|
|
self.setParent(image)
|
|
|
|
def read(self):
|
|
ret = self.getParent().read()
|
|
if ret is not None:
|
|
(I_sum, area_I, thresh1_count, thresh2_count, thresh3_count, thresh4_count, I_sum_bgr, area_bgr) = ret
|
|
ret = I_sum
|
|
#if area_bgr > 0:
|
|
# ret = ret - ((I_sum_bgr / area_bgr) * area_I)
|
|
self.setCache(ret, None)
|
|
return ret
|
|
|
|
|
|
class ImageCorrectedIntensity(DeviceBase, Readable):
|
|
def __init__(self, image):
|
|
DeviceBase.__init__(self, image.name + " corrected intensity")
|
|
self.setParent(image)
|
|
|
|
def read(self):
|
|
ret = self.getParent().intensity.take()
|
|
if ret is not None:
|
|
trans = transm.value
|
|
count_time = get_count_time()
|
|
if (trans == 0.0) or (count_time==0):
|
|
ret= float("nan")
|
|
else:
|
|
ret = ret / count_time / transm.value
|
|
self.setCache(ret, None)
|
|
return ret
|
|
|
|
class ImageMatrix(ReadonlyRegisterBase, ReadonlyRegisterMatrix, Readable.IntegerType):
|
|
def __init__(self, image):
|
|
DeviceBase.__init__(self, "Matrix")
|
|
self.setParent(image)
|
|
|
|
def doRead(self):
|
|
#print "----- DO READ --------------"
|
|
#traceback.print_stack()
|
|
return self.getParent().get_image()
|
|
|
|
def getWidth(self):
|
|
return self.getParent().pixel.PIX_XDIM
|
|
|
|
def getHeight(self):
|
|
return self.getParent().pixel.PIX_YDIM
|
|
|
|
|
|
|
|
def is_locked(filepath):
|
|
"""Checks if a file is locked by opening it in append mode.
|
|
If no exception thrown, then the file is not locked.
|
|
"""
|
|
locked = None
|
|
file_object = None
|
|
filepath = os.path.abspath(filepath)
|
|
if os.path.exists(filepath):
|
|
try:
|
|
print "Trying to open %s." % filepath
|
|
buffer_size = 8
|
|
# Opening file in append mode and read the first 8 characters.
|
|
file_object = open(filepath, 'a', buffer_size)
|
|
if file_object:
|
|
print "%s is not locked." % filepath
|
|
locked = False
|
|
except IOError, message:
|
|
print "File is locked (unable to open in append mode). %s." % \
|
|
message
|
|
locked = True
|
|
finally:
|
|
if file_object:
|
|
file_object.close()
|
|
print "%s closed." % filepath
|
|
else:
|
|
print "%s not found." % filepath
|
|
return locked
|
|
|
|
def wait_for_files(filepaths):
|
|
"""Checks if the files are ready.
|
|
|
|
For a file to be ready it must exist and can be opened in append
|
|
mode.
|
|
"""
|
|
wait_time = 5
|
|
for filepath in filepaths:
|
|
filepath = os.path.abspath(filepath)
|
|
# If the file doesn't exist, wait wait_time seconds and try again
|
|
# until it's found.
|
|
while not os.path.exists(filepath):
|
|
print "%s hasn't arrived. Waiting %s seconds." % \
|
|
(filepath, wait_time)
|
|
time.sleep(wait_time)
|
|
# If the file exists but locked, wait wait_time seconds and check
|
|
# again until it's no longer locked by another process.
|
|
while is_locked(filepath):
|
|
print "%s is currently in use. Waiting %s seconds." % \
|
|
(filepath, wait_time)
|
|
time.sleep(wait_time)
|
|
|
|
|
|
def wait_for_file_size(filepath, size):
|
|
wait_time = 0.01
|
|
filepath = os.path.abspath(filepath)
|
|
# If the file doesn't exist, wait wait_time seconds and try again
|
|
# until it's found.
|
|
while not os.path.exists(filepath) and os.path.getsize(filepath) < size:
|
|
time.sleep(wait_time)
|
|
|
|
|
|
class Image(DeviceBase, Readable):
|
|
def __init__(self, name, pixel):
|
|
DeviceBase.__init__(self, name)
|
|
self.pixel = pixel
|
|
self.roi = [0,0, self.pixel.PIX_XDIM-1, self.pixel.PIX_YDIM-1]
|
|
self.broi = [0,0, self.pixel.PIX_XDIM-1, self.pixel.PIX_YDIM-1]
|
|
self.intensity = ImageIntensity(self)
|
|
self.matrix = ImageMatrix(self)
|
|
self.corrected_intensity = ImageCorrectedIntensity(self)
|
|
self.last_filename = None
|
|
self.last_image = None
|
|
self.image_lock = threading.Lock()
|
|
|
|
|
|
def get_int(self, filename = None):
|
|
if filename is None:
|
|
filename = self.pixel.get_image_filename()
|
|
print "Waiting image file: " + filename,
|
|
file_size = 4 * self.pixel.PIX_XDIM * self.pixel.PIX_YDIM
|
|
print " size= " + file_size
|
|
wait_for_file_size(filename, file_size)
|
|
|
|
|
|
threshold1 = self.pixel.threshold1
|
|
threshold2 = self.pixel.threshold2
|
|
threshold3 = self.pixel.threshold3
|
|
threshold4 = self.pixel.threshold4
|
|
print "Calculating intensity for: " + filename + " - threshold: " + str((threshold1, threshold2, threshold3, threshold4))
|
|
|
|
roi = get_roi()
|
|
bkroi = get_bg_roi()
|
|
|
|
tmp_file = self.pixel.image_root_folder + "tmp/spec_image_info.dat"
|
|
copyfile(filename, tmp_file)
|
|
|
|
try:
|
|
ret = img_get_int(tmp_file, threshold1, threshold2, threshold3, threshold4, \
|
|
self.pixel.image_header_length, self.pixel.PIX_XDIM, self.pixel.PIX_YDIM,self.pixel.PIX_COLOR_DEPTH, \
|
|
roi[0], roi[1], roi[2], roi[3], bkroi[0], bkroi[1], bkroi[2], bkroi[3])
|
|
except:
|
|
log("Error calculating intensity: " + str(filename) + " - " + str(sys.exc_info()[1]), False)
|
|
ret = None
|
|
self.setCache(ret, None)
|
|
return ret
|
|
|
|
def get_image(self, filename = None):
|
|
if filename is None:
|
|
filename = self.pixel.get_image_filename()
|
|
else:
|
|
if not '/' in filename:
|
|
filename = self.pixel.get_full_path() + filename
|
|
filename = str(filename)
|
|
|
|
self.image_lock.acquire(True)
|
|
try:
|
|
if filename == self.last_filename:
|
|
return self.last_image
|
|
print ("Reading image : " + str(filename))
|
|
try:
|
|
ret = img_read(filename, self.pixel.image_header_length, self.pixel.PIX_XDIM, self.pixel.PIX_YDIM,self.pixel.PIX_COLOR_DEPTH)
|
|
except:
|
|
log("Error reading data file: " + str(filename) + " - " + str(sys.exc_info()[1]), False)
|
|
print ("Error reading data file: " + str(filename) + " - " + str(sys.exc_info()[1]))
|
|
self.last_filename = None
|
|
self.last_image = None
|
|
return None
|
|
ret = Convert.reshape(ret, self.pixel.PIX_YDIM, self.pixel.PIX_XDIM)
|
|
self.last_filename = filename
|
|
self.last_image = ret
|
|
return ret
|
|
finally:
|
|
self.image_lock.release()
|
|
def read(self):
|
|
return self.get_int()
|
|
|
|
|
|
|
|
class ReadableImage(ReadableMatrix):
|
|
def read(self):
|
|
ret = []
|
|
for i in range (self.getHeight()):
|
|
ret.append([random.random()] * self.getWidth())
|
|
return to_array(ret, 'd')
|
|
|
|
def getWidth(self):
|
|
return 80
|
|
|
|
def getHeight(self):
|
|
return 40
|
|
|
|
|
|
|
|
|
|
add_device( Image("image", pixel), True)
|
|
|
|
|
|
"""
|
|
def _update_detector_image():
|
|
time.sleep(1.0) #TODO: Must wait thje file to be ready instead
|
|
image.matrix.update()
|
|
|
|
if "_pixelListener" in globals(): pixel.removeListener(_pixelListener)
|
|
class PixelListener (DeviceListener):
|
|
def onValueChanged(self, device, value, former):
|
|
fork(_update_detector_image)
|
|
_pixelListener = PixelListener()
|
|
pixel.addListener(_pixelListener)
|
|
|
|
|
|
add_device(RegisterMatrixSource("detector_image", image.matrix), True)
|
|
detector_image.monitored = True
|
|
image.matrix.update()
|
|
"""
|
|
|
|
|
|
add_device(RegisterMatrixSource("detector_image", image.matrix), True)
|
|
detector_image.monitored = True
|