{"metadata":{"kernelspec":{"display_name":"Python 3","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.6.0"}},"nbformat":4,"nbformat_minor":0,"cells":[{"metadata":{"_cell_guid":"09bb203f-0d4e-a175-d26a-0bfdde76e084","_active":false,"collapsed":false},"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"cell_type":"code","outputs":[]},{"metadata":{"_cell_guid":"7a2ff1eb-563c-0daa-040f-3fe5ccc3b208","_active":false,"collapsed":false},"source":"\n#!/usr/bin/env python\n#################################################################\n# Name              : haar_positive_file_creator.py\n# Version           : 1.0a\n# Date created on   : 11/08/2015\n# Date modified on  : 11/08/2015\n# Description       : A small python tool to create a postive\n#                   : file for haar training\n#################################################################\n\n# module imports\n\nimport cv2\nimport sys\nimport glob\n\n# global variables\n\ndebug = 1\nobj_list = []\nobj_count = 0\nclick_count = 0\nx1 = 0\ny1 = 0\nh = 0\nw = 0\nkey = None\nframe = None\n\n# mouse callback\n\ndef obj_marker(event,x,y,flags,param):\n    global click_count\n    global debug\n    global obj_list\n    global obj_count\n    global x1\n    global y1\n    global w\n    global h\n    global frame\n    if event == cv2.EVENT_LBUTTONDOWN:\n        click_count += 1\n        if click_count % 2 == 1:\n            x1 = x\n            y1 = y\n        else:\n            w = abs(x1 - x)\n            h = abs(y1 - y)\n            obj_count += 1\n            if x1 > x:\n                x1 = x\n            if y1 > y:\n                y1 = y\n            obj_list.append('%d %d %d %d ' % (x1,y1,w,h))\n            if debug > 0:\n                print(obj_list)\n            cv2.rectangle(frame,(x1,y1),(x1+w,y1+h),(255,0,0),5)\n            cv2.imshow('frame',frame)\n\n\nif len(sys.argv) != 3:\n    print('Usage : python haar_positive_creator.py /path/to/location output_filename.txt') \nelse:\n    if debug > 0:\n        print('Arguments are ok')\n        print('Path is : %s' % sys.argv[1]) \n        print('Output file is : %s' % sys.argv[2]) \n        print('Click on edges you want to mark as an object') \n        print('Press q to quit') \n        print('Press c to cancel markings') \n        print('Press n to load next image') \n    #getting list of jpgs files from\n    list = glob.glob('%s/*.jpg' % sys.argv[1])\n    if debug > 0:\n        print(list) \n    #creating window for frame and setting mouse callback\n    cv2.namedWindow('frame',cv2.WINDOW_AUTOSIZE)\n    cv2.setMouseCallback('frame',obj_marker)\n    #creating a file handle\n    file_name = open(sys.argv[2],\"w\")\n    #loop to traverse through all the files in given path\n    for i in list:\n        frame = cv2.imread(i)                                                   # reading file\n        cv2.imshow('frame',frame)                                               # showing it in frame\n        obj_count = 0                                                           #initializing obj_count\n        key = cv2.waitKey(0)                                                    # waiting for user key\n        while((key & 0xFF != ord('q')) and (key & 0xFF != ord('n'))):           # wait till key pressed is q or n\n            key = cv2.waitKey(0)                                                # if not, wait for another key press\n            if(key & 0xFF == ord('c')):                                         # if key press is c, cancel previous markings\n                obj_count = 0                                                   # initializing obj_count and list\n                obj_list = []\n                frame = cv2.imread(i)                                           # read original file\n                cv2.imshow('frame',frame)                                       # refresh the frame\n        if(key & 0xFF == ord('q')):                                             # if q is pressed\n            break                                                               # exit\n        elif(key & 0xFF == ord('n')):                                           # if n is pressed\n            if(obj_count > 0):                                                  # and obj_count > 0\n                str1 = '%s %d ' % (i,obj_count)                                 # write obj info in file\n                file_name.write(str1)\n                for j in obj_list:\n                    file_name.write(j)\n                file_name.write('\\n')\n                obj_count = 0\n                obj_list = []\n    file_name.close()                                                           # end of the program; close the file\ncv2.destroyAllWindows()\n","execution_count":4,"cell_type":"code","outputs":[],"execution_state":"idle"}]}