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.
[ -z "$(lsof -ti:5432)" ] || kill -9 "$(lsof -ti:5432)"
sh: 1: kill: Illegal number: 10378
11257
nohup eva_server > eva.log 2>&1 &
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
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
# Getting the Yolo object detector
!wget -nc https://raw.githubusercontent.com/georgia-tech-db/eva/master/eva/udfs/yolo_object_detector.py
File 'ua_detrac.mp4' already there; not retrieving.
File 'yolo_object_detector.py' 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 ObjectDetectionVideos')
response = cursor.fetch_all()
print(response)
cursor.execute('LOAD VIDEO "*.mp4" INTO ObjectDetectionVideos;')
response = cursor.fetch_all()
print(response)
@status: ResponseStatus.SUCCESS
@batch:
0
0 Table Successfully dropped: ObjectDetectionVideos
@query_time: 0.02094384000156424
@status: ResponseStatus.SUCCESS
@batch:
0
0 Number of loaded VIDEO: 1
@query_time: 0.059963414998492226
Visualize Video#
from IPython.display import Video
Video("ua_detrac.mp4", embed=True)
Register FasterRCNN Object Detection model in an User-Defined Function (UDF) in EVA#
cursor.execute("""CREATE UDF IF NOT EXISTS YoloV5
INPUT (frame NDARRAY UINT8(3, ANYDIM, ANYDIM))
OUTPUT (labels NDARRAY STR(ANYDIM), bboxes NDARRAY FLOAT32(ANYDIM, 4),
scores NDARRAY FLOAT32(ANYDIM))
TYPE Classification
IMPL 'yolo_object_detector.py';
""")
response = cursor.fetch_all()
print(response)
@status: ResponseStatus.SUCCESS
@batch:
0
0 UDF YoloV5 successfully added to the database.
@query_time: 0.5039928840014909
Run Object Detector on the video#
cursor.execute("""SELECT id, YoloV5(data)
FROM ObjectDetectionVideos
WHERE id < 20""")
response = cursor.fetch_all()
print(response)
@status: ResponseStatus.SUCCESS
@batch:
objectdetectionvideos.id \
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
yolov5.labels \
0 [car, car, car, car, car, car, car, car, car, person, truck, car, car, motorcycle, car, car, car...
1 [car, car, car, car, car, car, car, car, car, person, truck, car, car, car, motorcycle, car, car...
2 [car, car, car, car, car, car, car, car, car, person, car, motorcycle, truck, car, car, car, car...
3 [car, car, car, car, car, car, car, person, car, car, car, motorcycle, truck, car, car, car, car...
4 [car, car, car, car, car, car, car, car, car, person, motorcycle, car, car, truck, car, car, car...
5 [car, car, car, car, car, car, car, car, person, car, car, truck, motorcycle, car, car, car, car...
6 [car, car, car, car, car, car, car, person, car, car, car, truck, motorcycle, car, car, car, car...
7 [car, car, car, car, car, car, car, car, person, car, car, truck, motorcycle, car, car, car, car...
8 [car, car, car, car, car, car, car, car, car, car, person, motorcycle, truck, car, car, car, car...
9 [car, car, car, car, car, car, car, car, car, car, person, motorcycle, truck, car, car, car, per...
10 [car, car, car, car, car, car, car, car, car, person, car, motorcycle, truck, car, car, car, car...
11 [car, car, car, car, car, car, car, person, car, car, motorcycle, truck, car, car, car, car, car...
12 [car, car, car, car, car, car, car, car, person, car, car, motorcycle, truck, car, motorcycle, c...
13 [car, car, car, car, car, car, car, car, car, car, person, motorcycle, truck, motorcycle, car, c...
14 [car, car, car, car, car, car, car, car, car, person, car, truck, motorcycle, motorcycle, car, c...
15 [car, car, car, car, car, car, car, car, car, person, car, truck, motorcycle, motorcycle, car, c...
16 [car, car, car, car, car, car, car, car, car, person, car, truck, motorcycle, motorcycle, car, c...
17 [car, car, car, car, car, car, car, car, car, person, car, truck, motorcycle, car, motorcycle, c...
18 [car, car, car, car, car, car, car, car, car, person, car, motorcycle, truck, car, car, car, mot...
19 [car, car, car, car, car, car, car, car, car, person, car, truck, car, motorcycle, car, car, mot...
yolov5.bboxes \
0 0 [615.0177612304688, 216.32139587402344, 720.9974365234375, 275.111572265625]
1 ...
1 0 [831.301025390625, 277.3230895996094, 960.0, 361.0600280761719]
1 ...
2 0 [835.5814819335938, 278.3634033203125, 960.0, 363.215576171875]
1 [6...
3 0 [839.367431640625, 279.6907653808594, 960.0, 363.59478759765625]
1 ...
4 0 [842.60888671875, 280.2098083496094, 960.0, 364.78851318359375]
1 ...
5 0 [623.1436767578125, 219.5082550048828, 732.8285522460938, 279.24090576171875]
1 ...
6 0 [624.30029296875, 219.43482971191406, 734.7637939453125, 280.71990966796875]
1 ...
7 0 [626.1151123046875, 220.8157196044922, 738.7499389648438, 281.8699951171875]
1 ...
8 0 [628.363525390625, 222.46878051757812, 740.7850341796875, 283.034423828125]
1 ...
9 0 [629.436767578125, 223.33621215820312, 743.2886352539062, 284.0799560546875]
1 ...
10 0 [631.0487060546875, 223.26510620117188, 744.912353515625, 284.68511962890625]
1 ...
11 0 [633.354736328125, 223.04473876953125, 747.6873779296875, 285.7003173828125]
1 ...
12 0 [634.788818359375, 222.9927978515625, 750.9302978515625, 286.6802978515625]
1 ...
13 0 [636.74267578125, 223.11044311523438, 753.564208984375, 287.98382568359375]
1 ...
14 0 [639.1224975585938, 224.44171142578125, 755.9517822265625, 288.9028015136719]
1 ...
15 0 [640.0599365234375, 225.69577026367188, 759.0479736328125, 289.31097412109375]
1 ...
16 0 [642.4671630859375, 225.98965454101562, 762.218017578125, 289.93438720703125]
1 ...
17 0 [645.32177734375, 226.70977783203125, 765.315673828125, 290.710205078125]
1 ...
18 0 [646.6506958007812, 227.4632568359375, 767.5223388671875, 291.5223388671875]
1 ...
19 0 [647.9536743164062, 227.80906677246094, 770.7684326171875, 293.2704162597656]
1 ...
yolov5.scores
0 [0.8976885080337524, 0.8935551047325134, 0.8289324641227722, 0.7905621528625488, 0.7743206620216...
1 [0.9067937135696411, 0.8873313069343567, 0.8370817303657532, 0.8034704923629761, 0.7769315242767...
2 [0.9066711664199829, 0.8586535453796387, 0.8389690518379211, 0.8235834836959839, 0.8092165589332...
3 [0.9023867845535278, 0.877165675163269, 0.8301073312759399, 0.8268834352493286, 0.80941176414489...
4 [0.896634578704834, 0.8964878916740417, 0.8297849893569946, 0.819599986076355, 0.805158197879791...
5 [0.8945522904396057, 0.8825253844261169, 0.8602743744850159, 0.8407529592514038, 0.8190867900848...
6 [0.8903161287307739, 0.8829993009567261, 0.8565675616264343, 0.8527420163154602, 0.8207698464393...
7 [0.8851314783096313, 0.8641899824142456, 0.8491590619087219, 0.8254091143608093, 0.8131759166717...
8 [0.8761552572250366, 0.8475750088691711, 0.8426567316055298, 0.8185161352157593, 0.8122233748435...
9 [0.8814643621444702, 0.8636887073516846, 0.8601973056793213, 0.824044942855835, 0.81919133663177...
10 [0.8830854296684265, 0.8734781742095947, 0.8712113499641418, 0.8251326680183411, 0.8065018057823...
11 [0.8828204870223999, 0.8745031952857971, 0.8738451600074768, 0.8063144087791443, 0.7980766892433...
12 [0.8927745223045349, 0.8842473030090332, 0.8785966634750366, 0.8073288202285767, 0.8001270294189...
13 [0.8936073184013367, 0.8891677260398865, 0.8754435181617737, 0.8336889743804932, 0.8142510652542...
14 [0.9018195271492004, 0.8859033584594727, 0.8765249848365784, 0.8446863889694214, 0.7918394804000...
15 [0.9037039279937744, 0.8661507964134216, 0.8648058176040649, 0.8274552226066589, 0.7959752082824...
16 [0.9109860062599182, 0.8697980642318726, 0.8593544363975525, 0.8282628059387207, 0.7935150265693...
17 [0.9092432856559753, 0.8774809837341309, 0.8753394484519958, 0.8411899209022522, 0.8310674428939...
18 [0.9049758315086365, 0.8759955167770386, 0.8713913559913635, 0.855153501033783, 0.83450663089752...
19 [0.9039983749389648, 0.8782927989959717, 0.8762125968933105, 0.8406676054000854, 0.8191236257553...
@query_time: 1.4196726129994204
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[['yolov5.bboxes', 'yolov5.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
img=cv2.rectangle(frame, (x1, y1), (x2, y2), color1, thickness)
# object label
cv2.putText(img, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color1, thickness)
# frame label
cv2.putText(img, 'Frame ID: ' + str(frame_id), (700, 500), cv2.FONT_HERSHEY_SIMPLEX, 1.2, color2, thickness)
video.write(img)
# Show every fifth frame
if frame_id % 5 == 0:
plt.imshow(img)
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.batch.frames
annotate_video(dataframe, input_path, output_path)
Video.from_file(output_path)
Dropping an User-Defined Function (UDF)#
cursor.execute("DROP UDF YoloV5;")
response = cursor.fetch_all()
print(response)
@status: ResponseStatus.SUCCESS
@batch:
0
0 UDF YoloV5 successfully dropped
@query_time: 0.017563602999871364