Created
September 21, 2025 14:53
-
-
Save MuhammetOzturk/ff82842d528d188ce88a111b1daafe93 to your computer and use it in GitHub Desktop.
Kamera Kayit.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyO9Ui/+AEiDtQYwcZFRF3tL", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/MuhammetOzturk/ff82842d528d188ce88a111b1daafe93/kamera-kayit.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "ACXBWosswvDd" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import cv2\n", | |
| "import numpy as np\n", | |
| "import base64\n", | |
| "from google.colab import output\n", | |
| "from IPython.display import display, HTML\n", | |
| "\n", | |
| "frames = []\n", | |
| "recording = False\n", | |
| "\n", | |
| "def save_frame(data):\n", | |
| " global frames, recording\n", | |
| " if not recording:\n", | |
| " return\n", | |
| " # base64 -> numpy array\n", | |
| " img_bytes = base64.b64decode(data.split(',')[1])\n", | |
| " img_array = np.frombuffer(img_bytes, np.uint8)\n", | |
| " frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR)\n", | |
| " frames.append(frame)\n", | |
| "\n", | |
| "def start_recording():\n", | |
| " global frames, recording\n", | |
| " frames = []\n", | |
| " recording = True\n", | |
| " print(\"Recording started...\")\n", | |
| "\n", | |
| "def stop_recording():\n", | |
| " global frames, recording\n", | |
| " recording = False\n", | |
| " print(f\"Recording stopped. {len(frames)} frames captured.\")\n", | |
| "\n", | |
| " # Kaydı video dosyası olarak kaydet\n", | |
| " if len(frames) > 0:\n", | |
| " h, w, _ = frames[0].shape\n", | |
| " out = cv2.VideoWriter(\"output.avi\", cv2.VideoWriter_fourcc(*'XVID'), 30.0, (w, h))\n", | |
| " for f in frames:\n", | |
| " out.write(f)\n", | |
| " out.release()\n", | |
| " print(\"Video kaydedildi: output.avi\")\n", | |
| "\n", | |
| "# Callbacks\n", | |
| "output.register_callback('notebook.save_frame', save_frame)\n", | |
| "output.register_callback('notebook.start_recording', start_recording)\n", | |
| "output.register_callback('notebook.stop_recording', stop_recording)\n", | |
| "\n", | |
| "# HTML + JS\n", | |
| "display(HTML(\"\"\"\n", | |
| "<div>\n", | |
| " <video id=\"video\" autoplay playsinline width=\"320\" height=\"240\" style=\"border:1px solid black;\"></video>\n", | |
| " <br>\n", | |
| " <button id=\"recordBtn\">🎥 Basılı Tut</button>\n", | |
| "</div>\n", | |
| "\n", | |
| "<script>\n", | |
| "let video = document.getElementById('video');\n", | |
| "let btn = document.getElementById('recordBtn');\n", | |
| "let stream;\n", | |
| "let sending = false;\n", | |
| "\n", | |
| "async function setupCamera() {\n", | |
| " stream = await navigator.mediaDevices.getUserMedia({video: true});\n", | |
| " video.srcObject = stream;\n", | |
| "}\n", | |
| "setupCamera();\n", | |
| "\n", | |
| "// Frame gönderme\n", | |
| "function captureFrame() {\n", | |
| " if (!sending) return;\n", | |
| " let canvas = document.createElement('canvas');\n", | |
| " canvas.width = video.videoWidth;\n", | |
| " canvas.height = video.videoHeight;\n", | |
| " let ctx = canvas.getContext('2d');\n", | |
| " ctx.drawImage(video, 0, 0, canvas.width, canvas.height);\n", | |
| " let data = canvas.toDataURL('image/jpeg', 0.8);\n", | |
| " google.colab.kernel.invokeFunction('notebook.save_frame', [data], {});\n", | |
| " requestAnimationFrame(captureFrame);\n", | |
| "}\n", | |
| "\n", | |
| "// Butona basılı tutulduğunda\n", | |
| "btn.onmousedown = () => {\n", | |
| " google.colab.kernel.invokeFunction('notebook.start_recording', [], {});\n", | |
| " sending = true;\n", | |
| " captureFrame();\n", | |
| "};\n", | |
| "btn.onmouseup = () => {\n", | |
| " sending = false;\n", | |
| " google.colab.kernel.invokeFunction('notebook.stop_recording', [], {});\n", | |
| "};\n", | |
| "</script>\n", | |
| "\"\"\"))\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "!ffmpeg -y -i output.avi output.mp4 &>/dev/null\n", | |
| "from IPython.display import Video\n", | |
| "Video('output.mp4',embed=True)" | |
| ], | |
| "metadata": { | |
| "id": "1yggZF-kw1Pm" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment