Lugovtsov/5.2.1-frank-herz-experiment/video-digits-recognition.ipynb

1142 lines
150 KiB
Plaintext
Raw Normal View History

2023-09-15 14:01:17 +03:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "eaa2416a-0267-47ba-9d79-bb261e26a468",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from PIL import Image\n",
"import matplotlib.pyplot as plt \n",
"import numpy as np\n",
"from skimage import transform, color\n",
"import cv2 as cv\n",
"\n",
"tuple2int = {\n",
" (1, 1, 1, 1, 1, 1, 0) : 0,\n",
" (0, 1, 1, 0, 0, 0, 0) : 1,\n",
" (1, 1, 0, 1, 1, 0, 1) : 2,\n",
" (1, 1, 1, 1, 0, 0, 1) : 3,\n",
" (0, 1, 1, 0, 0, 1, 1) : 4,\n",
" (1, 0, 1, 1, 0, 1, 1) : 5,\n",
" (0, 0, 1, 1, 1, 1, 1) : 6,\n",
" (1, 1, 1, 0, 0, 0, 0) : 7,\n",
" (1, 1, 1, 1, 1, 1, 1) : 8,\n",
" (1, 1, 1, 0, 0, 1, 1) : 9\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3d2098e2-ef3d-4a2d-87c4-6be66407bffa",
"metadata": {},
"outputs": [],
"source": [
"# импортируем видео и берём первый кадр\n",
"cap = cv.VideoCapture('video5v.mp4')\n",
"_, frame = cap.read()\n",
"cap.release()\n",
"\n",
"# преобразуем в чб и обрезаем\n",
"img = color.rgb2gray(frame)[40:160, 60:810]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4137cf71-9e31-4359-a21a-7b54a6354534",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"0\n",
"1\n",
"4\n",
"0\n",
"0\n",
"0\n",
"2\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABKUAAADuCAYAAADobbT3AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0c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6qMOkKPIdaqO35Wmb/7HXYgMZ+/rTbXAO5RzKOZRzKOdQzqFs/TmHKodzqNebQ+08KBVbc68ZUeEURbdiKHIEVGTrbNXhWANQpABl748pOJ+xzlPvKYtAbCuzIqbgtgyx7/bdsfzGUBbR3Aabpsp9l0ZXJJNd8lREpoquxfJkjT1wNS2XRoh5YYOnM4mRlBgRZCSk3++HfRYmkwlGo1GYuksjp+VXx63GTfO1q2GLtSPrwHZJg3sf0ME1Gg08fvwYjx8/DlOLm81mWOfP9fBKBqy8bVkpR3YINNoRi7pp++NfrF6UMBAkdSR4OoLPMjK/MZ1kdMnmSeukqI4skbflKXJgShpth497euhnJVZKVFguldt0Og3XKa8sy0JUzKal5Iy/kzjafNpntdwxO0X/QtL27NmznEy0TkiyVA8AYDqd5iJl7BhQdwEEGZGUa/ovG0VtsqzDrSCZTZIEZ2dnuVkX9h0AclP0tT3pEghtaxrNstEtOxWf79JILJclaIeNHTLbidNO5DZSG4u2l7U7RYxH7ELC9rlvVziHcg7lHMo5lHMo51DOoW4G51CvP4faeVCqLGEVXNHvMYWJjaJRACqEmHF73gZgG1NsWrV1Qqr4RYRK09yFWKhsyhRjn/K8KMTKsKtT3wfb5HSTPGiDVug07yzLclNf1cCwfjUiU9apYF4qlcsNFhuNRjDas9kMFxcXaDQa6HQ6IcqkxEm/a1pKKmIOu4hw7KKHu6B
"text/plain": [
"<Figure size 1200x300 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"src = np.array([[0, 0], [0, 55], [230, 55], [230, 0]])\n",
"dst_1 = np.array([[15, 60], [15, 100], [151, 90], [151, 50]])\n",
"dst_2 = np.array([[604, 19], [600, 55], [740, 62], [744, 26]])\n",
"\n",
"warped = [None, None]\n",
"# warped = buf\n",
"tform3_1 = transform.ProjectiveTransform()\n",
"tform3_1.estimate(src, dst_1)\n",
"warped[0] = transform.warp(img, tform3_1, output_shape=(50, 300))\n",
"\n",
"tform3_2 = transform.ProjectiveTransform()\n",
"tform3_2.estimate(src, dst_2)\n",
"warped[1] = transform.warp(img, tform3_2, output_shape=(50, 300))\n",
"# warped[0] = buf\n",
"\n",
"fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 3))\n",
"\n",
"\n",
"segments = np.array([[9,34], [18, 44], [36, 42], [47,30], [36, 20], [18, 22], [28, 32]], dtype=int)\n",
"\n",
"shift_x = [0, 0]\n",
"shift_y = [52, 46]\n",
"start_x = [-4, -2]\n",
"start_y = [-3, 34]\n",
"\n",
"min_val = 0.019\n",
"delta = 3\n",
"for i in range(2):\n",
" ax[0][i].imshow(img, cmap=plt.cm.gray)\n",
" ax[0][i].plot(dst_1[:, 0], dst_1[:, 1], '.r')\n",
" ax[1][i].imshow(warped[i], cmap=plt.cm.gray)\n",
"\n",
" sh_x = shift_x[i]\n",
" sh_y = shift_y[i]\n",
" numbers = np.array([[0, 0], [sh_x, sh_y], [2*sh_x, 2*sh_y], [3*sh_x, 3*sh_y]])\n",
" for number in numbers:\n",
" bool_arr = []\n",
" for segment in (segments + number + (start_x[i], start_y[i])):\n",
" ax[1][i].plot(segment[1], segment[0], '.r')\n",
" area = warped[i][(segment[0] - delta):(segment[0] + delta), (segment[1] - delta):(segment[1] + delta)]\n",
"\n",
" bool_arr.append(np.mean(area) > min_val)\n",
" # print(np.mean(area))\n",
" print(tuple2int[tuple(bool_arr)])\n",
" # print(bool_arr)\n",
" \n",
" for a in ax:\n",
" a[i].axis('off')\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c7df7aed-0b5e-47c8-b2df-0c7ea1eff160",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
},
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)\n",
"libGL error: failed to load driver: swrast\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 0\n",
"Error: 1\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Error: 0\n",
"Can't receive frame (stream end?). Exiting ...\n"
]
}
],
"source": [
"buf = None\n",
"\n",
"with open(\"data.csv\", \"w\") as f:\n",
" f.write(\"U[V], I[uA]\\n\")\n",
"\n",
"cap = cv.VideoCapture('video5v.mp4')\n",
"prev = (0, 0)\n",
"\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" \n",
" # if frame is read correctly ret is True\n",
" if not ret:\n",
" print(\"Can't receive frame (stream end?). Exiting ...\")\n",
" break\n",
" \n",
" img = color.rgb2gray(frame)[40:160, 60:810]\n",
" \n",
" warped[0] = transform.warp(img, tform3_1, output_shape=(50, 300))\n",
" warped[1] = transform.warp(img, tform3_2, output_shape=(50, 300))\n",
"\n",
" cv.imshow('frame', warped[1])\n",
" \n",
" if cv.waitKey(1) == ord('q'):\n",
" break\n",
" \n",
" v_nums = [[], []]\n",
" \n",
" for i in range(2):\n",
" sh_x = shift_x[i]\n",
" sh_y = shift_y[i]\n",
" numbers = np.array([[0, 0], [sh_x, sh_y], [2*sh_x, 2*sh_y], [3*sh_x, 3*sh_y]])\n",
" for number in numbers:\n",
" bool_arr = []\n",
" for segment in (segments + number + (start_x[i], start_y[i])):\n",
" area = warped[i][(segment[0] - delta):(segment[0] + delta), (segment[1] - delta):(segment[1] + delta)]\n",
" bool_arr.append(np.mean(area) > min_val)\n",
"\n",
" try:\n",
" v_nums[i].append(tuple2int[tuple(bool_arr)])\n",
" except KeyError:\n",
" buf = warped[i]\n",
" print(f\"Error: {i}\")\n",
" break\n",
" \n",
" if len(v_nums[0]) + len(v_nums[1]) == 8:\n",
" U = float(\"\".join(list(map(str, v_nums[0]))))/100\n",
" I = float(\"\".join(list(map(str, v_nums[1]))))/10\n",
" if prev != (U, I):\n",
" prev = (U, I)\n",
" with open(\"data.csv\", \"a\") as f:\n",
" f.write(f\"{U}, {I}\\n\")\n",
" \n",
"cap.release()\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "496a2ecf-5a9e-45a2-a815-cfaa72e31fa2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}