Jelajahi Sumber

fixed a couple of bugs

dianamaro 2 tahun lalu
induk
melakukan
4d73838450
4 mengubah file dengan 22 tambahan dan 8 penghapusan
  1. 1 0
      .gitignore
  2. 2 0
      aSIT.ipynb
  3. 18 7
      controllers/position.ipynb
  4. 1 1
      controllers/video.ipynb

+ 1 - 0
.gitignore

@@ -12,6 +12,7 @@ settings_*
 
 # log files
 *_log.*
+*.csv
 
 
 # cache

+ 2 - 0
aSIT.ipynb

@@ -222,6 +222,8 @@
       "Webcam stream 1024.0:768.0 at 20.00 FPS started\n",
       "Fake Arduino - sending a TTL pulse\n",
       "Fake Arduino - sending a TTL pulse\n",
+      "Fake Arduino - sending a TTL pulse\n",
+      "Fake Arduino - sending a TTL pulse\n",
       "Fake Arduino - exiting...\n",
       "Position tracker stopped\n",
       "Video writer stopped\n",

+ 18 - 7
controllers/position.ipynb

@@ -2,13 +2,13 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
     "import numpy as np\n",
     "import threading\n",
-    "import time, os\n",
+    "import time, os, json\n",
     "import cv2\n",
     "import matplotlib.pyplot as plt\n",
     "import multiprocessing as mp\n",
@@ -29,6 +29,7 @@
     "        'arena_x': 512,\n",
     "        'arena_y': 384,\n",
     "        'arena_radius': 350,\n",
+    "        \"max_fps\": 50,\n",
     "        'file_path': 'test_position_log.csv'\n",
     "    }\n",
     "    \n",
@@ -59,14 +60,21 @@
     "        print('Position tracker stopped')\n",
     "        \n",
     "    def update(self):\n",
+    "        next_frame = time.time() + 1.0/self.cfg['max_fps']\n",
+    "        \n",
     "        while not self.stopped:\n",
     "            frame = self.video_stream.read()\n",
     "            if frame is None:\n",
     "                time.sleep(0.05)\n",
     "                continue\n",
     "                \n",
+    "            if time.time() < next_frame:\n",
+    "                time.sleep(0.001)\n",
+    "                continue\n",
+    "                \n",
     "            self.count()  # count FPS\n",
     "            self.detect_position(frame)\n",
+    "            next_frame += 1.0/self.cfg['max_fps']\n",
     "\n",
     "            if self.status.value == 2:\n",
     "                with open(self.cfg['file_path'], 'a') as f:\n",
@@ -110,14 +118,14 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [
     {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "Webcam stream 960.0:720.0 at 20.00 FPS started\n",
+      "Webcam stream 1024.0:768.0 at 20.00 FPS started\n",
       "Camera released\n",
       "Position tracker stopped\n"
      ]
@@ -127,16 +135,19 @@
     "#import nbimporter\n",
     "#from camera import WebcamStream\n",
     "\n",
+    "with open(os.path.join('..', 'settings_light.json')) as json_file:\n",
+    "    cfg = json.load(json_file)\n",
+    "\n",
     "# controller status: 1 - detecting, 2 - detecting + logging\n",
     "status = mp.Value('i', 1)\n",
     "\n",
     "# let's use a webcam stream\n",
-    "vs = WebcamStream(WebcamStream.default_cfg)\n",
+    "vs = WebcamStream(cfg['camera'])\n",
     "vs.start()  # stream runs in a separate thread\n",
     "\n",
     "# init controller\n",
-    "pt_cfg = PositionTracker.default_cfg\n",
-    "pt_cfg['background_file'] = os.path.join('..', 'assets', 'background.png')\n",
+    "pt_cfg = cfg['position']\n",
+    "pt_cfg['background_file'] = os.path.join('..', 'assets', 'background_light.png')\n",
     "pt = PositionTracker(status, vs, pt_cfg)\n",
     "pt.start()\n",
     "\n",

+ 1 - 1
controllers/video.ipynb

@@ -62,7 +62,7 @@
     "        while not self.stopped:\n",
     "            if self.status.value == 2:\n",
     "                if time.time() < next_frame:\n",
-    "                    continue\n",
+    "                    continue  # TODO sleep here?\n",
     "\n",
     "                #frame = self.video_stream.read()\n",
     "                frame = self.video_stream.frame_with_infos\n",