Performance-wise, you’re probably better off with jetson-inference or the underlying libraries. OpenCV is slow and limited on Tegra (not Tegra’s fault, most OpenCV algorithms are CPU based and don’t use the GPU).
open camera
get image
save 1st copy of image & simultaneously run inference on 2nd copy
save 2nd copy with result (i.e. dog, cat, car, boat etc.)
This (should) do all that (I haven’t tested. Let me know if the saving doesn’t work):
import jetson.utils
import jetson.inference
import json
def cli_main():
"""from jetson inference detectnet-camera example"""
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--camera", default="0",
help="Index of the MIPI CSI camera to use (NULL for CSI camera 0)\n"
"or for VL42 cameras the /dev/video node to use.\n"
"By default, MIPI CSI camera 0 will be used.")
parser.add_argument("--width", type=int, default=1280,
help="desired width of camera stream (default is 1280 pixels)")
parser.add_argument("--height", type=int, default=720,
help="desired height of camera stream (default is 720 pixels)")
parser.add_argument("--dumpfile", help="json lines dumpfile",
default="dump.jl")
args = parser.parse_args()
main(**vars(args))
def main(width=1280, height=720, camera="0", dumpfile="dump.jl"):
camera = jetson.utils.gstCamera(width, height, camera)
classifier = jetson.inference.imageNet("googlenet")
frame_count = 0
with open(dumpfile, "w") as f:
try:
while True:
image = camera.CaptureRGBA(zeroCopy=True)
class_id, confidence = classifier.Classify(*image)
class_description = classifier.GetClassDesc(class_id)
jetson.utils.saveImageRGBA(f"{frame_count}.jpg", *image)
f.write(json.dumps(
{"fnum": frame_count, "desc": class_description}) + "\n")
frame_count += 1
except KeyboardInterrupt:
print("Got interrupt. Quitting.")
if __name__ == '__main__':
cli_main()
That’s mostly from the examples apart from the PIL part, which saves the image. You’ll need to “pip(3) install Pillow” in addition installing jetson-inference. Run it with --help for the usage options and press ctrl+c to stop capture and inference. The saving doesn’t technically run at the same time as the inference, but I can’t see a way to make it any faster in Python. In c++ it could possibly go a little faster, but it would be a lot more complicated to write. I’ll leave that exercise to you.
[s]Edit: actually, Pillow has this in it’s fromarray source:
if mode in ["1", "L", "I", "P", "F"]:
ndmax = 2
ndim (number of channels in this case) is 4 for a RGBA image. You may have to find another library to save a 4 channel floating point image to file or convert to uint8 using numpy beforehand. I’ll leave that as an exercise to you as well.[/s] There may already be a utility in jetson.utils. You might want to check.
Edit: There is indeed. Thanks Dusty. Updated the above.
edit: you may have to install libjpeg and headers if you have errors installing Pillow. (sudo apt install libjpeg-dev)