{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=pd.read_csv('../input/global-wheat-detection/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def get_list(a):\n  a=list(a.strip('[').strip(']'))\n  val='0'\n  liz=[]\n  for i in a:\n    if(i!=',' and i!=' '):\n      val+=i\n    elif (i==' '):\n      liz.append(float(val))\n      val='0'  \n  liz.append(float(val))\n  return liz\n\nids=[]\nbboxes=[]\n\nfor index,id in enumerate(data['image_id']):\n  count=0\n  for i in ids:\n    if(i==id):\n      count+=1\n  if(count==0):\n    bbox_array=np.zeros((16,16,5))\n    for index2,id2 in enumerate(data['image_id']):\n      if(id2==id):\n        bboz=get_list(data['bbox'][index2])\n        w=int(bboz[2])\n        h=int(bboz[3])\n        x=int(bboz[0]+bboz[2]/2)\n        y=int(bboz[1]+bboz[3]/2)\n        box1=int(x/64)\n        box2=int(y/64)\n        bbox_array[box1,box2,:]=(x%64)/64,(y%64)/64,w/1024,h/1024,1\n    bboxes.append(bbox_array)\n    ids.append(id)\n  print(index)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def flip_horizontal(img,bboxes):\n  img_center = np.array(img.shape[:2])[::-1]/2\n  img_center = np.hstack((img_center, img_center))\n  img =  img[:,::-1,:]\n  bboxes[:,[0,2]] += 2*(img_center[[0,2]] - bboxes[:,[0,2]])\n  box_w = abs(bboxes[:,0] - bboxes[:,2])\n  bboxes[:,0] -= box_w\n  bboxes[:,2] += box_w\n  return img, bboxes\n\ndef flip_vertical(img,bboxes):\n  img_center = np.array(img.shape[:2])[::-1]/2\n  img_center = np.hstack((img_center, img_center))\n  img =  img[::-1,:,:]\n  bboxes[:,[1,3]] += 2*(img_center[[1,3]] - bboxes[:,[1,3]])\n  box_h = abs(bboxes[:,1] - bboxes[:,3])\n  bboxes[:,1] -= box_h\n  bboxes[:,3] += box_h\n  return img, bboxes\n\ndef flip_horizontal_vertical(img,bboxes):\n  img_center = np.array(img.shape[:2])[::-1]/2\n  img_center = np.hstack((img_center, img_center))\n  img =  img[:,::-1,:]\n  bboxes[:,[0,2]] += 2*(img_center[[0,2]] - bboxes[:,[0,2]])\n  box_w = abs(bboxes[:,0] - bboxes[:,2])\n  bboxes[:,0] -= box_w\n  bboxes[:,2] += box_w\n\n  img_center = np.array(img.shape[:2])[::-1]/2\n  img_center = np.hstack((img_center, img_center))\n  img =  img[::-1,:,:]\n  bboxes[:,[1,3]] += 2*(img_center[[1,3]] - bboxes[:,[1,3]])\n  box_h = abs(bboxes[:,1] - bboxes[:,3])\n  bboxes[:,1] -= box_h\n  bboxes[:,3] += box_h\n\n\n  return img, bboxes\n\n\ndef flip_vertical_horizontal(img,bboxes):\n  img_center = np.array(img.shape[:2])[::-1]/2\n  img_center = np.hstack((img_center, img_center))\n  img =  img[::-1,:,:]\n  bboxes[:,[1,3]] += 2*(img_center[[1,3]] - bboxes[:,[1,3]])\n  box_h = abs(bboxes[:,1] - bboxes[:,3])\n  bboxes[:,1] -= box_h\n  bboxes[:,3] += box_h\n\n  img_center = np.array(img.shape[:2])[::-1]/2\n  img_center = np.hstack((img_center, img_center))\n  img =  img[:,::-1,:]\n  bboxes[:,[0,2]] += 2*(img_center[[0,2]] - bboxes[:,[0,2]])\n  box_w = abs(bboxes[:,0] - bboxes[:,2])\n  bboxes[:,0] -= box_w\n  bboxes[:,2] += box_w\n\n\n  return img, bboxes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"boxes=[]\nfor id in ids:\n    bboz=[]\n    for index2,id2 in enumerate(data['image_id']):\n      if(id2==id):\n        lis=get_list(data['bbox'][index2])\n        lis[2]=lis[0]+lis[2]\n        lis[3]=lis[1]+lis[3]\n        bboz.append(lis)\n    boxes.append(bboz)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nfor index,id in enumerate(ids):\n    filename=id\n    filename='../input/global-wheat-detection/train/'+id+'.jpg'\n    print(filename)\n    img=cv2.imread(filename)\n    img_h,bboz1=flip_horizontal(img,np.array(boxes[index]))\n    img_v,bboz2=flip_vertical(img,np.array(boxes[index]))\n    img_vh,bboz3=flip_vertical_horizontal(img,np.array(boxes[index]))\n    img_hv,bboz4=flip_horizontal_vertical(img,np.array(boxes[index]))\n    cv2.imwrite(id+'.jpg',img)\n    cv2.imwrite(id+'_h'+'.jpg',img_h)\n    cv2.imwrite(id+'_v'+'.jpg',img_v)\n    cv2.imwrite(id+'_vh'+'.jpg',img_vh)\n    cv2.imwrite(id+'_hv'+'.jpg',img_hv)\n    \n    \n    \n    \n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#for i in bboz4:\n    #x1=int(i[0])\n   # y1=int(i[1])\n    #x2=int(i[2])\n    #y2=int(i[3])\n    #fig = plt.figure() \n    #ax = fig.add_subplot(111) \n    #ax.imshow(img_hv)\n    #ax.add_patch( Rectangle((x1, y1), \n                 #       (x2-x1),(y2-y1), \n                ##        fc ='none',  \n                #        ec ='g', \n                   #     lw = 1) ) \n    \n#plt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}