def _step(self): # grab image container from port using traits optical_c = self.grab_input_using_trait('optical_image') thermal_c = self.grab_input_using_trait('thermal_image') # Get python image from conatiner (just for show) optical_npy = optical_c.image().asarray().astype('uint8') thermal_npy = thermal_c.image().asarray().astype('uint16') thermal_norm = normalize_thermal(thermal_npy) if thermal_norm is not None and optical_npy is not None: # compute transform ret, transform, _ = compute_transform( optical_npy, thermal_norm, warp_mode=cv2.MOTION_HOMOGRAPHY, match_low_res=True, good_match_percent=self._good_match_percent, ratio_test=self._ratio_test, match_height=self._match_height, min_matches=self._min_matches, min_inliers=self._min_inliers) else: ret = False if ret: # TODO: Make all of these computations conditional on port connection inv_transform = np.linalg.inv(transform) thermal_warped = cv2.warpPerspective( thermal_npy, transform, \ ( optical_npy.shape[1], optical_npy.shape[0] ) ) optical_warped = cv2.warpPerspective( optical_npy, inv_transform, \ ( thermal_npy.shape[1], thermal_npy.shape[0] ) ) #self.push_to_port_using_trait( 'thermal_to_optical_homog', # Homography.from_matrix( transform, 'd' ) #self.push_to_port_using_trait( 'optical_to_thermal_homog', # Homography.from_matrix( inv_transform, 'd' ) self.push_to_port_using_trait( 'warped_thermal_image', ImageContainer(from_pil(pil_image.fromarray(thermal_warped)))) self.push_to_port_using_trait( 'warped_optical_image', ImageContainer(from_pil(pil_image.fromarray(optical_warped)))) else: print('alignment failed!') #self.push_to_port_using_trait( "thermal_to_optical_homog", Homography() ) #self.push_to_port_using_trait( "optical_to_thermal_homog", Homography() ) self.push_to_port_using_trait('warped_optical_image', ImageContainer()) self.push_to_port_using_trait('warped_thermal_image', ImageContainer()) self._base_step()
def _step(self): # grab image container from port using traits input_c = self.grab_input_using_trait('image') # Get python image from conatiner and perform operation input_npy = input_c.image().asarray() input_8bit = input_npy.astype('uint8') input_fft = np.fft.fft2(input_8bit) filt = input_fft - self._noise_fft im_filt = np.absolute(np.fft.ifft2(filt)) im_filt = np.log( cv2.blur(im_filt, (self._response_kernel, self._response_kernel))) im_filt = (im_filt - im_filt.min()) / (im_filt.max() - im_filt.min()) smoothed_8bit = cv2.blur(input_8bit, (self._smooth_kernel, self._smooth_kernel)) output_image = input_8bit * im_filt + smoothed_8bit * (1.0 - im_filt) self.push_to_port_using_trait( 'image', ImageContainer( \ from_pil( pil_image.fromarray( output_image.astype( 'uint8' ) ) ) ) ) self._base_step()
def _step(self): print "[DEBUG] ----- start step" # grab image container from port using traits in_img_c = self.grab_input_using_trait('image') # Get image from conatiner in_img = in_img_c.get_image() # convert generic image to PIL image pil_image = in_img.get_pil_image() # draw on the image to prove we can do it num = 37 import ImageDraw draw = ImageDraw.Draw(pil_image) draw.line((0, 0) + pil_image.size, fill=128, width=5) draw.line((0, pil_image.size[1], pil_image.size[0], 0), fill=32768, width=5) # x0 y0 x1 y1 draw.rectangle([num, num, num + 100, num + 100], outline=125) del draw new_image = Image.from_pil(pil_image) # get new image handle new_ic = ImageContainer(new_image) # push object to output port self.push_to_port_using_trait('out_image', new_ic) self._base_step()
def test_detect(self): modules.load_known_modules() detector = ImageObjectDetector.create("example_detector") image = Image() image_container = ImageContainer(image) detections = detector.detect(image_container) nose.tools.ok_(detections is not None, "Unexpected empty detections") nose.tools.assert_equal(len(detections), 1)
def _step(self): print("[DEBUG] ----- start step") # grab image container from port using traits in_img_c = self.grab_input_using_trait("image") imageHeight = in_img_c.height() imageWidth = in_img_c.width() if (self.normImageType): print("Normalize image") in_img = in_img_c.image().asarray().astype("uint16") bottom, top = self.get_scaling_values(self.normImageType, imageHeight) in_img = self.lin_normalize_image(in_img, bottom, top) in_img = np.tile(in_img, (1, 1, 3)) else: in_img = np.array(get_pil_image(in_img_c.image()).convert("RGB")) self.push_to_port_using_trait("image_norm", ImageContainer(Image(in_img))) startTime = time.time() boxes, scores, classes = self.generate_detection( self.detection_graph, in_img) elapsed = time.time() - startTime print("Done running detector in {}".format( humanfriendly.format_timespan(elapsed))) goodBoxes = [] detections = DetectedObjectSet() for i in range(0, len(scores)): if (scores[i] >= self.confidenceThresh): bbox = boxes[i] goodBoxes.append(bbox) topRel = bbox[0] leftRel = bbox[1] bottomRel = bbox[2] rightRel = bbox[3] xmin = leftRel * imageWidth ymin = topRel * imageHeight xmax = rightRel * imageWidth ymax = bottomRel * imageHeight obj = DetectedObject(BoundingBox(xmin, ymin, xmax, ymax), scores[i]) detections.add(obj) print("Detected {}".format(len(goodBoxes))) self.push_to_port_using_trait("detected_object_set", detections) self._base_step()
def add_data_from_disk(self, in_img): img = in_img.image().asarray().astype("uint16") mi = np.percentile(img, 1) ma = np.percentile(img, 100) normalized = (img - mi) / (ma - mi) normalized = normalized * 255 normalized[normalized < 0] = 0 output = ImageContainer(Image(normalized.astype("uint8"))) return
def _test_numpy(dtype_name, nchannels, order='c'): np_img = create_numpy_image(dtype_name, nchannels, order) img_container = ImageContainer(Image(np_img)) recast = img_container.asarray() # asarray always returns 3 channels np_img = np.atleast_3d(np_img) vital_img = img_container.image() pixel_type_name = vital_img.pixel_type_name() pixel_type_name = vital_img.pixel_type_name() want = map_dtype_name_to_pixel_type(dtype_name) assert pixel_type_name == want, 'want={} but got={}'.format( want, pixel_type_name) if not np.all(np_img == recast): raise AssertionError( 'Failed dtype={}, nchannels={}, order={}'.format( dtype_name, nchannels, order))
def _step(self): # Get c image pointer from incoming pipeline in_img_c = self.grab_input_using_trait('image') # Get python image from conatiner (not used here, just for show) in_img = in_img_c.image() # Print out text to screen, just so we're doing something here print("Text: " + str(self.text)) # push dummy detections object to output port self.push_to_port_using_trait('image', ImageContainer(in_img)) self._base_step()
def _step(self): # grab image container from port using traits in_img_c = self.grab_input_using_trait('image') # Get python image from conatiner (just for show) in_img = in_img_c.get_image() # Print out text to screen print "Text: " + str( self.text ) # push dummy detections object to output port self.push_to_port_using_trait('out_image', ImageContainer(in_img)) self._base_step()
def _step(self): print("[DEBUG] ----- start step") # grab image container from port using traits in_img_c = self.grab_input_using_trait('image') # Get python image from conatiner (just for show) in_img = in_img_c.get_image() # Print out text to screen print("Text: " + str(self.text)) # push dummy image object (same as input) to output port self.push_to_port_using_trait('out_image', ImageContainer(in_img)) self._base_step()
def _convert_image_container_in(datum_ptr): """ Convert datum as PyCapsule to image_container opaque handle. """ _VCL = _find_converter_lib() # Convert from datum to opaque handle. func = _VCL.vital_image_container_from_datum func.argtypes = [ ctypes.py_object ] func.restype = ImageContainer.C_TYPE_PTR # get opaque handle from the datum handle = func(datum_ptr) # convert handle to python object - from c-ptr py_ic_obj = ImageContainer( None, handle ) return py_ic_obj
def _step( self ): # grab image container from port using traits in_img_c = self.grab_input_using_trait( 'image' ) tracks = self.grab_input_using_trait( 'object_track_set' ) # Get python image from conatiner (just for show) in_img = get_pil_image( in_img_c.image() ).convert( 'RGB' ) if len( tracks.tracks() ) == 0: # Fill image in_img = pil_image.new( mode='RGB', size=in_img.size, color = ( randint( 0, 255 ), randint( 0, 255 ), randint( 0, 255 ) ) ) # push dummy image object (same as input) to output port self.push_to_port_using_trait( 'image', ImageContainer( from_pil( in_img ) ) ) self._base_step()
def convert(self, image_container): """ :param image_container: The image container with image data to convert :type image_container: ImageContainer :return: A new ImageContainer with the converted underlying data :rtype: ImageContainer """ ci_convert = self.VITAL_LIB.vital_algorithm_convert_image_convert ci_convert.argtypes = [ self.C_TYPE_PTR, ImageContainer.C_TYPE_PTR, VitalErrorHandle.C_TYPE_PTR ] ci_convert.restype = ImageContainer.C_TYPE_PTR with VitalErrorHandle() as eh: return ImageContainer( from_cptr=ci_convert(self, image_container, eh))
def _step(self): # grab image container from port using traits img_c = self.grab_input_using_trait('image') img = img_c.image().asarray().astype("uint16") mi = np.percentile(img, 1) ma = np.percentile(img, 100) normalized = (img - mi) / (ma - mi) normalized = normalized * 255 normalized[normalized < 0] = 0 output = ImageContainer(Image(normalized.astype("uint8"))) # push dummy image object (same as input) to output port self.push_to_port_using_trait('image', output) self._base_step()
def load(self, filepath): """ Load an image into a ImageContainer :param filepath: Path to the image to load :type filepath: str :return: New ImageContainer containing the loaded image :rtype: ImageContainer """ iio_load = self.VITAL_LIB.vital_algorithm_image_io_load iio_load.argtypes = [ self.C_TYPE_PTR, ctypes.c_char_p, VitalErrorHandle.C_TYPE_PTR ] iio_load.restype = ImageContainer.C_TYPE_PTR with VitalErrorHandle() as eh: ic_ptr = iio_load(self, filepath, eh) return ImageContainer(from_cptr=ic_ptr)
def test_size(self): i = Image(720, 480) ic = ImageContainer(i) nose.tools.assert_equal(ic.size(), 720 * 480)
def test_height(self): i = Image(720, 480) ic = ImageContainer(i) nose.tools.assert_equal(ic.height(), 480)
def test_width(self): i = Image(720, 480) ic = ImageContainer(i) nose.tools.assert_equal(ic.width(), 720)
def test_new(self): image = Image() img_c = ImageContainer(image) image = Image(100, 100) img_c = ImageContainer(image)