In this lesson I show you how to do object detection on the Raspberry Pi using Tensorflow Lite. We will write our first program and by the end of the lesson you will have your Pi detecting objects, boxing them and labeling them in OpenCV.
The video demonstrates step-by-step how to install the tensorflow libraries.
For your convenience I have included the code below we develop in this lesson
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import cv2 import time from picamera2 import Picamera2 from tflite_support.task import core from tflite_support.task import processor from tflite_support.task import vision import utils model='efficientdet_lite0.tflite' num_threads=4 dispW=1280 dispH=720 picam2=Picamera2() picam2.preview_configuration.main.size=(dispW,dispH) picam2.preview_configuration.main.format='RGB888' picam2.preview_configuration.align() picam2.configure("preview") picam2.start() webCam='/dev/video2' cam=cv2.VideoCapture(webCam) cam.set(cv2.CAP_PROP_FRAME_WIDTH, dispW) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH) cam.set(cv2.CAP_PROP_FPS, 30) pos=(20,60) font=cv2.FONT_HERSHEY_SIMPLEX height=1.5 weight=3 myColor=(255,0,0) fps=0 base_options=core.BaseOptions(file_name=model,use_coral=False, num_threads=num_threads) detection_options=processor.DetectionOptions(max_results=3, score_threshold=.3) options=vision.ObjectDetectorOptions(base_options=base_options,detection_options=detection_options) detector=vision.ObjectDetector.create_from_options(options) tStart=time.time() while True: ret, im = cam.read() #im=picam2.capture_array() #im=cv2.flip(im,-1) imRGB=cv2.cvtColor(im,cv2.COLOR_BGR2RGB) imTensor=vision.TensorImage.create_from_array(imRGB) detections=detector.detect(imTensor) image=utils.visualize(im, detections) cv2.putText(im,str(int(fps))+' FPS',pos,font,height,myColor,weight) cv2.imshow('Camera',im) if cv2.waitKey(1)==ord('q'): break tEnd=time.time() loopTime=tEnd-tStart fps= .9*fps +.1*1/loopTime tStart=time.time() cv2.destroyAllWindows() |