{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport os.path as osp\nimport time\nimport datetime\nimport random\nfrom PIL import Image\nimport numpy as np\nfrom sklearn.metrics import accuracy_score\nimport torch.nn as nn\nfrom torch.autograd import Function\nimport torch.optim as optim\nimport torch.nn.functional as F\nimport tensorflow as tf\nimport torch\nimport pandas as pd\nimport torch.utils.data as data\nfrom torch.utils.data import Dataset, TensorDataset, DataLoader, RandomSampler, SequentialSampler, WeightedRandomSampler\nimport logging\nfrom torchvision import datasets,transforms, models\nimport tqdm\nimport glob\nimport matplotlib.image as image\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-16T04:36:48.908010Z","iopub.execute_input":"2023-09-16T04:36:48.908423Z","iopub.status.idle":"2023-09-16T04:36:48.918255Z","shell.execute_reply.started":"2023-09-16T04:36:48.908389Z","shell.execute_reply":"2023-09-16T04:36:48.917126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Download YOLOv7 code\n!git clone https://github.com/WongKinYiu/yolov7\n%cd yolov7","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:36:48.920197Z","iopub.execute_input":"2023-09-16T04:36:48.921223Z","iopub.status.idle":"2023-09-16T04:36:53.926649Z","shell.execute_reply.started":"2023-09-16T04:36:48.921175Z","shell.execute_reply":"2023-09-16T04:36:53.925326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from models.yolo import Model\nfrom utils.general import check_requirements, set_logging\nfrom utils.google_utils import attempt_download\nfrom utils.torch_utils import select_device\nfrom  hubconf import custom\nmodel = custom(path_or_model='/kaggle/input/d/hongminhv/dambong/best.pt', autoshape = True) \n\nmodel.conf = 0.1","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:36:53.930635Z","iopub.execute_input":"2023-09-16T04:36:53.930962Z","iopub.status.idle":"2023-09-16T04:37:00.715948Z","shell.execute_reply.started":"2023-09-16T04:36:53.930930Z","shell.execute_reply":"2023-09-16T04:37:00.714824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:37:00.717826Z","iopub.execute_input":"2023-09-16T04:37:00.718240Z","iopub.status.idle":"2023-09-16T04:37:00.726511Z","shell.execute_reply.started":"2023-09-16T04:37:00.718190Z","shell.execute_reply":"2023-09-16T04:37:00.723643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nimg = Image.open('/kaggle/input/hsgshackathon2021/Test_data/Test/video_60a/frame_003852.PNG')\n\nresult = model(img)\nprint(result.pandas().xyxy[0])\n# Run evaluation\n!python detect.py --weights /kaggle/input/dambong/best.pt --conf 0.1 --source /kaggle/input/hsgshackathon2021/Test_data/Test/video_60a/frame_003852.PNG","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:37:00.729728Z","iopub.execute_input":"2023-09-16T04:37:00.730089Z","iopub.status.idle":"2023-09-16T04:37:10.788086Z","shell.execute_reply.started":"2023-09-16T04:37:00.730053Z","shell.execute_reply":"2023-09-16T04:37:10.786751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nfrom IPython.display import Image, display\n\ni = 0\nlimit = 10000 # max images to print\nfor imageName in glob.glob('/kaggle/working/yolov7/runs/detect/exp4/frame_003852.PNG'): #assuming JPG\n    if i < limit:\n      display(Image(filename=imageName))\n      print(\"\\n\")\n    i = i + 1","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:37:10.791125Z","iopub.execute_input":"2023-09-16T04:37:10.791565Z","iopub.status.idle":"2023-09-16T04:37:10.801026Z","shell.execute_reply.started":"2023-09-16T04:37:10.791500Z","shell.execute_reply":"2023-09-16T04:37:10.799999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TEST_DATA\nfrom PIL import Image\ntest_data = np.array([])\nans = []\ntest_pred = []\nfor r, dirnames, fn in os.walk('/kaggle/input/hsgshackathon2021/Test_data/Test'):\n    for dirname in sorted(dirnames):\n       # print(dirname)\n        cur_path = '/kaggle/input/hsgshackathon2021/Test_data/Test' + '/' + dirname\n        for filename in sorted(os.listdir(cur_path)):\n             ans.append((dirname+'_'+filename))\n             ans[(len(ans)-1)]=ans[(len(ans)-1)].strip('.PNG')\n             img = Image.open((cur_path+'/'+filename))\n             if dirname =='video_20b':\n                img = img.rotate(90)\n             result = model(img)\n             table = result.pandas().xyxy[0]\n             ball = []\n             hand = []\n             for i in range(0, len(table)):\n                    if table.name[i] == '0':\n                        ball1 = []\n                        ball1.append(table.xmin[i])\n                        ball1.append(table.xmax[i])\n                        ball1.append(table.ymin[i])\n                        ball1.append(table.ymax[i])\n                        ball.append(ball1)\n                    elif table.name[i] == '1':\n                        hand1 = []\n                        hand1.append(table.xmin[i])\n                        hand1.append(table.xmax[i])\n                        hand1.append(table.ymin[i])\n                        hand1.append(table.ymax[i])\n                        hand.append(hand1)\n             mx = 0\n            \n             for i in ball:\n                    for j in (hand):\n                        ballxmin = i[0]\n                        ballxmax = i[1]\n                        ballymin = i[2]\n                        ballymax = i[3]\n                        handxmin = j[0]\n                        handxmax = j[1]\n                        handymin = j[2]\n                        handymax = j[3]\n                        if ballxmin <= handxmax and handxmax <= ballxmax and (not(ballymin>handymax or handymin>ballymax)):\n                            mx = 1\n                        if handxmin <= ballxmax and ballxmax <= handxmax and (not(ballymin>handymax or handymin>ballymax)): \n                            mx = 1\n                        if (abs(ballxmin-handxmax) <= 4) and (not(ballymin>handymax or handymin>ballymax)):\n                            mx = 1\n                        if abs(ballxmax-handxmin) <= 4 and (not(ballymin>handymax or handymin>ballymax)):\n                            mx = 1\n             test_pred.append(mx)\n        \n           #  img = img.resize(224, 224)","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:39:27.447397Z","iopub.execute_input":"2023-09-16T04:39:27.448378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(data=zip(ans,test_pred),columns=[\"Frame\",\"Label\"])\nsubmission.head()\nprint(submission)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"vhm9.csv\", index=False)\n#kaggle competitions submit -c hsgshackathon2021 -f vhm7.csv -m \"Message\"","metadata":{"execution":{"iopub.status.busy":"2023-09-16T04:37:11.025851Z","iopub.status.idle":"2023-09-16T04:37:11.026616Z","shell.execute_reply.started":"2023-09-16T04:37:11.026331Z","shell.execute_reply":"2023-09-16T04:37:11.026355Z"},"trusted":true},"execution_count":null,"outputs":[]}]}