Browse Source

updates and fixes

dianamaro 2 years ago
parent
commit
8cfc2370dd
6 changed files with 131 additions and 109 deletions
  1. 48 54
      aSIT.ipynb
  2. 22 13
      background.ipynb
  3. 2 0
      controllers/camera.ipynb
  4. 5 2
      controllers/position.ipynb
  5. 44 30
      hippoSIT.ipynb
  6. 10 10
      sandbox/tracking.ipynb

File diff suppressed because it is too large
+ 48 - 54
aSIT.ipynb


+ 22 - 13
background.ipynb

@@ -2,15 +2,18 @@
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 76,
+   "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
     "import cv2\n",
     "import os, json\n",
     "import numpy as np\n",
+    "import time\n",
     "\n",
-    "from controllers.situtils import FPSTimes"
+    "import nbimporter\n",
+    "from controllers.situtils import FPSTimes\n",
+    "from controllers.serial import MCSArduino"
    ]
   },
   {
@@ -22,14 +25,14 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 77,
+   "execution_count": 2,
    "metadata": {},
    "outputs": [],
    "source": [
     "is_light = True\n",
     "#settings_filename = os.path.join('profiles', 'kate_postrack.json')\n",
     "#settings_filename = os.path.join('profiles', 'kate_aSIT_single.json')\n",
-    "settings_filename = os.path.join('profiles', 'andrey_hippoSIT_test.json')\n",
+    "settings_filename = os.path.join('profiles', 'andrey_hippoSIT_prod.json')\n",
     "\n",
     "with open(settings_filename) as json_file:\n",
     "    cfg = json.load(json_file)"
@@ -37,10 +40,12 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 78,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [],
    "source": [
+    "board = MCSArduino(cfg['experiment']['MCSArduinoPort'])\n",
+    "\n",
     "# Define BGR colors\n",
     "BGR_COLOR = {\n",
     "    'red': (0,0,255),\n",
@@ -56,10 +61,11 @@
     "cfg_pos = cfg['position']\n",
     "\n",
     "cap = cv2.VideoCapture(cfg_cam['source'], cfg_cam['api']) if cfg_cam['api'] else cv2.VideoCapture(cfg_cam['source'])\n",
-    "cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))\n",
+    "cap.set(cv2.CAP_PROP_FPS, cfg_cam['fps'])\n",
+    "time.sleep(1)  # this helps to keep the FPS stable\n",
     "cap.set(cv2.CAP_PROP_FRAME_WIDTH, cfg_cam['frame_width'])\n",
     "cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cfg_cam['frame_height'])\n",
-    "cap.set(cv2.CAP_PROP_FPS, cfg_cam['fps'])\n",
+    "\n",
     "cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))    \n",
     "\n",
     "fps = FPSTimes()\n",
@@ -76,17 +82,20 @@
     "    cv2.circle(mask, (cfg_pos['arena_x'], cfg_pos['arena_y']), cfg_pos['arena_radius'], BGR_COLOR['white'], -1)\n",
     "    \n",
     "    frame = cv2.bitwise_and(src1=frame, src2=mask)\n",
-    "    cv2.putText(frame, 'LIGHT' if is_light else 'DARK', (10, 20), cv2.FONT_HERSHEY_DUPLEX, .5, BGR_COLOR['white'])\n",
-    "    cv2.putText(frame, '%.2f FPS' % fps.get_avg_fps(), (10, 40), cv2.FONT_HERSHEY_DUPLEX, .5, BGR_COLOR['white'])    \n",
-    "    cv2.imshow('Press \"c\" to capture, \"q\" to quit', frame)\n",
-    "\n",
+    "    \n",
     "    k = cv2.waitKey(33)\n",
     "    if k == ord('c'):\n",
     "        f_name = cfg_pos['background_light'] if is_light else cfg_pos['background_dark']\n",
     "        cv2.imwrite(os.path.join('assets', f_name), frame)\n",
+    "    if k == ord('l'):\n",
+    "        board.switch_light()\n",
+    "        is_light = not is_light\n",
+    "    if k == ord('q'):  # full quit\n",
     "        break\n",
-    "    if k == ord('q'):\n",
-    "        break        \n",
+    "        \n",
+    "    cv2.putText(frame, 'LIGHT' if is_light else 'DARK', (10, 20), cv2.FONT_HERSHEY_DUPLEX, .5, BGR_COLOR['white'])\n",
+    "    cv2.putText(frame, '%.2f FPS' % fps.get_avg_fps(), (10, 40), cv2.FONT_HERSHEY_DUPLEX, .5, BGR_COLOR['white'])    \n",
+    "    cv2.imshow('Press \"c\" to capture, \"q\" to quit', frame)\n",
     "\n",
     "# When the background image is captured, release the capture\n",
     "cap.release()\n",

+ 2 - 0
controllers/camera.ipynb

@@ -25,6 +25,7 @@
     "    # check camera output formats with FFMPEG\n",
     "    # https://stackoverflow.com/questions/15301608/how-to-query-a-webcams-output-formats\n",
     "    #\n",
+    "    # cd D:\\software\\ffmpeg\n",
     "    # ffmpeg -list_devices true -f dshow -i dummy\n",
     "    # ffmpeg -list_options true -f dshow -i video=\"HD USB Camera\"\n",
     "    #\n",
@@ -51,6 +52,7 @@
     "        # preparing a MPEG video stream\n",
     "        self.stream = cv2.VideoCapture(cfg['source'], cfg['api']) if cfg['api'] else cv2.VideoCapture(cfg['source'])\n",
     "        self.stream.set(cv2.CAP_PROP_FPS, cfg['fps'])\n",
+    "        time.sleep(1)  # this helps to keep the FPS stable\n",
     "        self.stream.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))\n",
     "        self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, cfg['frame_width'])\n",
     "        self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, cfg['frame_height'])\n",

+ 5 - 2
controllers/position.ipynb

@@ -28,7 +28,8 @@
     "    default_cfg = {\n",
     "        \"background_light\": \"background_light.png\",\n",
     "        \"background_dark\": \"background_dark.png\",\n",
-    "        \"threshold\": 40,\n",
+    "        \"threshold_light\": 40,\n",
+    "        \"threshold_dark\": 20,\n",
     "        \"arena_x\": 522,\n",
     "        \"arena_y\": 372,\n",
     "        \"arena_radius\": 330,\n",
@@ -119,6 +120,7 @@
     "            subject = cv2.subtract(masked_frame, self.background)\n",
     "        else:\n",
     "            subject = cv2.subtract(self.background, masked_frame)\n",
+    "        subject = cv2.absdiff(masked_frame, self.background)\n",
     "\n",
     "        # Converts subject to grey scale\n",
     "        subject_gray = cv2.cvtColor(subject, cv2.COLOR_BGR2GRAY)\n",
@@ -126,7 +128,8 @@
     "        # Applies blur and thresholding to the subject\n",
     "        kernel_size = (25,25)\n",
     "        frame_blur = cv2.GaussianBlur(subject_gray, kernel_size, 0)\n",
-    "        _, thresh = cv2.threshold(frame_blur, self.cfg['threshold'], 255, cv2.THRESH_BINARY)\n",
+    "        th = self.cfg['threshold_light'] if self.is_light else self.cfg['threshold_dark']\n",
+    "        _, thresh = cv2.threshold(frame_blur, th, 255, cv2.THRESH_BINARY)\n",
     "\n",
     "        # Finds contours and selects the contour with the largest area\n",
     "        contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)\n",

File diff suppressed because it is too large
+ 44 - 30
hippoSIT.ipynb


File diff suppressed because it is too large
+ 10 - 10
sandbox/tracking.ipynb