Code examples¶
Face detection¶
Pipes video frames captured from the flying Tello drone through the facenet detector and reports any human faces it sees.
#!/usr/bin/env python3
import asyncio
import jetson.inference
from jetson_tello import run_jetson_tello_app, get_coco_class
face_detector = jetson.inference.detectNet("facenet", threshold=0.5)
def detect_faces(drone, frame, cuda):
face_detections = face_detector.Detect(cuda)
for d in face_detections:
print(d)
async def fly(drone):
await drone.takeoff()
for i in range(4):
await drone.turn_clockwise(90)
await asyncio.sleep(3)
await drone.land()
run_jetson_tello_app(fly, process_frame=detect_faces)
Face and object detection¶
As face detection above, but also detecting objects in view at the same time.
#!/usr/bin/env python3
import asyncio
import jetson.inference
from jetson_tello import run_jetson_tello_app
face_detector = jetson.inference.detectNet("facenet", threshold=0.5)
object_detector = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
def detect_faces_and_objects(drone, frame, cuda):
face_detections = face_detector.Detect(cuda)
object_detections = object_detector.Detect(cuda)
print('faces:')
for d in face_detections:
print(d)
print('objects:')
for d in object_detections:
print(d)
async def fly(drone):
await drone.takeoff()
for i in range(4):
await drone.turn_clockwise(90)
await asyncio.sleep(3)
await drone.land()
run_jetson_tello_app(fly, process_frame=detect_faces_and_objects)
Example output:
faces:
<detectNet.Detection object>
-- ClassID: 0
-- Confidence: 0.809878
-- Left: 434.667
-- Top: 0
-- Right: 702.267
-- Bottom: 302.5
-- Width: 267.6
-- Height: 302.5
-- Area: 80949
-- Center: (568.467, 151.25)
objects:
<detectNet.Detection object>
-- ClassID: 7
-- Confidence: 0.500977
-- Left: 0
-- Top: 7.30054
-- Right: 959
-- Bottom: 719.04
-- Width: 959
-- Height: 711.74
-- Area: 682559
-- Center: (479.5, 363.171)
H.264 frames to NumPy arrays¶
Demonstrates the decoding of pre-captured video frames (of my cat, Marble) and loading into NumPy arrays suitable for analysis.
#!/usr/bin/env python3
from ast import literal_eval
from jetson_tello import h264_frame_to_numpy_array, NoFrameData
with open("frames.txt", "r") as f:
frames = [literal_eval(l) for l in f]
for frame in frames:
try:
decoded_frame, numpy_array = h264_frame_to_numpy_array(frame)
print(f'frame {decoded_frame.number} size: {decoded_frame.width} x {decoded_frame.height}')
print(numpy_array)
except NoFrameData:
print(f'(no frame data - skipped)')
H.264 frames to CUDA¶
The same video frames, but this time loaded into CUDA memory for GPU processing. The images are saved back out to JPEG files just so you can see that they are valid.
#!/usr/bin/env python3
from pathlib import Path
import jetson.utils
from jetson_tello import h264_frame_to_cuda, NoFrame
from ast import literal_eval
with open("frames.txt", "r") as f:
frames = [literal_eval(l) for l in f]
Path("h264_frames_to_cuda").mkdir(exist_ok=True)
for frame in frames:
try:
decoded_frame, cuda = h264_frame_to_cuda(frame)
i = decoded_frame.number
print(f'frame {i}:')
print(cuda)
file_path = f'h264_frames_to_cuda/frame-{i}.jpg'
jetson.utils.saveImageRGBA(file_path, cuda, decoded_frame.width, decoded_frame.height)
print(f'saved as {file_path}')
i += 1
except NoFrame:
print('(no frame data, skipped)')
print('-----------------------------------------------------------------')
Object detection¶
Runs the frames through the ssd-mobilenet-v2 detector and (hopefully) finds the cat.
#!/usr/bin/env python3
import jetson.inference
from jetson_tello import h264_frame_to_cuda, NoFrameData
from ast import literal_eval
with open("frames.txt", "r") as f:
frames = [literal_eval(l) for l in f]
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
for frame in frames:
try:
decoded_frame, cuda = h264_frame_to_cuda(frame)
detections = net.Detect(cuda)
i = decoded_frame.number
print(f'frame {i} detections:')
for d in detections:
print(d)
except NoFrameData:
pass