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.
[notice] A new release of pip is available: 23.0.1 -> 23.1.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
nohup eva_server > eva.log 2>&1 &
[notice] A new release of pip is available: 23.0.1 -> 23.1.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.

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#

cursor.execute('DROP TABLE IF EXISTS ObjectDetectionVideos')
response = cursor.fetch_all()
response.as_df()

cursor.execute('LOAD VIDEO "ua_detrac.mp4" INTO ObjectDetectionVideos;')
response = cursor.fetch_all()
response.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';
      """)
response = cursor.fetch_all()
response.as_df()
0
0 UDF Yolo already exists, nothing added.

Run Object Detector on the video#

cursor.execute("""SELECT id, Yolo(data)
                  FROM ObjectDetectionVideos 
                  WHERE id < 20""")
response = cursor.fetch_all()
response.as_df()
objectdetectionvideos.id yolo.labels yolo.bboxes yolo.scores
0 0 [car, car, car, car, car, car, person, car, ca... [[829.0, 277.0, 960.0, 360.0], [615.0, 216.0, ... [0.91, 0.86, 0.85, 0.83, 0.76, 0.73, 0.72, 0.7...
1 1 [car, car, car, car, car, car, car, car, car, ... [[832.0, 278.0, 960.0, 361.0], [616.0, 216.0, ... [0.92, 0.85, 0.84, 0.83, 0.78, 0.76, 0.76, 0.7...
2 2 [car, car, car, car, car, car, car, person, ca... [[836.0, 279.0, 960.0, 362.0], [618.0, 216.0, ... [0.92, 0.84, 0.84, 0.82, 0.81, 0.75, 0.73, 0.7...
3 3 [car, car, car, car, car, car, car, car, car, ... [[839.0, 280.0, 960.0, 363.0], [619.0, 217.0, ... [0.91, 0.84, 0.82, 0.8, 0.8, 0.75, 0.74, 0.72,...
4 4 [car, car, car, car, car, car, car, car, car, ... [[843.0, 281.0, 960.0, 364.0], [621.0, 218.0, ... [0.9, 0.85, 0.83, 0.8, 0.76, 0.73, 0.72, 0.72,...
5 5 [car, car, car, car, car, car, person, car, ca... [[847.0, 282.0, 960.0, 363.0], [623.0, 218.0, ... [0.89, 0.86, 0.84, 0.8, 0.78, 0.74, 0.72, 0.72...
6 6 [car, car, car, car, car, car, car, person, ca... [[851.0, 283.0, 959.0, 360.0], [625.0, 219.0, ... [0.89, 0.87, 0.85, 0.81, 0.79, 0.73, 0.72, 0.7...
7 7 [car, car, car, car, car, car, car, car, car, ... [[855.0, 284.0, 960.0, 357.0], [626.0, 220.0, ... [0.9, 0.87, 0.84, 0.83, 0.83, 0.79, 0.73, 0.67...
8 8 [car, car, car, car, car, car, person, car, ca... [[859.0, 285.0, 960.0, 357.0], [628.0, 221.0, ... [0.89, 0.88, 0.83, 0.82, 0.79, 0.71, 0.68, 0.6...
9 9 [car, car, car, car, car, car, car, person, ca... [[863.0, 286.0, 960.0, 357.0], [630.0, 222.0, ... [0.88, 0.87, 0.84, 0.82, 0.8, 0.75, 0.74, 0.74...
10 10 [car, car, car, car, car, car, car, person, ca... [[632.0, 223.0, 744.0, 284.0], [867.0, 287.0, ... [0.88, 0.88, 0.85, 0.82, 0.8, 0.79, 0.76, 0.71...
11 11 [car, car, car, car, car, car, person, car, ca... [[871.0, 289.0, 960.0, 356.0], [634.0, 223.0, ... [0.9, 0.9, 0.85, 0.8, 0.79, 0.77, 0.69, 0.68, ...
12 12 [car, car, car, car, car, car, car, person, ca... [[636.0, 223.0, 750.0, 287.0], [875.0, 290.0, ... [0.9, 0.88, 0.83, 0.81, 0.78, 0.78, 0.78, 0.67...
13 13 [car, car, car, car, car, car, person, car, ca... [[171.0, 409.0, 291.0, 539.0], [637.0, 224.0, ... [0.9, 0.89, 0.89, 0.83, 0.81, 0.81, 0.72, 0.71...
14 14 [car, car, car, car, car, car, person, car, ca... [[174.0, 405.0, 294.0, 538.0], [885.0, 291.0, ... [0.9, 0.89, 0.88, 0.84, 0.82, 0.81, 0.75, 0.72...
15 15 [car, car, car, car, car, car, person, car, ca... [[888.0, 293.0, 960.0, 355.0], [177.0, 400.0, ... [0.89, 0.88, 0.87, 0.84, 0.82, 0.78, 0.76, 0.7...
16 16 [car, car, car, car, car, car, car, person, ca... [[893.0, 293.0, 960.0, 355.0], [180.0, 396.0, ... [0.88, 0.88, 0.87, 0.82, 0.81, 0.76, 0.75, 0.7...
17 17 [car, car, car, car, car, car, car, person, ca... [[182.0, 392.0, 296.0, 519.0], [897.0, 294.0, ... [0.9, 0.89, 0.87, 0.83, 0.82, 0.78, 0.72, 0.69...
18 18 [car, car, car, car, car, car, car, person, mo... [[901.0, 295.0, 960.0, 356.0], [647.0, 225.0, ... [0.88, 0.88, 0.83, 0.82, 0.8, 0.78, 0.75, 0.7,...
19 19 [car, car, car, car, car, person, car, car, ca... [[648.0, 226.0, 770.0, 293.0], [906.0, 297.0, ... [0.89, 0.87, 0.81, 0.8, 0.78, 0.77, 0.73, 0.72...

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'

dataframe = response.as_df()
annotate_video(dataframe, input_path, output_path)
Video.from_file(output_path)
../../_images/02-object-detection_17_0.png ../../_images/02-object-detection_17_1.png ../../_images/02-object-detection_17_2.png ../../_images/02-object-detection_17_3.png

Dropping an User-Defined Function (UDF)#

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