{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":96745,"databundleVersionId":11513148,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install transformers torch opencv-python pandas tqdm Pillow","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:38:45.220284Z","iopub.execute_input":"2025-08-24T22:38:45.220783Z","iopub.status.idle":"2025-08-24T22:39:58.616803Z","shell.execute_reply.started":"2025-08-24T22:38:45.220763Z","shell.execute_reply":"2025-08-24T22:39:58.616030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport pandas as pd\nimport cv2\nfrom PIL import Image\nfrom transformers import BlipProcessor, BlipForConditionalGeneration\nfrom tqdm import tqdm\nimport os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:40:07.104369Z","iopub.execute_input":"2025-08-24T22:40:07.104656Z","iopub.status.idle":"2025-08-24T22:40:30.819529Z","shell.execute_reply.started":"2025-08-24T22:40:07.104626Z","shell.execute_reply":"2025-08-24T22:40:30.818881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\n\nmodel_id = \"Salesforce/blip-image-captioning-large\"\nprocessor = BlipProcessor.from_pretrained(model_id)\nmodel = BlipForConditionalGeneration.from_pretrained(model_id).to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:40:33.332692Z","iopub.execute_input":"2025-08-24T22:40:33.333722Z","iopub.status.idle":"2025-08-24T22:40:44.477982Z","shell.execute_reply.started":"2025-08-24T22:40:33.333695Z","shell.execute_reply":"2025-08-24T22:40:44.477383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def generate_caption_for_video(video_path):\n    \"\"\"\n    Извлекает центральный кадр из видео и генерирует для него описание.\n    \"\"\"\n    try:\n        # Открываем видеофайл\n        cap = cv2.VideoCapture(video_path)\n        if not cap.isOpened():\n            print(f\"Ошибка: не удалось открыть видео {video_path}\")\n            return \"Could not process video.\"\n\n        # Находим центральный кадр\n        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n        middle_frame_index = total_frames // 2\n        \n        # Устанавливаем позицию для чтения центрального кадра\n        cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_index)\n        \n        ret, frame = cap.read()\n        cap.release()\n\n        if not ret:\n            print(f\"Ошибка: не удалось прочитать кадр из видео {video_path}\")\n            return \"Could not read frame from video.\"\n\n        # OpenCV читает изображения в формате BGR, а модель ожидает RGB\n        # Конвертируем цветовое пространство и создаем PIL Image\n        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        image = Image.fromarray(frame_rgb)\n\n        # --- Генерация описания ---\n        # Подготавливаем изображение для модели\n        inputs = processor(images=image, return_tensors=\"pt\").to(device)\n        \n        # Генерируем ID токенов (слов)\n        output_ids = model.generate(**inputs, max_length=200, num_beams=5)\n        \n        # Декодируем ID в текст\n        caption = processor.decode(output_ids[0], skip_special_tokens=True)\n        \n        return caption.strip()\n\n    except Exception as e:\n        print(f\"Произошла ошибка при обработке {video_path}: {e}\")\n        return \"Error during processing.\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:50:32.961056Z","iopub.execute_input":"2025-08-24T22:50:32.961360Z","iopub.status.idle":"2025-08-24T22:50:32.968476Z","shell.execute_reply.started":"2025-08-24T22:50:32.961340Z","shell.execute_reply":"2025-08-24T22:50:32.967666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TEST_CSV_PATH = '/kaggle/input/automated-video-captioning/test.csv'  \nVIDEO_FOLDER = '/kaggle/input/automated-video-captioning/test_videos' \n\n\ntest_df = pd.read_csv(TEST_CSV_PATH)\n\n\ncaptions = []\n\nfor video_file in tqdm(test_df['file_name'], desc=\"de\"):\n    full_video_path = os.path.join(VIDEO_FOLDER, video_file)\n    \n    generated_caption = generate_caption_for_video(full_video_path)\n    captions.append(generated_caption)\n\ntest_df['caption'] = captions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:50:35.413320Z","iopub.execute_input":"2025-08-24T22:50:35.413632Z","iopub.status.idle":"2025-08-24T22:55:00.567978Z","shell.execute_reply.started":"2025-08-24T22:50:35.413613Z","shell.execute_reply":"2025-08-24T22:55:00.567327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['caption'].sample","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:55:15.949245Z","iopub.execute_input":"2025-08-24T22:55:15.949531Z","iopub.status.idle":"2025-08-24T22:55:15.956838Z","shell.execute_reply.started":"2025-08-24T22:55:15.949510Z","shell.execute_reply":"2025-08-24T22:55:15.956144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'index': test_df.index,\n    'file_name': test_df['file_name'],\n    'caption': test_df['caption']\n})\n\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T22:55:18.920366Z","iopub.execute_input":"2025-08-24T22:55:18.921026Z","iopub.status.idle":"2025-08-24T22:55:18.929503Z","shell.execute_reply.started":"2025-08-24T22:55:18.920994Z","shell.execute_reply":"2025-08-24T22:55:18.928851Z"}},"outputs":[],"execution_count":null}]}