def classify_panda(model, image_shape): rgb_panda_image = imread(panda_path) if show_image: bgr_panda_image = cv2.cvtColor(rgb_panda_image, cv2.COLOR_RGB2BGR) cv2.imshow('panda_image', bgr_panda_image) # preprocess input cropped_rgb_panda_image = center_crop_and_resize(rgb_panda_image, image_size=image_shape) if show_image: cropped_bgr_panda_image = cv2.cvtColor( cropped_rgb_panda_image.astype('uint8'), cv2.COLOR_RGB2BGR) cv2.imshow('cropped_panda_image', cropped_bgr_panda_image) preprocessed_rgb_panda_image = preprocess_input(cropped_rgb_panda_image) batch = np.expand_dims(preprocessed_rgb_panda_image, 0) # make prediction and decode predictions = model.predict(batch) xprint(decode_predictions(predictions)) if show_image: while cv2_window_open('panda_image') and cv2_window_open( 'cropped_panda_image'): if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
def get_frame_batches(video, image_shape, desired_batch_size): video_w = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) video_h = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_rate = video.get(cv2.CAP_PROP_FPS) if ( video_w, video_h ) not in expected_video_resolutions or frame_rate not in expected_frame_rates: xprint("Resolution: {}x{}".format(video_w, video_h)) xprint("Frame rate:", frame_rate) raise Exception("Unexpected video property detected") frame_idx = 0 batch = [] while True: ret, bgr_frame = video.read() if not ret: break else: if show_image and frame_idx % frame_display_stride == 0: image_scale = 0.5 new_width, new_height = bgr_frame.shape[ 1] * image_scale, bgr_frame.shape[0] * image_scale resized_bgr_frame = cv2.resize( bgr_frame, (int(new_width), int(new_height))) cv2.imshow('frame', resized_bgr_frame) cropped_bgr_frame = center_crop_and_resize( bgr_frame, image_size=image_shape, crop_padding=crop_padding) if show_image: cv2.imshow('cropped_frame', cropped_bgr_frame.astype('uint8')) cropped_rgb_frame = cv2.cvtColor(cropped_bgr_frame.astype('uint8'), cv2.COLOR_BGR2RGB) batch.append(preprocess_input(cropped_rgb_frame)) if show_image: if cv2.waitKey(1) & 0xFF == ord('q'): raise Exception("q key pressed during video processing") if not (cv2_window_open('frame') and cv2_window_open('cropped_frame')): raise Exception("Window closed during video processing") if len(batch) == desired_batch_size: yield np.asarray(batch) batch = [] frame_idx += 1 if len(batch) > 0: yield np.asarray(batch) cv2.destroyAllWindows()
def test_webcams(model, image_shape): for cam_idx in itertools.count(): cap = cv2.VideoCapture(cam_idx + cam_api_reference) if not cap.isOpened(): break ret, bgr_frame = cap.read() xprint("Cam {} image shape: {}".format(cam_idx, bgr_frame.shape)) while True: ret, bgr_frame = cap.read() cv2.imshow('frame', bgr_frame) # Use neural network cam_batch_size = 1 verbose = False # print("model.input_shape:", model.input_shape) # input_size = model.input_shape[2] cropped_bgr_frame = center_crop_and_resize(bgr_frame, image_size=image_shape) cv2.imshow('cropped_frame', cropped_bgr_frame.astype('uint8')) xprint("cropped_bgr_frame.shape:", cropped_bgr_frame.shape) cropped_rgb_frame = cv2.cvtColor(cropped_bgr_frame.astype('uint8'), cv2.COLOR_BGR2RGB) batch = preprocess_input( cropped_rgb_frame.reshape((-1, ) + cropped_rgb_frame.shape)) prediction = model.predict(batch, batch_size=cam_batch_size, verbose=verbose) if use_original_classifier: for decoded_frame_prediction in decode_predictions(prediction): xprint(decoded_frame_prediction) else: if not isinstance(prediction, list): prediction = [prediction] print() for output_head in prediction: for frame_output in output_head: print(frame_output.shape, frame_output) if cv2.waitKey(1) & 0xFF == ord('q'): break if not (cv2_window_open('frame') and cv2_window_open('cropped_frame')): break cap.release() cv2.destroyAllWindows()
def save_examples(ds_info, ds, num_examples=10, folder=".", image_key=None): """Save images from an image classification dataset. Only works with datasets that have 1 image feature and optionally 1 label feature (both inferred from `ds_info`). Note the dataset should be unbatched. Usage: ```python ds, ds_info = tfds.load('cifar10', split='train', with_info=True) fig = save_examples(ds_info, ds) ``` Args: ds_info: The dataset info object to which extract the label and features info. Available either through `tfds.load('mnist', with_info=True)` or `tfds.builder('mnist').info` ds: `tf.data.Dataset`. The tf.data.Dataset object to visualize. Examples should not be batched. num_examples: `int`. Number of examples to save folder: `str`. Where to save images image_key: `string`, name of the feature that contains the image. If not set, the system will try to auto-detect it. Returns: """ if not image_key: # Infer the image and label keys image_keys = [ k for k, feature in ds_info.features.items() if isinstance(feature, features_lib.Image) ] if not image_keys: raise ValueError( "Visualisation not supported for dataset `{}`. Was not able to " "auto-infer image.".format(ds_info.name)) if len(image_keys) > 1: raise ValueError( "Multiple image features detected in the dataset. Using the first one. You can " "use `image_key` argument to override. Images detected: %s" % (",".join(image_keys))) image_key = image_keys[0] label_keys = [ k for k, feature in ds_info.features.items() if isinstance(feature, features_lib.ClassLabel) ] label_key = label_keys[0] if len(label_keys) == 1 else None if not label_key: logging.info("Was not able to auto-infer label.") examples = list(dataset_utils.as_numpy(ds.take(num_examples))) for i, ex in enumerate(examples): if not isinstance(ex, dict): raise ValueError( "tfds.show_examples requires examples as `dict`, with the same " "structure as `ds_info.features`. It is currently not compatible " "with `as_supervised=True`. Received: {}".format(type(ex))) # Plot the image image = ex[image_key] if len(image.shape) != 3: raise ValueError( "Image dimension should be 3. tfds.show_examples does not support " "batched examples or video.") _, _, c = image.shape if c == 1: image = image.reshape(image.shape[:2]) image = center_crop_and_resize(image, 224).astype(np.uint8) im = Image.fromarray(image) if label_key: label = ex[label_key] label_str = ds_info.features[label_key].int2str(label).replace( "/", "_") else: label_str = "" im.save(f"{folder}/image_{label_str}_{i}.jpeg")
import os import numpy as np import efficientnet.tfkeras as efn from tensorflow.keras.applications.imagenet_utils import decode_predictions, preprocess_input from efficientnet.preprocessing import center_crop_and_resize from skimage.io import imread model = efn.EfficientNetB0(weights='imagenet') with open('./label.txt', 'r') as f: text_labels = [''.join(l.split("'")[1]) for l in f] image_list = os.listdir('./samples') for image_name in image_list: image_path = os.path.join('./samples', image_name) print(image_path) image = imread(image_path) image_size = model.input_shape[1] x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x, mode='torch') inputs = np.expand_dims(x, 0) expected = model.predict(inputs) result = decode_predictions(expected, top=1) print('With prob = %.2f, it contains %s' % ( result[0][0][2] * 100, result[0][0][1]))