Topic 6: Vision-Language Models (VLM)

LLaVA Show

Learning Goals

Tasks

You will be writing your own code for the exercises using Ollama and the llava open vision-language model. It can run on a MacBook or PC with a GPU or Colab. To start up Ollama, use the shell command

Ollama handles selecting the appropriate GPU and quantization level for all platforms.

This Python snippet shows how to run the model:

The images key in the message is a list of file paths and/or base64 encoded strings of the file contents. The following snippet turns the contents of a file into a base64 encoded string:

You have several choices for creating the user interfaces for your programs:

Exercise 1: Vision-Language LangGraph Chat Agent

Write a chat agent that can carry on a multi-turn conversation about an image you upload. Use all that you have learned about good LangGraph style to structure the agent and manage the context. If the program runs slowly, check if it helps to reduce the resolution of the uploaded image.

Exercise 2: Video-Surveillance Agent

LLaVA is not powerful enough to handle video directly. However, you can still use it for video by extracting frames from the video every few seconds and sending them to LLaVA.

  1. Create a 2-minute video clip of an empty space that a person enters and exits at some point.

  2. Split in the video into frames that are 2 seconds apart using OpenCV (cv2). Install it with

    pip install opencv-python

    You do not need to install the full opencv-contrib-python. Here is a code snipped that spits a file and saves it as images:

  3. Write a program that runs through the all the frames with a prompt that asks LLaVA if there is a person in the scene. Write the times at which a person enters and exits the scene.

  4. Optional: Connect your program to a webcam and run it in real time. cv2.VideoCapture(0) typically captures a frame from the webcam in your laptop. Running locally on my MacBook Pro, it takes about 8.5 seconds for LLaVA to process a frame, so you will either need to increase the time between samples and/or run on a platform with more powerful GPUs such as Colab or a GPU-equipped PC.

  5. Optional: Run the program in a place where there are cats or dogs. Prompt LLaVA to let you know if there is a person, cat, or dog in the scene and how many of each. Is LLaVA able to count? If it sees people, it should also say INTRUDER ALERT (a good text to speech library is edge-tts)but not if it sees a cat or dog.

Resources

Portfolio

Create a subdirectory in your GitHub portfolio named Topic6VLM and save your programs, each modified version named to indicate its task number and purpose. Create appropriately named text files saving the outputs from your terminal sessions running the programs. Create README.md with a table of contents of the directory.