SQLServerCentral Article

How to process images and analyze charts with AI

,

In this article, we will see how to handle images with AI. Sometimes we need to import the text from images into SQL Server. In this article, we will use ChatGPT to convert text from images into text.

If you want to know more about ChatGPT, you can read my previous article: ChatGPT in SQL Server - Practical examples

Getting started

First, go to ChatGPT. If you do not have an account, create one.

I will ask ChatGPT if it can OCR some images.

Handle images with ChatGPT

Let’s try to read the text of the images with ChatGPT. You can drag and drop your image or use the Upload files icon.

Me: Please read the text of this image.

Read signs

ChatGPT: The text in the image reads: "ROAD CLOSED."

Insert the text from the image into a table

Daniel: I have the following table:

CREATE TABLE image (

id INT,

description VARCHAR(8000)

);

Can you create the code to insert the previous picture's text into the table?

ChatGPT:

To insert the text "ROAD CLOSED" from the image into the image table, you can use the following SQL query:

INSERT INTO image (id, description) 

VALUES (1, 'ROAD CLOSED');

This query assumes you're inserting the first record (id = 1) into the table. Let me know if you need any adjustments!

How does the OCR handle images with Bard?

Me: How do you read the text from images?

ChatGPT:

I use Optical Character Recognition (OCR) technology to read and extract text from images. OCR scans the image for patterns that resemble letters, numbers, or symbols and converts them into readable text. When you upload an image with text, I can analyze it and provide you with the content of the image by recognizing these patterns.

If you'd like, I can also help you extract text from other images you might have. Just let me know!

Test how Bard handles images of a milk box

We will try to read the text on a box of milk.

Handle images with AI - reading a milk box Milk box

Me: Can you do OCR to the text of this image?

ChatGPT:

The text from the image is:

  • "REAL MILK great taste"
  • "HORIZON ORGANIC"
  • "ORGANIC WHOLE MILK"
  • "VITAMIN D • UHT"
  • "8 FL OZ (236 mL)"
  • "USDA ORGANIC"

This is the text displayed on the milk carton. Let me know if you'd like to extract more information or need anything else!

How to create a SQL Server table to insert images from ChatGPT

Previously, we showed how to convert the text in images into text. Now, we need a table in SQL Server to store the data. I am assuming that you have SQL Server installed.

First, create the table:

Me: Generate the T-SQL code to create a table named comments with the columns id of type int with autonumeric columns (numbers generated automatically with the identity property) and the column description of type varchar(max)

ChatGPT:

Here is the T-SQL code to create the comments table with the specified columns:

sql
CREATE TABLE comments (

    id INT IDENTITY(1,1) PRIMARY KEY,

    description VARCHAR(MAX)

);

  • The id column is of type INT and is set to automatically generate numbers starting from 1, incrementing by 1, thanks to the IDENTITY(1,1) property.
  • The description column is of type VARCHAR(MAX) to allow variable-length strings of any size.

Handle handwritten images in SQL Server with AI

Me: Can you do OCR to the text of this image and generate the T-SQL code to insert the text into the comments table previously created?

ChatGPT:

The text extracted from the image is:

  • "We Start With Good"
  • "Because all businesses should be doing something good."

Here is the T-SQL code to insert this text into the comments table:

INSERT INTO comments (description)

VALUES ('We Start With Good Because all businesses should be doing something good.');

This query inserts the text as a single row into the comments table, with the id column automatically generated.

Me: I am impressed. The OCR could read handwritten text easily.

Insert Data from a chart into a table

Me: I have the following chart.

Please insert the coordinates x and y in the following table based on the image attached:

CREATE TABLE Coordinates (

id INT IDENTITY(1,1) PRIMARY KEY,

x INT,

y INT

);

ChatGPT:

Based on the scatter plot, the coordinates (x, y) extracted are:

  • (4, 2)

  • (5, 3)

  • (6, 4)

  • (7, 5)

  • (8, 8)

You can use these coordinates to populate the table. Here's the SQL statement to insert the values into your table:

INSERT INTO Coordinates (x, y) VALUES

(4, 2),

(5, 3),

(6, 4),

(7, 5),

(8, 8);

Me: Unfortunately, ChatGPT has some limitations in extracting values from an X, Y chart. It could not do it.  This may change in the future.

Extracting values from images into T-SQL

Me: I have the following picture of the table:

Please create the T-SQL code to create the table with the data named dbo.salesreason.

ChatGPT:

To create the table dbo.salesreason based on the data provided in the image, you can use the following T-SQL code:

CREATE TABLE dbo.salesreason (

    SalesReasonID INT PRIMARY KEY,

    Name VARCHAR(255),

    ReasonType VARCHAR(255),

    ModifiedDate DATETIME

);



INSERT INTO dbo.salesreason (SalesReasonID, Name, ReasonType, ModifiedDate)

VALUES

(1, 'Price', 'Other', '2008-04-30 00:00:00.000'),

(2, 'On Promotion', 'Promotion', '2008-04-30 00:00:00.000'),

(3, 'Magazine Advertisement', 'Marketing', '2008-04-30 00:00:00.000'),

(4, 'Television Advertisement', 'Marketing', '2008-04-30 00:00:00.000'),

