
Rather than discussing AI related topics, this week I wanted to try something out, building a simple AI tool that can generate captions for images. I was able to complete this in under 45-minutes, and I’m going to walk you through the process, step-by-step.
Step 1: Setting up the Folder in Terminal
First things first, I opened Terminal on my Mac (you can find it in Applications > Utilities). Then, I ran these commands to create a folder and go inside it:
- mkdir ai-image-captioning
- cd ai-image-captioning
This creates an empty folder to store everything for the AI model.
Step 2: Setting up VS Code
I opened up VS Code directly from the Terminal by typing:
- code .
This opens up VS Code in the current folder, ai-image-captioning. If you don’t have VS Code already downloaded, you can do that on the website.
Step 3: Downloading AI Model and Script
I wasn’t sure where the script was, so I had to find a way to call the caption generator script for the AI. So, I found a GitHub repository that had the necessary script and cloned it to my folder by doing:
Once that was completed, the script for caption_generator.py was ready
Step 4: Installing the Python Libraries That Are Needed
The caption_generator.py script needs a few Python libraries to work, so I installed them using this code (your code will not run unless you download these!):
- pip3 install torch torchvision matplotlib pillow requests
These libraries help with everything from image handling to deep learning.
Step 5: Running the Caption Generator
And with that you’re done with the coding aspect. All you need to do is execute the code using:
- python3 caption_generator.py
After doing so, it prompted me to enter an image URL, so I found a picture of a dog online and copied the URL.
Step 6: AI Generates the Caption
Once I hit Enter, the AI processed the image and generated this caption:
- “Generated Caption: a dog sitting in the grass”
Now you’re done! You have successfully completed making an AI caption generator.
But, how does this work?
Make sure to read my next post to find out all about this. 🙂
Resource:
Wang, Jim. “What Is Image Captioning and How to Use Python to Generate Caption from an Image?” Medium, Medium, 14 Sept. 2024, medium.com/@jimwang3589/what-is-image-captioning-and-how-to-use-python-to-generate-caption-from-an-image-98a9eb6be06d.


