Python ·

Quick and easy image recognition with 9 lines of code in Python

Need a quick and easy image recognition solution in Python? Learn how to create one in 9 lines of code.


This is how you can create a simple image recognition script in Python using pre-trained deep learning models. The approach shown here is great if you want quick results without having to train your own neural network from scratch.

You'll need the following packages to run the script:

opencv-contrib-python
cvlib
pillow
tensorflow

Put them in your requirements.txt and install them with:

pip install -r requirements.txt

Note: The first time you run the script, TensorFlow and the pre-trained detection models may take a little while to load or download. Subsequent runs will be significantly faster.

The script in its entirety can be seen here:

import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
from PIL import Image

img = cv2.imread("DJI_0040.JPG")

bbox, label, conf = cv.detect_common_objects(img)
output_image = draw_bbox(img, bbox, label, conf)

im = Image.fromarray(output_image)
im.save("DJI_0040_labels.JPG")

Simply change the filename to your own image, and you are good to go. Below you can see an example of the input and output of the program.

Image without image recognition

Input image.

Image with image recognition

Output image with detected items marked.

What’s happening in the code?

  • cv2.imread() loads the image into memory as a NumPy array.
  • detect_common_objects() runs object detection using a pre-trained deep learning model.
  • bbox contains bounding box coordinates for each detected object.
  • label contains the detected object types.
  • conf contains confidence scores for each detection.
  • draw_bbox() draws boxes and labels directly onto the image.

The labels of all the identified objects are saved as a list in the label variable.

In my example, the label list includes 8 boats and 3 cars:

['boat', 'boat', 'boat', 'boat', 'boat', 'boat', 'boat', 'boat', 'car', 'car', 'car']

Using the results programmatically

Because the detected labels are returned as a simple Python list, it’s easy to build logic on top of the results. For example, you could count specific object types, trigger alerts, or store detections in a database:

boats = label.count('boat')
cars = label.count('car')

print(f"Detected {boats} boats and {cars} cars")

There you go. Image detection doesn’t get much simpler than this, and it’s a great starting point for more advanced use cases such as video analysis, drone imagery, or automated inspections.

Happy coding!