Files
x04sa-es3/script/device/Image.py
2019-07-04 13:32:01 +02:00

182 lines
6.4 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):
global AUTO_THRESH1_COUNT, AUTO_THRESH2_COUNT, AUTO_THRESH3_COUNT, AUTO_THRESH4_COUNT
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
AUTO_THRESH1_COUNT=thresh1_count
AUTO_THRESH2_COUNT=thresh2_count
AUTO_THRESH3_COUNT=thresh3_count
AUTO_THRESH4_COUNT=thresh4_count
AUTO_THRESH1 = self.pixel.threshold1
AUTO_THRESH2 = self.pixel.threshold2
AUTO_THRESH3 = self.pixel.threshold3
AUTO_THRESH4 = self.pixel.threshold4
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
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()
file_size = 4 * self.pixel.PIX_XDIM * self.pixel.PIX_YDIM
print "Waiting image file: " , filename, " size=", file_size
wait_for_file_size(filename, file_size, 20.0)
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