{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[INFO ] [Logger ] Record log in C:\\Users\\Jack sklavinic\\.kivy\\logs\\kivy_21-02-04_1.txt\n", "[INFO ] [Kivy ] v1.11.1\n", "[INFO ] [Kivy ] Installed at \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\kivy\\__init__.py\"\n", "[INFO ] [Python ] v3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]\n", "[INFO ] [Python ] Interpreter at \"C:\\ProgramData\\Anaconda3\\python.exe\"\n", "[INFO ] [Factory ] 184 symbols loaded\n", "[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)\n" ] } ], "source": [ "import cv2\n", "import sys\n", "import winsound\n", "import random\n", "\n", "\n", "class Tone_Block():\n", " def __init__(self, rect_left_top_x, rect_left_top_y, tone=1, edge_length=20, color=(255,0,0)):\n", " self.rect_start=[rect_left_top_x, rect_left_top_y]\n", " self.rect_end=[rect_left_top_x+edge_length, rect_left_top_y+edge_length]\n", " self.color = color\n", " self.tone = tone\n", " self.status =True #allow make sound\n", " \n", " def display(self, frame):\n", " return cv2.rectangle(frame, tuple(self.rect_start), tuple(self.rect_end), self.color, 1, 0)\n", " \n", " def make_sound(self):\n", " self.status = False\n", " return winsound.Beep(self.tone,200)\n", "\n", " def overlapping(self, bbox_pos, frame):\n", " R1= [0,0,0,0]\n", " R2= [0,0,0,0] \n", " R1[0]=self.rect_start[0]\n", " R1[1]=self.rect_start[1]\n", " R1[2]=self.rect_end[0]\n", " R1[3]=self.rect_end[1]\n", " \n", " R2[0]=bbox_pos[0]\n", " R2[1]=bbox_pos[1]\n", " R2[2]=bbox_pos[0]+bbox_pos[2]\n", " R2[3]=bbox_pos[1]+bbox_pos[3]\n", " cv2.putText(frame, str(R1), (100,160), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)\n", " cv2.putText(frame, str(R2), (100,240), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)\n", "\n", " if R1[0]>=R2[2] or R2[0]>=R1[2]:\n", " self.status = True\n", " return False\n", " if R1[1]>=R2[3] or R2[1]>=R1[3]:\n", " self.status = True\n", " return False\n", " return True\n", " \n", "\n", "def activation(toneblock_list, bbox_pos, frame):\n", " for toneblock in toneblock_list:\n", " if toneblock.overlapping(bbox_pos,frame):\n", " if toneblock.status == True:\n", " toneblock.make_sound()\n", "\n", "(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')\n", "\n", "from kivy.app import App\n", "from kivy.uix.widget import Widget\n", "from kivy.uix.boxlayout import BoxLayout\n", "from kivy.uix.image import Image\n", "from kivy.clock import Clock\n", "from kivy.graphics.texture import Texture\n", "\n", "import cv2\n", "\n", "class CamApp(App):\n", "\n", " def build(self):\n", " self.img1=Image()\n", " layout = BoxLayout()\n", " layout.add_widget(self.img1)\n", " #opencv2 stuffs\n", " self.capture = cv2.VideoCapture(0)\n", " cv2.namedWindow(\"CV2 Image\")\n", " Clock.schedule_interval(self.update, 1.0/33.0)\n", " return layout\n", "\n", " def update(self, dt):\n", " # display image from cam in opencv window\n", " ret, frame = self.capture.read()\n", " cv2.imshow(\"CV2 Image\", frame)\n", " # convert it to texture\n", " buf1 = cv2.flip(frame, 0)\n", " buf = buf1.tostring()\n", " texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr') \n", " #if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick with \"bgr\" in blit_buffer. \n", " texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')\n", " # display image from the texture\n", " self.img1.texture = texture1\n", "\n", "if __name__ == '__main__' :\n", "\n", " # Set up tracker.\n", " # Instead of MIL, you can also use\n", "\n", " tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']\n", " tracker_type = tracker_types[0]\n", "\n", " if int(minor_ver) < 3:\n", " tracker = cv2.Tracker_create(tracker_type)\n", " else:\n", " if tracker_type == 'BOOSTING':\n", " tracker = cv2.TrackerBoosting_create()\n", " if tracker_type == 'MIL':\n", " tracker = cv2.TrackerMIL_create()\n", " if tracker_type == 'KCF':\n", " tracker = cv2.TrackerKCF_create()\n", " if tracker_type == 'TLD':\n", " tracker = cv2.TrackerTLD_create()\n", " if tracker_type == 'MEDIANFLOW':\n", " tracker = cv2.TrackerMedianFlow_create()\n", " if tracker_type == 'GOTURN':\n", " tracker = cv2.TrackerGOTURN_create()\n", " if tracker_type == 'MOSSE':\n", " tracker = cv2.TrackerMOSSE_create()\n", " if tracker_type == \"CSRT\":\n", " tracker = cv2.TrackerCSRT_create()\n", "\n", " # Read video\n", " video = cv2.VideoCapture(\"./eu6.mp4\")\n", " \n", "\n", " # Exit if video not opened.\n", " if not video.isOpened():\n", " print (\"Could not open video\")\n", " sys.exit()\n", "\n", " # Read first frame.\n", " ok, frame = video.read()\n", " frame = cv2.resize(frame,(1024,768))\n", " #frame = imutils.resize(frame, width=320)\n", " if not ok:\n", " print ('Cannot read video file')\n", " sys.exit()\n", "\n", " cv2.moveWindow('ROI selector', 100, 100)\n", "\n", " \n", " toneblock1 = Tone_Block(380,380,800)\n", " toneblock2 = Tone_Block(480,380,1000)\n", " toneblock3 = Tone_Block(580,380,1200)\n", " toneblock4 = Tone_Block(380,280,1400)\n", " toneblock5 = Tone_Block(480,280,1600)\n", " toneblock6 = Tone_Block(580,280,1800)\n", " toneblock1.display(frame)\n", " toneblock2.display(frame)\n", " toneblock3.display(frame)\n", " toneblock4.display(frame)\n", " toneblock5.display(frame)\n", " toneblock6.display(frame)\n", " \n", " # Define an initial bounding box\n", " bbox = (287, 23, 86, 320)\n", " # Uncomment the line below to select a different bounding box\n", " bbox = cv2.selectROI(frame, False)\n", "\n", " # Initialize tracker with first frame and bounding box\n", " ok = tracker.init(frame, bbox)\n", "\n", " while True:\n", " # Read a new frame\n", " ok, frame = video.read()\n", " frame = cv2.resize(frame,(1024,768))\n", " if not ok:\n", " break\n", " \n", " cv2.moveWindow('Tracking', 100, 100)\n", " toneblock_list=[toneblock1,toneblock2,toneblock3,toneblock4,toneblock5,toneblock6]\n", " for toneblock in toneblock_list:\n", " toneblock.display(frame)\n", " \n", " # Start timer\n", " timer = cv2.getTickCount()\n", " # Update tracker\n", "\n", " ok, bbox = tracker.update(frame)\n", " activation(toneblock_list, bbox, frame)\n", " \n", " # Calculate Frames per second (FPS)\n", " fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);\n", "\n", " # Draw bounding box\n", " if ok:\n", " # Tracking success\n", " p1 = (int(bbox[0]), int(bbox[1]))\n", " p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))\n", " cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)\n", " else :\n", " # Tracking failure\n", " cv2.putText(frame, \"Tracking failure detected\", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)\n", "\n", " # Display tracker type on frame\n", " cv2.putText(frame, tracker_type + \" Tracker\", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);\n", " \n", " # Display FPS on frame\n", " cv2.putText(frame, \"FPS : \" + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);\n", "\n", " # Display result\n", " cv2.imshow(\"Tracking\", frame)\n", "\n", " # Exit if ESC pressed\n", " k = cv2.waitKey(1) & 0xff\n", " if k == 27 : \n", " video.release()\n", " break\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import kivy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.4 64-bit ('base': conda)", "language": "python", "name": "python37464bitbaseconda2372d2dfd64542a28a4b26c0ef4ec6cb" }, "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }