{"id":173,"date":"2021-05-24T17:20:40","date_gmt":"2021-05-24T08:20:40","guid":{"rendered":"https:\/\/rfsec.ddns.net\/db\/?p=173"},"modified":"2021-06-01T14:00:49","modified_gmt":"2021-06-01T05:00:49","slug":"tansorflow-light","status":"publish","type":"post","link":"https:\/\/rfsec.ddns.net\/db\/?p=173","title":{"rendered":"raspberry pi4\u3067TansorFlow-Light\u3092\u4f7f\u3063\u3066\u307f\u305f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30b5\u30a4\u30c8\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/EdjeElectronics\/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi\/blob\/master\/Raspberry_Pi_Guide.md\">https:\/\/github.com\/EdjeElectronics\/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi\/blob\/master\/Raspberry_Pi_Guide.md<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30ab\u30e1\u30e9\u52d5\u753b\u306e\u8a8d\u8b58\u5b9f\u884c\u4f8b:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u822a\u7a7a\u6a5f\u306e\u6620\u50cf\u304c\u5c0f\u3055\u3044\uff08\u9060\u65b9\u306e\u76ee\u6a19\uff09\u3068\u3001\u51e7(kite)\u3084\u9ce5(bird)\u3068\u3057\u3066\u8aa4\u8a8d\u8b58\u3055\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3002\u518d\u751f\u901f\u5ea6\u3092\u9045\u304f\u3057\u3066\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"202105261146\" width=\"625\" height=\"352\" src=\"https:\/\/www.youtube.com\/embed\/m6d-9Fcd1zU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u624b\u9806\u306e\u5168\u822c<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1a. Update the Raspberry Pi<\/li><li>1b. \u30d1\u30c3\u30b1\u30fc\u30b8\u3092repository \u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u6210<\/li><li>1c. TensorFlow and OpenCV\u305d\u306e\u4ed6\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u30fc\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/li><li>1d. TensorFlow Lite detection model\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/li><li>1e. TensorFlow Lite model!\u306e\u5b9f\u884c<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1a. Update the Raspberry Pi<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>sudo apt-get update\nsudo apt-get dist-upgrade<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1b.<\/strong> \u30ea\u30dd\u30b8\u30c8\u30ea\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u6210<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$ git clone https:\/\/github.com\/EdjeElectronics\/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi.git\n\n\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u30fc\u540d\u304c\u9577\u3044\u306e\u3067\u3001\u77ed\u3081\u306a\u540d\u79f0\u306b\u30ea\u30cd\u30fc\u30e0\n$ mv TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi tflite1\n$ cd tflite1\n\nvirtualenv\u3092\u5229\u7528\u3057\u305f\u4eee\u60f3\u74b0\u5883\u3092\u69cb\u7bc9\uff1a\n$sudo pip3 install virtualenv\n\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3067\u4eee\u60f3\u74b0\u5883\u3000&quot;tflite1-env&quot;\u3000\u3092\u4f5c\u6210\n$ python3 -m venv tflite1-env\n\n&quot;tflite1-env&quot;\u306e\u6d3b\u6027\u5316\n$ source tflite1-env\/bin\/activate<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1c<\/strong>. Install TensorFlow Lite dependencies and OpenCV<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$ bash get_pi_requirements.sh\n\u6b21\u306eURL\u304b\u3089\u3001\u81ea\u5206\u306e\u74b0\u5883\u306b\u3042\u3063\u305f\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u9078\u3093\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002\nhttps:\/\/github.com\/google-coral\/pycoral\/releases\/\n\u4f8b\u3048\u3070\u3001python3.8\u3000arm64bit\u306e\u5834\u5408\n$pip3 install pip3 install https:\/\/github.com\/google-coral\/pycoral\/re\nleases\/download\/v1.0.1\/tflite_runtime-2.5.0-cp38-cp38-linux_aarch64.whl<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1d<\/strong>. Set up TensorFlow Lite detection model<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8a8d\u8b58\u306e\u30e2\u30c7\u30eb\u3092\u30b9\u30af\u30e9\u30c3\u30c1\u304b\u3089\u4f5c\u308b\u306e\u306f\u5927\u5909\u306a\u306e\u3067\u3001\u3053\u3053\u3067\u306fGoogle&#8217;s sample\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u62dd\u501f<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$ wget https:\/\/storage.googleapis.com\/download.tensorflow.org\/models\/tflite\/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip\n$ unzip coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip -d Sample_TFLite_model<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1e.<\/strong> TensorFlow Lite model\u306e\u5b9f\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9759\u6b62\u753b\u50cf\u3001\u52d5\u753b\u3001\u30ab\u30e1\u30e9\u5165\u529b\u306a\u3069\u3092\u5bfe\u8c61\u3068\u3057\u305fpython\u30b9\u30af\u30ea\u30d7\u30c8\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u3001\u540c\u68b1\u3055\u308c\u3066\u3044\u308btest.mp4\u52d5\u753b\u3067\u30c6\u30b9\u30c8\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>$ python3 TFLite_detection_video.py --modeldir=Sample_TFLite_model\n\n\u5225\u306e\u52d5\u753b\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408,\u3000--video\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u30d5\u30a1\u30a4\u30eb\u540d\uff08\u30d1\u30b9\uff09\u3092\u6307\u5b9a\u3059\u308b\u3002\n$ python3 TFLite_detection_video.py --video \u52d5\u753b\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3000--modeldir=Sample_TFLite_model<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">youtube\u304b\u3089\u62fe\u3063\u3066\u304d\u305f<a href=\"https:\/\/www.youtube.com\/watch?v=rX_QamabJoA&amp;t=292s\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=rX_QamabJoA&amp;t=292s\">\u5c90\u961c\u822a\u7a7a\u796d<\/a>\u306e\u52d5\u753b\u3067\u8a66\u3057\u3066\u307f\u305f\u3089\u3001\u601d\u3063\u305f\u4ee5\u4e0a\u306b\u826f\u597d\u306b\u691c\u51fa\u3057\u3066\u304f\u308c\u307e\u3057\u305f\u3002\uff08\u7a00\u306b\u822a\u7a7a\u6a5f\u3092\u51e7\u3001\u9ce5\u3068\u8aa4\u8a8d\u8b58\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Web\u30ab\u30e1\u30e9\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306f\uff1b<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>$ python3 TFLite_detection_webcam.py --modeldir=Sample_TFLite_model<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Google\u306e\u5b66\u7fd2\u6e08\u306e\u30e2\u30c7\u30eb\u306b\u306f\u3001\u6570\u5341\u7a2e\u985e\u306e\u8a8d\u8b58\u5bfe\u8c61\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u3002\u5bfe\u8c61\u306e\u30ea\u30b9\u30c8\u306fSample_TFLite_model\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u30fc\u306e\u4e2d\u306b\u3001labelmap.txt\u3068\u3044\u3046\u540d\u79f0\u3067\u5165\u3063\u3066\u3044\u308b\u3002\u540c\u3058\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u30fc\u5185\u306b\u3001\u30d5\u30a1\u30a4\u30ebdetect.tflite\u304c\u3042\u308a\u3001\u3053\u308c\u304c\u5b66\u7fd2\u6e08\u306e\u30c7\u30fc\u30bf\uff08\u30d0\u30a4\u30ca\u30ea\u30fc\uff09\u306e\u3088\u3046\u3060\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">TFLite_detection_video.py\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5c11\u3057\u6539\u5909\u3057\u3066\u3001\u4f8b\u3048\u3070\u822a\u7a7a\u6a5f\u3092\u691c\u51fa\u3057\u305f\u5834\u5408\u306b\u9650\u5b9a\u3057\u3066\u3001\u691c\u51fa\u67a0\u306e\u5ea7\u6a19\u3092\u53d6\u308a\u51fa\u3059\u3053\u3068\u3082\u3067\u304d\u305f\u306e\u3067\u3001\u3053\u308c\u307e\u3067\u306b\u5b9f\u88c5\u3057\u305f\u30b9\u30c6\u30c3\u30d7\u30e2\u30fc\u30bf\u3084\u30b5\u30fc\u30dc\u30e2\u30fc\u30bf\u3067\u30ab\u30e1\u30e9\u3092\u52d5\u304b\u3059\u5b9f\u9a13\u3068\u5408\u4f53\u3055\u305b\u3066\u307f\u305f\u3044\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u65b0\uff1a\u30b5\u30fc\u30dc\u30e2\u30fc\u30bf\u3067\u8ffd\u5c3e\u3059\u308b\u30b3\u30fc\u30c9<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import time\nimport math\nimport datetime\nimport cv2\nimport pigpio\nimport queue\nimport numpy as np\nimport sys\nfrom threading import Thread\nimport importlib.util\nimport os\n\nface_cascade_path = &#39;\/home\/pi\/opencv\/data\/haarcascades\/haarcascade_frontalface_default.xml&#39;\nface_cascade = cv2.CascadeClassifier(face_cascade_path)\nusleep = lambda x: time.sleep(x\/1000000.0)\n\nTILT=17\nPAN=27\nRPi=False\nGP=pigpio.pi(&#39;localhost&#39;,8880)\nGP.set_mode(PAN,pigpio.OUTPUT)\nGP.set_mode(TILT,pigpio.OUTPUT)\n\n# Define VideoStream class to handle streaming of video from webcam in separate processing thread\n# Source - Adrian Rosebrock, PyImageSearch: https:\/\/www.pyimagesearch.com\/2015\/12\/28\/increasing-raspberry-pi-fps-with-python-and-opencv\/\nclass VideoStream:\n    &quot;&quot;&quot;Camera object that controls video streaming from the Picamera&quot;&quot;&quot;\n    def __init__(self,resolution=(640,480),framerate=30):\n        # Initialize the PiCamera and the camera image stream\n        self.stream = cv2.VideoCapture(0)\n        #self.stream = cv2.VideoCapture(&#39;rtsp:\/\/admin:@192.168.68.128:554\/1\/h264major&#39;)\n        ret = self.stream.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*&#39;MJPG&#39;))\n        ret = self.stream.set(3,resolution[0])\n        ret = self.stream.set(4,resolution[1])\n\n        # Read first frame from the stream\n        (self.grabbed, self.frame) = self.stream.read()\n\n        # Variable to control when the camera is stopped\n        self.stopped = False\n\n    def start(self):\n        # Start the thread that reads frames from the video stream\n        Thread(target=self.update,args=()).start()\n        return self\n\n    def update(self):\n        # Keep looping indefinitely until the thread is stopped\n        while True:\n            # If the camera is stopped, stop the thread\n            if self.stopped:\n                # Close camera resources\n                self.stream.release()\n                return\n\n            # Otherwise, grab the next frame from the stream\n            (self.grabbed, self.frame) = self.stream.read()\n\n    def read(self):\n        # Return the most recent frame\n        return self.frame\n\n    def stop(self):\n        # Indicate that the camera and thread should be stopped\n        self.stopped = True\n        \ndef move(p0,p1,dev):\n    global tPos,pPos\n    global tMin,tMax,pMin,pMax\n    if dev==PAN:\n        if p1 &gt; pMax or p1 &lt; pMin:\n            return\n    else:\n        if p1 &gt; tMax or p1 &lt; tMin:\n            return\n            \n    deg=p0\n    dx=0.4\n    counts=int(abs(p1-p0)\/dx)\n    if p1&lt;p0:\n        dx=-dx\n    for i in range(0,counts):\n        deg=deg+dx\n        pw=500+int(deg*2000\/270)\n        GP.set_servo_pulsewidth(dev,pw)\n        #time.sleep(0.005)\n        #GP.set_servo_pulsewidth(dev,0)\n        if dev==TILT:   \n            tPos=deg\n        else:\n            pPos=deg\n\ndef key(k):\n    global pPos,tPos,PAN,TILT,track,f_all\n    global capture,fontFace,color,Green,Red\n\n    if k == ord(&#39;j&#39;):\n        new=pPos+2\n        move(pPos,new,PAN)\n        return\n    elif k == ord(&#39;k&#39;):\n        new=pPos-2\n        move(pPos,new,PAN)\n        return\n    elif k == ord(&#39;m&#39;):\n        new=tPos-2\n        move(tPos,new,TILT)\n        return\n    elif k == ord(&#39;i&#39;):\n        new=tPos+2\n        move(tPos,new,TILT)\n        return\n    elif k == ord(&#39;p&#39;):\n        tmp=input()\n        move(pPos,int(tmp),PAN)\n    elif k == ord(&#39;t&#39;):\n        tmp=input()\n        move(tPos,int(tmp),TILT)\n    elif k == ord(&#39;a&#39;):\n        f_all = not f_all\n    elif k == ord(&#39;f&#39;):\n        track = not(track)\n        if  track:\n            color=Red\n        else:\n            color=Green\n \n    elif k == ord(&#39;z&#39;):\n        move(tPos,0,TILT)\n        move(pPos,90,PAN)\n\ndef tracking(dX,dY):\n    global xW,yW,pPos,tPos,tW\n    ret=False\n    if dX &gt;0 :\n        move(pPos,pPos+1,PAN)\n    elif dX &lt; 0:\n        move(pPos,pPos-1,PAN)\n    if dY &gt; 0:\n        move(tPos,tPos+1,TILT)\n    elif dY &lt; 0:\n        move(tPos,tPos-1,TILT)\n    return ret\n\n# \u79fb\u52d5\u4f53\u691c\u77e5\ndef detectMOV(tm, tc):\n    global avg,  img1,frame\n    ret = False\n    x,y=0,0\n    if avg is None:\n        avg = img1.copy().astype(&quot;float&quot;)\n    else:\n        cv2.accumulateWeighted(img1, avg, 0.5)\n        frameDelta = cv2.absdiff(img1, cv2.convertScaleAbs(avg))\n        thresh = cv2.threshold(frameDelta, tm,  255, cv2.THRESH_BINARY)[1]\n        #cv2.imshow(&#39;th&#39;,thresh)\n        contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    #    contours=cv2.drawContours(img,contours,-1,(0,255,0),2)\n        for i in range(0,len(contours)):\n            if len(contours[i]) &gt; 0:\n                 if cv2.contourArea(contours[i]) &gt; tc:\n                    rect = contours[i]\n                    x, y, w, h = cv2.boundingRect(rect)\n                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)\n                    ret=True\n                        \n    return ret,x,y\n\n   \ndef detect_face(frame,gray):\n    global xW,yW,xC,yC\n    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)\n    xC,yC=xW,yW\n    for x, y, w, h in faces:\n        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)\n        face = frame[y: y + h, x: x + w]\n        try:\n            xC,yC=x+w\/2,y+h\/2\n            dX,dY=xC-xW,yC-yW\n            if track:\n                if(abs(dX)&gt;tW or (abs(dY)&gt;tW)):\n                    tracking(dX,dY)\n        except:\n            xC,yC=xW,yW    \n        \nif __name__ == &quot;__main__&quot;:\n\n    avg=None\n    tc=350     # Minimum area  moving detection\n    tm=10      # Threshold vale to BINARY\n    before = None\n    tPos,pPos=0,0\n    track=False\n    fontFace =cv2.FONT_HERSHEY_SIMPLEX\n    Red=(0,0,255)\n    Blue=(255,0,0)\n    Green=(0,255,0)\n    TGT=[&#39;airplane&#39;,&#39;bird&#39;,&#39;kite&#39;]\n    #capture = cv2.VideoCapture(1)\n    #\n    tMin,tMax=0,90  # minimum\/Maximum setting for TILT \n    pMin,pMax=0,180 # minimum\/Maximum setting for PAN\n    move(tPos,0,TILT)\n    move(pPos,0,PAN)\n    wMax=50\n    f_count=wMax\n\n    f_all=True\n    MODEL_NAME = &#39;Sample_TFLite_model&#39;\n    GRAPH_NAME = &#39;detect.tflite&#39;\n    LABELMAP_NAME = &#39;labelmap.txt&#39;\n    min_conf_threshold = 0.5\n    #resW, resH =1280,720\n    resW, resH =640,480\n    imW, imH = int(resW), int(resH)\n    use_TPU = False\n    size=(resW, resH)\n    Cx=int(resW\/2)\n    Cy=int(resH\/2)\n# Import TensorFlow libraries\n# If tflite_runtime is installed, import interpreter from tflite_runtime, else import from regular tensorflow\n# If using Coral Edge TPU, import the load_delegate library\n    pkg = importlib.util.find_spec(&#39;tflite_runtime&#39;)\n    if pkg:\n        from tflite_runtime.interpreter import Interpreter\n        if use_TPU:\n            from tflite_runtime.interpreter import load_delegate\n    else:\n        from tensorflow.lite.python.interpreter import Interpreter\n        if use_TPU:\n            from tensorflow.lite.python.interpreter import load_delegate\n\n    # If using Edge TPU, assign filename for Edge TPU model\n    if use_TPU:\n        # If user has specified the name of the .tflite file, use that name, otherwise use default &#39;edgetpu.tflite&#39;\n        if (GRAPH_NAME == &#39;detect.tflite&#39;):\n            GRAPH_NAME = &#39;edgetpu.tflite&#39;\n\n    # Get path to current working directory\n    CWD_PATH = os.getcwd()\n\n    # Path to .tflite file, which contains the model that is used for object detection\n    PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,GRAPH_NAME)\n\n    # Path to label map file\n    PATH_TO_LABELS = os.path.join(CWD_PATH,MODEL_NAME,LABELMAP_NAME)\n\n    # Load the label map\n    with open(PATH_TO_LABELS, &#39;r&#39;) as f:\n        labels = [line.strip() for line in f.readlines()]\n\n    # Have to do a weird fix for label map if using the COCO &quot;starter model&quot; from\n    # https:\/\/www.tensorflow.org\/lite\/models\/object_detection\/overview\n    # First label is &#39;???&#39;, which has to be removed.\n    if labels[0] == &#39;???&#39;:\n        del(labels[0])\n\n    # Load the Tensorflow Lite model.\n    # If using Edge TPU, use special load_delegate argument\n    if use_TPU:\n        interpreter = Interpreter(model_path=PATH_TO_CKPT,\n                                  experimental_delegates=[load_delegate(&#39;libedgetpu.so.1.0&#39;)])\n        print(PATH_TO_CKPT)\n    else:\n        interpreter = Interpreter(model_path=PATH_TO_CKPT)\n\n    interpreter.allocate_tensors()\n\n    # Get model details\n    input_details = interpreter.get_input_details()\n    output_details = interpreter.get_output_details()\n    height = input_details[0][&#39;shape&#39;][1]\n    width = input_details[0][&#39;shape&#39;][2]\n    W,H = width,height\n    xW,yW =int( W\/2),int(H\/2)\n    tW=W\/80         # minimum offcenter distance\n\n    floating_model = (input_details[0][&#39;dtype&#39;] == np.float32)\n\n    input_mean = 127.5\n    input_std = 127.5\n    move(tPos,20,TILT)\n    move(pPos,120,PAN)\n    # Initialize frame rate calculation\n    frame_rate_calc = 1\n    freq = cv2.getTickFrequency()\n\n    # Initialize video stream\n    videostream = VideoStream(resolution=(imW,imH),framerate=30).start()\n    time.sleep(1)\n    frame_rate = 24.0 # \u30d5\u30ec\u30fc\u30e0\u30ec\u30fc\u30c8\n    now=datetime.datetime.now().strftime(&quot;%Y%m%d_%H%M&quot;)\n    fmt = cv2.VideoWriter_fourcc(&#39;m&#39;, &#39;p&#39;, &#39;4&#39;, &#39;v&#39;) # \u30d5\u30a1\u30a4\u30eb\u5f62\u5f0f(\u3053\u3053\u3067\u306fmp4)\n    writer = cv2.VideoWriter(&#39;SV_&#39;+now+&#39;.mp4&#39;, fmt, frame_rate, size) # \u30e9\u30a4\u30bf\u30fc\u4f5c\u6210\n    frames=0\n    #for frame1 in camera.capture_continuous(rawCapture, format=&quot;bgr&quot;,use_video_port=True):\n    while True:\n        now=datetime.datetime.now().strftime(&quot;%Y%m%d_%H:%M:%S&quot;)\n        # Start timer (for calculating frame rate)\n        t1 = cv2.getTickCount()\n\n        # Grab frame from video stream\n        frame1 = videostream.read()\n\n        # Acquire frame and resize to expected shape [1xHxWx3]\n        frame = frame1.copy()\n        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        frame_resized = cv2.resize(frame_rgb, (width, height))\n        input_data = np.expand_dims(frame_resized, axis=0)\n\n        # Normalize pixel values if using a floating model (i.e. if model is non-quantized)\n        if floating_model:\n            input_data = (np.float32(input_data) - input_mean) \/ input_std\n\n        # Perform the actual detection by running the model with the image as input\n        interpreter.set_tensor(input_details[0][&#39;index&#39;],input_data)\n        interpreter.invoke()\n\n        # Retrieve detection results\n        boxes = interpreter.get_tensor(output_details[0][&#39;index&#39;])[0] # Bounding box coordinates of detected objects\n        classes = interpreter.get_tensor(output_details[1][&#39;index&#39;])[0] # Class index of detected objects\n        scores = interpreter.get_tensor(output_details[2][&#39;index&#39;])[0] # Confidence of detected objects\n        #num = interpreter.get_tensor(output_details[3][&#39;index&#39;])[0]  # Total number of detected objects (inaccurate and not needed)\n        # Draw framerate in corner of frame\n        msg=&#39;FPS: {0:.2f}&#39;.format(frame_rate_calc)\n        msg = msg + &#39; Track:&#39;+str(track)+ &#39; F:&#39; + str(frames) + &#39; T:&#39;+ str(f_all) + &#39; &#39; + now\n        cv2.putText(frame,msg,(30,50),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,0),1,cv2.LINE_AA)\n        # Loop over all detections and draw detection box if confidence is above minimum threshold\n        for i in range(len(scores)):\n            object_name = labels[int(classes[i])] # Look up object name from &quot;labels&quot; array using class index\n            if f_all or (object_name in TGT):\n                if ((scores[i] &gt; min_conf_threshold) and (scores[i] &lt;= 1.0)):\n                    # Get bounding box coordinates and draw box\n                    # Interpreter can return coordinates that are outside of image dimensions, need to force them to be within image using max() and min()\n                    ymin = int(max(1,(boxes[i][0] * imH)))\n                    xmin = int(max(1,(boxes[i][1] * imW)))\n                    ymax = int(min(imH,(boxes[i][2] * imH)))\n                    xmax = int(min(imW,(boxes[i][3] * imW)))\n                    x,y=Cx,Cy\n                    if (xmax-xmin)*(ymax-ymin)&lt;10000:\n                        x=xmin+int((xmax-xmin)*0.5)\n                        y=ymin+int((ymax-ymin)*0.5)\n                        if f_all:\n                            # Draw label\n                            label = &#39;%s: %d%%&#39; % (object_name, int(scores[i]*100))\n                            labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2) # Get font size\n                            label_ymin = max(ymin, labelSize[1] + 10) # Make sure not to draw label too close to top of window\n                            cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)\n                            cv2.rectangle(frame, (xmin, label_ymin-labelSize[1]-10), (xmin+labelSize[0], label_ymin+baseLine-10), (255, 255, 255), cv2.FILLED) # Draw white box to put label text in\n                            cv2.putText(frame, label, (xmin, label_ymin-7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) # Draw label text\n                        else:\n                            x=xmin+int((xmax-xmin)*0.5)\n                            y=ymin+int((ymax-ymin)*0.5)\n                            cv2.circle(frame,(x,y),4,color=Green,thickness=1)\n                            cv2.circle(frame,(x,y),10,color=Green,thickness=1)\n                            cv2.circle(frame,(x,y),16,color=Green,thickness=1)\n                            frames=frames+1\n                            writer.write(frame)\n                            f_count=wMax\n                        dW = Cx - x\n                        dH = Cy - y\n                        msg=&#39;dW:&#39;+str(dW)+&#39; dH:&#39;+ str(dH)\n                        cv2.putText(frame, msg, (30, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 1) \n                        #msg=tgt_track(track,dH,dW,tH,tW,ptz,moverequest)\n                        msg=tracking(dW,dH)\n\n        # All the results have been drawn on the frame, so it&#39;s time to display it.\n        cv2.imshow(&#39;Object detector&#39;, frame)\n        if f_count&gt;0 and f_count !=wMax:\n            if not f_all:\n                writer.write(frame)\n        f_count=f_count-1\n        # Calculate framerate\n        t2 = cv2.getTickCount()\n        time1 = (t2-t1)\/freq\n        frame_rate_calc= 1\/time1\n        # Press &#39;q&#39; to quit\n        k=cv2.waitKey(1) & 0xFF\n        key(k)       \n        if k == ord(&#39;q&#39;):\n            break\n        for i in range(5):\n            frame1 = videostream.read()\n    # Clean up\n    cv2.destroyAllWindows()\n    videostream.stop()\n    if writer is not None:\n        writer.release()\n    move(tPos,10,TILT)\n    move(pPos,90,PAN)\n    GP.stop()\n    print(&#39;Finish!&#39;)<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u30b5\u30a4\u30c8\u306e\u624b\u9806\u306b\u5f93\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\uff1a https:\/\/github.com\/EdjeElectronics\/TensorFlow-Lite-Object-Detection-on-Android-and-Raspber [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_src":null,"author_info":{"display_name":"mars","author_link":"https:\/\/rfsec.ddns.net\/db\/?author=1"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=173"}],"version-history":[{"count":13,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}