(5, 'Manufacturer', 'Other', '2008-04-30 00:00:00.000'),

(6, 'Review', 'Other', '2008-04-30 00:00:00.000'),

(7, 'Demo Event', 'Marketing', '2008-04-30 00:00:00.000'),

(8, 'Sponsorship', 'Marketing', '2008-04-30 00:00:00.000'),

(9, 'Quality', 'Other', '2008-04-30 00:00:00.000'),

(10, 'Other', 'Other', '2008-04-30 00:00:00.000');

 

Handle chart images with AI

Now, I will try to get information from the charts

Me: Can you explain to me what this chart means?

ChatGPT:

This image displays a chart for the cryptocurrency Bitcoin (BTC) from CoinMarketCap. Here's what each element means:

  1. Price: The current price of Bitcoin is $84,075.27. The chart shows the price movement over the last year, and the price is currently increasing.

  2. Change (1 Year): The price has risen 17.61% over the past year, which is indicated right next to the current price.

  3. Market Cap: The market capitalization of Bitcoin is $1.66 trillion, which has increased by 3.79% over the last 24 hours. Market cap is calculated by multiplying the current price of Bitcoin by the circulating supply.

  4. Volume (24h): The 24-hour trading volume is $29.46 billion, which has decreased by 5.57% in the last 24 hours. This refers to the total value of Bitcoin traded during the last 24 hours.

  5. FDV (Fully Diluted Valuation): $1.76 trillion is the market cap if all the Bitcoin tokens (the full supply of 21 million BTC) were in circulation. It gives an idea of the potential market cap when all Bitcoin are mined.

  6. Volume/Market Cap (24h): The ratio is 1.77%, showing how much the volume traded in the last 24 hours compares to the market cap of Bitcoin. A lower ratio suggests lower trading volume relative to the overall market cap.

  7. Total Supply: The total supply of Bitcoin is 19.83 million BTC, which is the total number of Bitcoin that has been mined so far.

  8. Max Supply: Bitcoin's maximum supply is capped at 21 million BTC. This means there will never be more than 21 million Bitcoin in existence.

  9. Circulating Supply: The circulating supply is 19.83 million BTC, which is the total amount of Bitcoin that is currently available in the market.

  10. Chart: The graph shows Bitcoin's price over the last year (with a 1Y time span selected). The chart displays price movements with a steady increase, particularly in the last few months, but there are fluctuations. The green area on the chart represents price increases, and the red area shows declines.

In summary, this chart tracks Bitcoin's performance over the last year, showing growth in both price and market cap, along with a steady increase in trading volume. It also shows Bitcoin's price volatility and where it stands relative to its maximum supply.

Provide an analysis of the chart with AI

Me: Based on the chart, do you think the price will increase or decrease this year?

ChatGPT:

Based on the chart you've provided, Bitcoin's price has been on a general upward trend over the last year, with some fluctuations along the way. There has been a noticeable rise, particularly in the recent months, and the chart shows a strong upward trajectory, especially after the dip around mid-2024.

However, predicting the future direction of Bitcoin's price is difficult because the cryptocurrency market is highly volatile, influenced by various factors like market sentiment, global events, regulatory changes, and technological advancements.

Some key observations from the chart:

  • Current growth: The price is still relatively high compared to earlier periods, which might suggest continued bullish sentiment.
  • Volatility: The chart also shows significant fluctuations, suggesting that the price could experience ups and downs, even within an overall increasing trend.
  • Market sentiment: If the current momentum continues, with positive news around adoption or institutional investments, the price could continue to rise. However, any regulatory changes or adverse news could cause the price to dip.

Given this, while the trend seems positive, there is a significant level of uncertainty. If you are considering investing or making decisions based on Bitcoin's price, it might be wise to proceed cautiously and stay updated on market news.

Would you like more insights or help with any specific analysis regarding Bitcoin's price?

Store the data from the chart in a table

Me: Please store the price, marketcap, Volume, FDV, and Total supply from the previous chart into this table:

CREATE TABLE TradingData (

    Id INT IDENTITY(1,1) PRIMARY KEY,       

    Price DECIMAL(18, 8) NOT NULL,            

    MarketCap DECIMAL(18, 2) NOT NULL,     

    Volume DECIMAL(18, 2) NOT NULL,           

    FDV DECIMAL(18, 2) NOT NULL,              

    TotalSupply DECIMAL(18, 2) NOT NULL,      

    Timestamp DATETIME NOT NULL DEFAULT GETDATE() 

);

ChatGPT:

Here is the T-SQL code to insert the data into the TradingData table:

INSERT INTO TradingData (Price, MarketCap, Volume, FDV, TotalSupply)

VALUES 

(84075.27, 1660000000000, 29460000000, 1760000000000, 19830000);

This code will insert the values for Price, MarketCap, Volume, FDV, and TotalSupply into the TradingData table.

Let me know if you need any more adjustments!

Conclusion about the handled images in the AI article

Now the AI is providing not only OCR features to import text from images into SQL Server. Also, the AI can understand charts and do data analysis. AI is simplifying our job of doing data analysis, and soon, we will only need to ask the AI to do our job, and we will only need to organize the information.

 

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating