Object Detection Tutorial#

Run on Google Colab View source on GitHub Download notebook

Start EVA server#

We are reusing the start server notebook for launching the EVA server.

!wget -nc "https://raw.githubusercontent.com/georgia-tech-db/eva/master/tutorials/00-start-eva-server.ipynb"
%run 00-start-eva-server.ipynb
cursor = connect_to_server()
File ‘00-start-eva-server.ipynb’ already there; not retrieving.
Note: you may need to restart the kernel to use updated packages.
Stopping EVA Server ...
Starting EVA Server ...
nohup eva_server > eva.log 2>&1 &

Download the Videos#

# Getting the video files
!wget -nc "https://www.dropbox.com/s/k00wge9exwkfxz6/ua_detrac.mp4?raw=1" -O ua_detrac.mp4
File ‘ua_detrac.mp4’ already there; not retrieving.

Load the surveillance videos for analysis#

We use regular expression to load all the videos into the table#

response = (
    cursor.execute("DROP TABLE IF EXISTS ObjectDetectionVideos;").fetch_all().as_df()
)
cursor.execute(
    'LOAD VIDEO "ua_detrac.mp4" INTO ObjectDetectionVideos;'
).fetch_all().as_df()
0
0 Number of loaded VIDEO: 1

Visualize Video#

from IPython.display import Video
Video("ua_detrac.mp4", embed=True)

Register YOLO Object Detector an an User-Defined Function (UDF) in EVA#

cursor.execute("""
            CREATE UDF IF NOT EXISTS Yolo
            TYPE  ultralytics
            'model' 'yolov8m.pt';
      """).fetch_all().as_df()
0
0 UDF Yolo already exists, nothing added.

Run Object Detector on the video#

response = cursor.execute("""SELECT id, Yolo(data)
                  FROM ObjectDetectionVideos 
                  WHERE id < 20""").fetch_all().as_df()

Visualizing output of the Object Detector on the video#

import cv2
from pprint import pprint
from matplotlib import pyplot as plt

def annotate_video(detections, input_video_path, output_video_path):
    color1=(207, 248, 64)
    color2=(255, 49, 49)
    thickness=4

    vcap = cv2.VideoCapture(input_video_path)
    width = int(vcap.get(3))
    height = int(vcap.get(4))
    fps = vcap.get(5)
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') #codec
    video=cv2.VideoWriter(output_video_path, fourcc, fps, (width,height))

    frame_id = 0
    # Capture frame-by-frame
    # ret = 1 if the video is captured; frame is the image
    ret, frame = vcap.read() 

    while ret:
        df = detections
        df = df[['yolo.bboxes', 'yolo.labels']][df.index == frame_id]
        if df.size:
            dfLst = df.values.tolist()
            for bbox, label in zip(dfLst[0][0], dfLst[0][1]):
                x1, y1, x2, y2 = bbox
                x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
                # object bbox
                frame=cv2.rectangle(frame, (x1, y1), (x2, y2), color1, thickness) 
                # object label
                cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color1, thickness) 
                # frame label
                cv2.putText(frame, 'Frame ID: ' + str(frame_id), (700, 500), cv2.FONT_HERSHEY_SIMPLEX, 1.2, color2, thickness) 
            video.write(frame)

            # Stop after twenty frames (id < 20 in previous query)
            if frame_id == 20:
                break

            # Show every fifth frame
            if frame_id % 5 == 0:
                plt.imshow(frame)
                plt.show()

        
        frame_id+=1
        ret, frame = vcap.read()

    video.release()
    vcap.release()
from ipywidgets import Video, Image
input_path = 'ua_detrac.mp4'
output_path = 'video.mp4'

annotate_video(response, input_path, output_path)
Video.from_file(output_path)
../../_images/53aee7aa159877f4dbbfc9a24718171556de17d28c955804e566b41a7a5fdef5.png ../../_images/2eb39b245d6a6d7e40ae27fee8af535186ff798c2712d7bf961bbdcae48a97fb.png ../../_images/a9a126cc57a142e9e3b72334d5c2996e86ed5377fc89f81f15ffa5d28f3143e5.png ../../_images/d6e873142e59cdbc0a5aa3571f490fc9ff3da2a48652523b7f91207478ad17ac.png

Dropping an User-Defined Function (UDF)#

cursor.execute("DROP UDF IF EXISTS Yolo;").fetch_all().as_df()
0
0 UDF Yolo successfully dropped