VaranAwesomenow
Mega Sage

Unlocking AI Potential: Predicting Images with ServiceNow and TensorFlow

In today’s fast-paced digital landscape, integrating artificial intelligence (AI) into your workflows can significantly enhance efficiency and innovation. One exciting application is using AI to analyze and predict the contents of images directly within ServiceNow. In this blog post, we’ll walk you through a practical example of how to read an image from ServiceNow and predict its contents using a TensorFlow model.

Why Integrate AI with ServiceNow?

ServiceNow is a powerful platform for IT service management, and adding AI capabilities can take it to the next level. By leveraging AI, you can automate repetitive tasks, improve decision-making, and provide more accurate and timely responses to user queries. TensorFlow, an open-source machine learning framework, is perfect for building and deploying AI models that can handle complex tasks like image recognition.

Step-by-Step Guide

1. Setting Up Your Environment

Upload required images to ServiceNow as an entry in db_image table.

 

2. Reading an Image from ServiceNow

First, you’ll need to fetch the image from your ServiceNow instance.

Following python code can be used to read the image

import requests

url = 'https://dev263135.service-now.com/x_146833_genaico_0.CatImage4.jpg'
local_path = '/opt/app-root/src/cat-doc-predict/test/cats/aniltest.jpg'

response = requests.get(url)
if response.status_code == 200:
    with open(local_path, 'wb') as file:
        file.write(response.content)
    print('File downloaded successfully')
else:
    print('Failed to download file')

3. Loading and Preprocessing the Image

Once you have the image, the next step is to load and preprocess it for prediction. TensorFlow provides utilities to handle this efficiently:

Sample image : x_146833_genaico_0.DogDrawing.jpg (176×176) (service-now.com)
# Use the pre-trained model

! pip install opencv-python
import cv2
import tensorflow as tf
from tensorflow import keras
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
#test_img = cv2.imread('openshift-ai/2_Cat-dog-prediction/cat.jpg')
test_img = cv2.imread(local_path)



model = keras.models.load_model("openshift-ai/2_Cat-dog-prediction/keras_model/my_dog_cat_model.keras")
plt.imshow(test_img)

test_img.shape

test_img = cv2.resize(test_img,(256,256))
test_input = test_img.reshape((1,256,256,3))

modeloutput = model.predict(test_input)
print(modeloutput)
sample output
Requirement already satisfied: opencv-python in /opt/app-root/lib/python3.9/site-packages (4.10.0.84)
Requirement already satisfied: numpy>=1.17.0 in /opt/app-root/lib/python3.9/site-packages (from opencv-python) (1.26.4)

[notice] A new release of pip available: 22.2.2 -> 24.2
[notice] To update, run: pip install --upgrade pip
1/1 [==============================] - 0s 112ms/step
[[0.]]
VaranAwesomenow_0-1725635847735.png

Since this is a dog it predicted with result as 0. If its a cat prediction will be >0

One more example with a cat, sample image : x_146833_genaico_0.CatImage3.jpg (431×359) (service-now.com)

output : 

 

Conclusion

Integrating AI into your ServiceNow workflows can unlock new levels of efficiency and innovation. By following this example, you can harness the power of TensorFlow to predict the contents of images directly within ServiceNow. This is just the beginning—imagine the possibilities as you continue to explore and implement AI solutions in your organization.

Feel free to reach out if you have any questions or need further assistance. Happy coding!

Version history
Last update:
‎09-06-2024 08:24 AM
Updated by:
Contributors