{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import matplotlib.pyplot as plt\n", "import keras\n", "import numpy as np\n", "import threading\n", "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def wrap_frozen_graph(graph_def, inputs, outputs, print_graph=False):\n", " def _imports_graph_def():\n", " tf.compat.v1.import_graph_def(graph_def, name=\"\")\n", "\n", " wrapped_import = tf.compat.v1.wrap_function(_imports_graph_def, [])\n", " import_graph = wrapped_import.graph\n", "\n", " if print_graph == True:\n", " print(\"-\" * 50)\n", " print(\"Frozen model layers: \")\n", " layers = [op.name for op in import_graph.get_operations()]\n", " for layer in layers:\n", " print(layer)\n", " print(\"-\" * 50)\n", "\n", " return wrapped_import.prune(\n", " tf.nest.map_structure(import_graph.as_graph_element, inputs),\n", " tf.nest.map_structure(import_graph.as_graph_element, outputs))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ " # Load frozen graph using TensorFlow 1.x functions\n", "with tf.io.gfile.GFile(\"./frozen_models/frozen_graph_best_vgg.pb\", \"rb\") as f:\n", " graph_def = tf.compat.v1.GraphDef()\n", " loaded = graph_def.ParseFromString(f.read())\n", "\n", "# Wrap frozen graph to ConcreteFunctions\n", "frozen_func = wrap_frozen_graph(graph_def=graph_def,\n", " inputs=[\"x:0\"],\n", " outputs=[\"Identity:0\"],\n", " print_graph=False)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jellyfish'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class_names=sorted(['Fish', \"Jellyfish\", 'Lionfish', 'Shark', 'Stingray', 'Turtle'])\n", "a = cv2.imread('test.PNG')\n", "# a.shape\n", "a = cv2.resize(a,(227,227))\n", "pred = frozen_func(x=tf.convert_to_tensor(a[None, :], dtype='float32'))\n", "label = class_names[np.argmax(pred)]\n", "label" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[51], line 42\u001b[0m\n\u001b[0;32m 39\u001b[0m roi \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mresize(roi, (\u001b[39m960\u001b[39m, \u001b[39m540\u001b[39m)) \n\u001b[0;32m 40\u001b[0m cv2\u001b[39m.\u001b[39mimshow(\u001b[39m\"\u001b[39m\u001b[39mroi\u001b[39m\u001b[39m\"\u001b[39m, roi)\n\u001b[1;32m---> 42\u001b[0m key \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39;49mwaitKey(\u001b[39m30\u001b[39;49m)\n\u001b[0;32m 43\u001b[0m \u001b[39mif\u001b[39;00m key \u001b[39m==\u001b[39m \u001b[39m27\u001b[39m:\n\u001b[0;32m 44\u001b[0m \u001b[39mbreak\u001b[39;00m\n", "\u001b[1;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "cap = cv2.VideoCapture(\"rybki4.mp4\")\n", "# cap.set(cv2.CAP_PROP_FPS, 60)\n", "\n", "class_names=sorted(['Fish', \"Jellyfish\", 'Lionfish', 'Shark', 'Stingray', 'Turtle'])\n", "object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=50)\n", "\n", "\n", "# width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) \n", "# height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", "# fps = cap.get(cv2.CAP_PROP_FPS)\n", "# out = cv2.VideoWriter('track_fish.avi', cv2.VideoWriter_fourcc(*'MJPG'), fps, (width, height))\n", "\n", "while True:\n", " ret, frame = cap.read()\n", " if(ret):\n", " roi = frame[100: 900,330:1900]\n", " mask = object_detector.apply(roi)\n", " _, mask = cv2.threshold(mask,254,255, cv2.THRESH_BINARY)\n", " conturs, _ =cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n", "\n", " images = []\n", " for cnt in conturs:\n", " area = cv2.contourArea(cnt)\n", " if area > 300:\n", " #cv2.drawContours(roi,[cnt],-1,(0,255,0),2)\n", " x,y,w,h = cv2.boundingRect(cnt)\n", " rectangle = cv2.rectangle(roi,(x,y),(x+w,y+h),(0,255,0),3)\n", " # images.append((x,y,rectangle,np.expand_dims(image_to_predict,axis=0)))\n", " # image_to_predict = roi[y:y+h,x:x+w]\n", " # image_to_predict = cv2.resize(image_to_predict,(227,227))\n", " # pred = frozen_func(x=tf.convert_to_tensor(image_to_predict[None, :], dtype='float32'))\n", " # label = class_names[np.argmax(pred)]\n", " # cv2.putText(rectangle, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 1)\n", " \n", " # if images:\n", " # pred = model.predict(np.vstack([image[3] for image in images]))\n", " # labels = [class_names[np.argmax(pre)] for pre in pred]\n", " # for i,image in enumerate(images):\n", " roi = cv2.resize(roi, (960, 540)) \n", " cv2.imshow(\"roi\", roi)\n", "\n", " key = cv2.waitKey(30)\n", " if key == 27:\n", " break\n", "\n", " #out.write(frame)\n", " else:\n", " break\n", "\n", "\n", "#out.release()\n", "cap.release()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "um", "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.9.15" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "876e189cbbe99a9a838ece62aae1013186c4bb7e0254a10cfa2f9b2381853efb" } } }, "nbformat": 4, "nbformat_minor": 2 }