Computer Vision
Computer vision is the part of AI that lets computers understand pictures and videos.
Images Are Just Numbers
To a computer, a picture is a grid of tiny dots called pixels. Each pixel is stored as numbers. In a black-and-white (greyscale) image, each pixel is a number from 0 (black) to 255 (white). A colour image stores three numbers per pixel: red, green and blue.
Here is a tiny 3 x 3 greyscale "image" in Python:
Example
image = [
[0, 255, 0],
[255, 255, 255],
[0, 255, 0],
]
for row in image:
print("".join("#" if p > 128 else "." for p in row)).#. ### .#.
The numbers form a plus sign. A real phone photo has millions of pixels, so it holds millions of numbers.
Main Computer Vision Tasks
| Task | Question it answers | Example |
|---|---|---|
| Image classification | What is in this picture? | "This is a mango" |
| Object detection | What objects are here, and where? | Boxes around each car on a road |
| Segmentation | Exactly which pixels belong to each object? | Separating a person from the background in a video call |
| Face recognition | Whose face is this? | Phone face unlock |
| OCR (text reading) | What text is written here? | Reading a CNIC or a printed bill |
How a Model Learns to See
Modern vision models are usually convolutional neural networks (CNNs) or vision transformers. They are trained on many labelled images. Early layers learn edges and colours; later layers learn shapes and full objects.
Here is a small, real example that trains a model to recognise handwritten digits using a dataset that comes with scikit-learn:
Example
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
digits = load_digits() # 1797 small 8x8 images of digits 0-9
X_train, X_test, y_train, y_test = train_test_split(
digits.data, digits.target, test_size=0.25, random_state=0)
model = SVC()
model.fit(X_train, y_train)
print("Accuracy:", round(model.score(X_test, y_test), 2))Accuracy: 0.99
The model correctly reads about 99% of digit images it has never seen. Reading postal codes and cheque amounts started with exactly this kind of task.
Accuracy on test data matters more than accuracy on training data. A model that only memorises its training images will fail on new photos.
Computer Vision Around You
- Phones: face unlock, portrait mode blur, scanning documents into PDFs.
- Roads: cameras that read number plates and issue e-challans for traffic violations.
- Shops and warehouses: counting stock on shelves.
- Health: helping doctors spot problems in X-rays and eye scans.
- Farming: apps that look at a photo of a leaf and suggest which disease the crop may have.
- Chatbots: many AI assistants can now look at a photo you upload and describe or explain it.
Limits and Risks
- Bad lighting, blur or unusual angles can confuse models.
- If training photos mostly show one group of people, face recognition can work worse for others.
- Cameras with face recognition raise serious privacy questions.
Exercise
Exercise
In a greyscale image, what does each pixel store?
