File size: 3,848 Bytes
c636b54 60bfd82 c636b54 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | {
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"source": [
"import jsonlines\n",
"import os"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"import cv2"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 8,
"source": [
"# extract first frame from mp4 file and save it as image\n",
"\n",
"def extract_frame(mp4,file_name):\n",
" save_path='./imgs/'+file_name[:-4]+\".jpg\"\n",
" # print(save_path)\n",
" if os.path.exists(save_path):\n",
" # print(\"file already exists\")\n",
" return\n",
" else:\n",
" try:\n",
" # extract first frame\n",
" cap = cv2.VideoCapture(mp4)\n",
" ret, frame = cap.read()\n",
" cv2.imwrite(save_path, frame)\n",
" except:\n",
" print(f\"error in {save_path} file\")"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 16,
"source": [
"# read jsonlines\n",
"im_un=[]\n",
"base_dir=\"./gifs\"\n",
"data=jsonlines.open('./ReactionGIF.json')\n",
"\n",
"train_writer=jsonlines.open(\"train.json\", mode='w')\n",
"val_writer=jsonlines.open(\"val.json\", mode='w')\n",
"writer=train_writer\n",
"\n",
"for x in data:\n",
" text=x[\"text\"]\n",
" sentiment=x[\"label\"]\n",
" image_name=x[\"reply\"]\n",
" if image_name is None:\n",
" continue\n",
" else:\n",
" image_name=image_name.split(\"/\")[-1]\n",
" jpg_name='/home/ceyda/data/ReactionGIF/imgs/'+image_name[:-4]+\".jpg\"\n",
"\n",
" # image_path=os.path.join(base_dir,image_name)\n",
" # extract_frame(image_path,image_name)\n",
" if os.path.exists(jpg_name):\n",
" dic={\n",
" \"image_path\":jpg_name,\n",
" \"captions\":[text,sentiment]\n",
" }\n",
" im_un.append(jpg_name)\n",
" if len(im_un)>=19000:\n",
" writer=val_writer\n",
" writer.write(dic)\n",
" # text,jpg_name,sentiment"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 13,
"source": [
"len(im_un)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"19387"
]
},
"metadata": {},
"execution_count": 13
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 14,
"source": [
"len(set(im_un))"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"19385"
]
},
"metadata": {},
"execution_count": 14
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 17,
"source": [
"import jax"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 18,
"source": [
"jax.device_count()"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"8"
]
},
"metadata": {},
"execution_count": 18
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"import shutil\n",
"from pathlib import Path\n",
"val=jsonlines.open('./val.json')\n",
"for v in val:\n",
"\n",
" shutil.copy(v['image_path'],\"/home/ceyda/code/clip-reply-demo/imgs/\" +Path(v['image_path']).name)\n"
],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"orig_nbformat": 4,
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
} |