{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "\n", "# Load the .h5 model\n", "model = tf.keras.models.load_model('modelo_entrenado.h5')\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "imagen = tf.keras.preprocessing.image.load_img('imprueba.jpg')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "def split_image(uploaded_file): \n", " \n", " # Convert the image to a numpy array\n", " img_array = tf.keras.preprocessing.image.img_to_array(uploaded_file)\n", " print(img_array.shape)\n", " # Split the image into four equal parts\n", " height, width, _ = img_array.shape\n", " split_width = width // 2\n", " split_height = height // 2\n", " top_left = img_array[0:split_height, 0:split_width, :]\n", " top_right = img_array[0:split_height, split_width:width, :]\n", " bottom_left = img_array[split_height:height, 0:split_width, :]\n", " bottom_right = img_array[split_height:height, split_width:width, :]\n", " \n", " return top_left, top_right, bottom_left, bottom_right" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "# Create a function that accepts an image file and returns the classification predictions\n", "def classify_image(image_file):\n", "\n", " img_resized = tf.image.resize(image_file, (255, 255))\n", " \n", " x = np.expand_dims(img_resized, axis=0)\n", " \n", "\n", " print(x.shape)\n", "\n", " prediction = model.predict(x)\n", " predicted_class = np.argmax(prediction)\n", " return predicted_class" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1200, 1600, 3)\n" ] } ], "source": [ "lista_images = split_image(imagen)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 255, 255, 3)\n" ] } ], "source": [ "label = classify_image(lista_images[0])" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "label" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ml", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }